【发布时间】:2012-12-02 04:50:31
【问题描述】:
父视图代码,里面有一个刷新按钮:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
final TabHost tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec("Tab1")
.setIndicator("All", getResources().getDrawable(R.drawable.all))
.setContent(new Intent(this, All.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
tabHost.addTab(tabHost.newTabSpec("Tab2")
.setIndicator("One", getResources().getDrawable(android.R.drawable.one))
.setContent(new Intent(this, One.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
tabHost.addTab(tabHost.newTabSpec("Tab3")
.setIndicator("Two",getResources().getDrawable(android.R.drawable.two))
.setContent(new Intent(this, Two.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
tabHost.addTab(tabHost.newTabSpec("Tab4")
.setIndicator("Three",getResources().getDrawable(android.R.drawable.three))
.setContent(new Intent(this, Three.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
tabHost.getTabWidget().bringToFront();
setContentView(tabHost);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.title_bar);
tabHost.setCurrentTab(0);
}
我在每个子视图中定义了一个按钮,我使用了这个
refreshButton = (ImageButton) getParent().findViewById(R.id.refresh);
在父视图中获取按钮,但 refreshButton 始终为 null 并且 有一个例外: java.lang.RuntimeException: 无法启动活动 ComponentInfo{ParentView}:java.lang.NullPointerException。
所以请帮我解决这个问题,我该如何解决这个问题。谢谢!
【问题讨论】:
-
包含此类问题的堆栈跟踪很有帮助。从您发布的内容来看,我猜
getParent()正在返回null。 -
你的按钮
refresh在哪个类?这个代码在哪里被称为refreshButton = (ImageButton) getParent().findViewById(R.id.refresh);?? -
我大概知道为什么会这样,因为 getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.title_bar);在显示的 OneClass 之后运行,因此按钮始终为空。
标签: android nullpointerexception android-tabhost