0

嗨,我是这个“标签活动”概念的新手。我搜索了这个,最后我知道我应该实现“片段”。我完成了以下代码,但它显示错误。请指导我解决这个问题。

public class VendorActivity extends FragmentActivity
{
    private FragmentTabHost mTabHost;


    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vendor);

        mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);


         // Tab for Photos

        TabSpec photospec = mTabHost.newTabSpec("Photos");
        // setting Title and Icon for the Tab
        photospec.setIndicator("Photos");
        Intent photosIntent = new Intent(this, Simple.class);
        photospec.setContent(photosIntent);


        System.out.println("b4 fragment");
        TabSpec songspec = mTabHost.newTabSpec("Songs");
        songspec.setIndicator("Songs");
        Intent songsIntent = new Intent(this, Contacts.class);
        songspec.setContent(songsIntent);
        System.out.println("bafter fragment");

        System.out.println("1");
        mTabHost.addTab(photospec); // Error comes here.android
        System.out.println("2");
        mTabHost.addTab(songspec); 
        System.out.println("3");

//       mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"),
//                  FragmentStackSupport.sim.class, null);





    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_vendor, menu);
        return true;
    }

} 

我的 XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.app.FragmentTabHost
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"/>
    </LinearLayout>
    </android.support.v4.app.FragmentTabHost>

我的日志猫:

03-29 23:44:09.245: I/Process(447): Sending signal. PID: 447 SIG: 9
03-29 23:46:17.304: I/System.out(474): b4 fragment
03-29 23:46:17.304: I/System.out(474): bafter fragment
03-29 23:46:17.304: I/System.out(474): 1
03-29 23:46:17.444: D/AndroidRuntime(474): Shutting down VM
03-29 23:46:17.444: W/dalvikvm(474): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
03-29 23:46:17.584: E/AndroidRuntime(474): FATAL EXCEPTION: main
03-29 23:46:17.584: E/AndroidRuntime(474): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.vegetable/com.vegetable.VendorActivity}: java.lang.NullPointerException
03-29 23:46:17.584: E/AndroidRuntime(474):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
03-29 23:46:17.584: E/AndroidRuntime(474):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-29 23:46:17.584: E/AndroidRuntime(474):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-29 23:46:17.584: E/AndroidRuntime(474):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-29 23:46:17.584: E/AndroidRuntime(474):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-29 23:46:17.584: E/AndroidRuntime(474):  at android.os.Looper.loop(Looper.java:123)
03-29 23:46:17.584: E/AndroidRuntime(474):  at android.app.ActivityThread.main(ActivityThread.java:4627)
03-29 23:46:17.584: E/AndroidRuntime(474):  at java.lang.reflect.Method.invokeNative(Native Method)
03-29 23:46:17.584: E/AndroidRuntime(474):  at java.lang.reflect.Method.invoke(Method.java:521)
03-29 23:46:17.584: E/AndroidRuntime(474):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-29 23:46:17.584: E/AndroidRuntime(474):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-29 23:46:17.584: E/AndroidRuntime(474):  at dalvik.system.NativeStart.main(Native Method)
03-29 23:46:17.584: E/AndroidRuntime(474): Caused by: java.lang.NullPointerException
03-29 23:46:17.584: E/AndroidRuntime(474):  at android.widget.TabHost.addTab(TabHost.java:209)
03-29 23:46:17.584: E/AndroidRuntime(474):  at com.vegetable.VendorActivity.onCreate(VendorActivity.java:50)
03-29 23:46:17.584: E/AndroidRuntime(474):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-29 23:46:17.584: E/AndroidRuntime(474):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-29 23:46:17.584: E/AndroidRuntime(474):  ... 11 more
4

1 回答 1

0

mTabHostnull

显然,这是因为res/layout/activity_vendor.xml不包含FragmentTabHostnamed @android:id/tabhost,这是您的findViewById()呼叫所要求的。

于 2013-03-29T18:31:55.213 回答