android中的选项卡活动

android中的选项卡活动,android,tabview,Android,Tabview,您好,我是这个“选项卡活动”概念的新手。我搜索了这个,最后我知道我应该实现“片段”。我完成了以下代码,但显示了错误。请指导我解决这个问题 public class VendorActivity extends FragmentActivity { private FragmentTabHost mTabHost; @Override protected void onCreate(Bundle savedInstanceState) { su

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

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>

mTabHost
null


显然,这是因为
res/layout/activity\u vendor.xml
不包含名为
@android:id/tabhost
FragmentTabHost
,这正是您的
findViewById()
调用所要求的。

首先尝试了解片段请指出
vendoravity
的第50行,我们无从得知。谢谢@Pragnani,我一定会阅读教程。你能说我做错了什么吗?@commonware在我的帖子中,我指定了错误,请看这里。android@Subburaj:您可以尝试清理项目(在Eclipse中,project>Clean;在命令行中,
ant Clean
),看看这是否有帮助。否则,XML、Java代码和堆栈跟踪之间会出现不同步。
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