父视图代码,其中有一个刷新按钮:
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:Unable to start activity ComponentInfo{ParentView}:java.lang.NullPointerException。
所以请帮我解决这个问题,我该如何解决这个问题。谢谢!