我正在尝试在具有操作栏的活动中添加Tab控件。这是参考代码
MessageViewActivity.java
public class MessageViewActivity extends ActionBarActivity {
TabHost messageViewTabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_message_view);
messageViewTabHost = (TabHost)findViewById(R.id.messageViewTabHost);
TabHost.TabSpec spec;
Intent intent;
intent = new Intent().setClass(this, MessageCategoryTab.class);
spec = messageViewTabHost.newTabSpec("Category").setIndicator("Category").setContent(intent);
messageViewTabHost.addTab(spec);
activity_message_view.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MessageViewActivity">
<TabHost
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/messageViewTabHost"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<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">
</FrameLayout>
</LinearLayout>
</TabHost>
MessageCategoryTab.java
public class MessageCategoryTab extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_message_category_tab);
}
}
当我运行上面的代码时,错误显示如下
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.carbonrider.texto/com.carbonrider.texto.MessageViewActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TabWidget.addView(android.view.View)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TabWidget.addView(android.view.View)' on a null object reference
at android.widget.TabHost.addTab(TabHost.java:243)
at com.xyz.MessageViewActivity.onCreate(MessageViewActivity.java:31)
我完全不知道发生了什么。我已经看到其他帖子表明要使用片段等,但我需要知道当前代码有什么问题。
答案 0 :(得分:0)
我知道你说尝试安装方法,但在定义你的标签主机后尝试下面的代码:(
messageViewTabHost = (TabHost)findViewById(R.id.messageViewTabHost); )
LocalActivityManager mLocalActivityManager = new calActivityManager(this, false);
mLocalActivityManager.dispatchCreate(savedInstanceState);
messageViewTabHost.setup(mLocalActivityManager);
它适用于我!