所以,我正在关注Android教程的NewBoston系列。因此,本教程使用Tabhost和Tabspecs设置选项卡。现在,我完全尝试了教程的功能,但崩溃了,它指向我的代码中的一个特定行,它是OnCreate()中的setContentView()方法。我甚至尝试删除它并运行代码,但程序崩溃了。可能是我的错误?我还尝试了下面的文章中的代码,看看它是否有效,但它仍然崩溃。 http://www.learn-android-easily.com/2013/07/android-tabwidget-example.html
这是我的代码 -
Splash.java是启动Launcher活动的类
public class Splash extends TabActivity {
TabHost th;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
th = (TabHost)findViewById(R.id.tabHost2);
TabHost.TabSpec tab1 = th.newTabSpec("First Tab");
TabHost.TabSpec tab2 = th.newTabSpec("Second Tab");
TabHost.TabSpec tab3 = th.newTabSpec("Third tab");
tab1.setIndicator("Tab1");
tab1.setContent(new Intent(this,TabActivity1.class));
tab2.setIndicator("Tab2");
tab2.setContent(new Intent(this, TabActivity2.class));
tab3.setIndicator("Tab3");
tab3.setContent(new Intent(this, TabActivity3.class));
th.addTab(tab1);
th.addTab(tab2);
th.addTab(tab3);
}
}
TabActivity1.java -
public class TabActivity1 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv=new TextView(this);
tv.setTextSize(25);
tv.setGravity(Gravity.CENTER_VERTICAL);
tv.setText("This Is Tab1 Activity");
setContentView(tv);
}
}
TabActivity2.java: -
public class TabActivity2 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv=new TextView(this);
tv.setTextSize(25);
tv.setGravity(Gravity.CENTER_VERTICAL);
tv.setText("This Is Tab2 Activity");
setContentView(tv);
}
}
TabActivity3.java
public class TabActivity3 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv=new TextView(this);
tv.setTextSize(25);
tv.setGravity(Gravity.CENTER_VERTICAL);
tv.setText("This Is Tab3 Activity");
setContentView(tv);
}
}
splash.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TabHost
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tabHost2"
android:layout_gravity="center_horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"></TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"></LinearLayout>
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"></LinearLayout>
<LinearLayout
android:id="@+id/linearLayout3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"></LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
答案 0 :(得分:1)
更改此行
th = (TabHost)findViewById(R.id.tabHost2);
到
th = (TabHost)findViewById(android.R.id.tabHost2);
这样做。
因为在布局文件中,您可能设置了Tabhost id,如android:id="@android:id/tabhost2">