[英]Why UnCaught Exception occurs in a TabActivity?
I have created a TabActivity and added TabWidget and frame to the layout. 我创建了一个TabActivity并将TabWidget和框架添加到布局中。
I have four other Activity classes with single label in each Activity. 我还有其他四个Activity类,每个Activity中都有一个标签。 Four tabs are associated with four tabs.
四个选项卡与四个选项卡相关联。
When I am trying to launch the application in emulator, I am getting the below exception 当我尝试在模拟器中启动应用程序时,出现以下异常
09-03 23:24:43.905: ERROR/AndroidRuntime(450): java.lang.NullPointerException 09-03 23:24:43.905: ERROR/AndroidRuntime(450): at android.widget.TabHost.dispatchWindowFocusChanged(TabHost.java:295) 09-03 23:24:43.905: ERROR/AndroidRuntime(450): at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:661) 09-03 23:24:43.905: ERROR/AndroidRuntime(450): at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:661)
09-03 23:24:43.905:错误/ AndroidRuntime(450):java.lang.NullPointerException 09-03 23:24:43.905:错误/ AndroidRuntime(450):位于android.widget.TabHost.dispatchWindowFocusChanged(TabHost.java: 295)09-03 23:24:43.905:ERROR / AndroidRuntime(450):位于android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:661)09-03 23:24:43.905:ERROR / AndroidRuntime(450):位于android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:661)
public class TabHome extends TabActivity {
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost tabHost=getTabHost();
TabHost.TabSpec tabSpec;
Resources res=getResources();
Intent intent;
intent=new Intent().setClass(this, MyMap.class);
tabSpec= tabHost.newTabSpec("Maps").setIndicator("Maps",res.getDrawable(R.drawable.ic_tab_artists)).setContent(intent);
tabHost.addTab(tabSpec);
intent=new Intent().setClass(this, MyStash.class);
tabSpec= tabHost.newTabSpec("Stash").setIndicator("Stash",res.getDrawable(R.drawable.ic_tab_artists)).setContent(intent);
tabHost.addTab(tabSpec);
intent=new Intent().setClass(this, MyList.class);
tabSpec= tabHost.newTabSpec("List").setIndicator("List",res.getDrawable(R.drawable.ic_tab_artists)).setContent(intent);
tabHost.addTab(tabSpec);
intent=new Intent().setClass(this, MySearch.class);
tabSpec= tabHost.newTabSpec("Search").setIndicator("Search",res.getDrawable(R.drawable.ic_tab_artists)).setContent(intent);
tabHost.addTab(tabSpec);
tabHost.setCurrentTab(2);
}
I have missed to mention the following attributes to main.xml 我错过了提到main.xml的以下属性
android:id="@android:id/tabhost" in TabHost tag TabHost标记中的android:id =“ @ android:id / tabhost”
and 和
android:id="@android:id/tabs" in TabWidget tag TabWidget标记中的android:id =“ @ android:id / tabs”
and 和
android:id="@android:id/tabcontent" in FrameLayout tag. FrameLayout标记中的android:id =“ @ android:id / tabcontent”。
Thanks all for your help! 感谢你的帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.