Android 添加选项卡时出错
我正在尝试使用此代码创建垂直选项卡 在oncreate中,我尝试添加选项卡 在tabHandler中,单击按钮可设置要查看的选项卡Android 添加选项卡时出错,android,uitableview,nullpointerexception,Android,Uitableview,Nullpointerexception,我正在尝试使用此代码创建垂直选项卡 在oncreate中,我尝试添加选项卡 在tabHandler中,单击按钮可设置要查看的选项卡 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tabHost = (TabHost)findViewById(
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabHost = (TabHost)findViewById(android.R.id.tabhost);
TabSpec firstTabSpec = tabHost.newTabSpec("tid1");
TabSpec secondTabSpec = tabHost.newTabSpec("tid2");
firstTabSpec.setIndicator("First Tab Name").setContent(new Intent(this,PageOne.class));
secondTabSpec.setIndicator("Second Tab Name").setContent(new Intent(this,PageTwo.class));
tabHost.addTab(firstTabSpec);
tabHost.addTab(secondTabSpec);
}
public void tabHandler(View target){
//artistButton.setSelected(false);
// albumButton.setSelected(false);
//songButton.setSelected(false);
if(target.getId() == R.id.artist_id){
tabHost.setCurrentTab(0);
// artistButton.setSelected(true);
} else if(target.getId() == R.id.album_id){
tabHost.setCurrentTab(1);
// albumButton.setSelected(true);
} else if(target.getId() == R.id.song_id){
tabHost.setCurrentTab(2);
// songButton.setSelected(true);
}
}
这是main.xml
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<FrameLayout android:layout_width="0dip"
android:layout_height="fill_parent" android:layout_weight="0.2">
<TabWidget android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone"/>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Button android:layout_height="0dip"
android:layout_width="fill_parent"
android:layout_weight="1.0"
android:id="@+id/artist_id"
android:onClick="tabHandler"/>
<Button android:layout_height="0dip"
android:layout_width="fill_parent"
android:layout_weight="1.0"
android:id="@+id/album_id"
android:onClick="tabHandler"/>
<Button android:layout_height="0dip"
android:layout_width="fill_parent"
android:layout_weight="1.0"
android:id="@+id/song_id"
android:onClick="tabHandler"/>
</LinearLayout>
</FrameLayout>
<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="0dip"
android:layout_height="fill_parent" android:layout_weight="0.8"/>
当这两行相加时:
tabHost.addTab(firstTabSpec);
tabHost.addTab(第二个tabspec)
但是我不知道为什么在这里输入代码你似乎在使用android.R而不仅仅是R来访问你的资源
android.R
访问不特定于您的应用程序的android资源,我假设您希望访问您创建的xml文件中的TabHost
tabHost = (TabHost)findViewById(android.R.id.tabhost);
应改为:
tabHost = (TabHost)findViewById(R.id.tabhost);
问题是扩展活动而不是扩展TabActivity有关更多信息,请参阅此链接谢谢@biddulph.r,但我发现问题是扩展活动而不是扩展TabActivity,但无论如何,感谢您的快速回复创建您自己的答案并接受它,以帮助将来遇到相同问题的其他人
tabHost = (TabHost)findViewById(R.id.tabhost);