在Android中向Tabspec添加标签时出现崩溃

时间:2012-06-16 04:07:33

标签: android layout android-fragments

我想创建标签并向其添加片段,但是在将Tabspec添加到标签时我遇到了崩溃。这是来源,

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


  TabHost.TabSpec spec;  // Resusable TabSpec for each tab
  Intent intent;  // Reusable Intent for each tab


  intent = new Intent().setClass(this, DummyFrag.class);

  spec = mTabHost.newTabSpec("artists").setIndicator("Numbers").setContent(intent);

  FragmentManager fm       = getSupportFragmentManager();
  Fragment        fragment = fm.findFragmentById(android.R.id.tabcontent);

  if (fragment == null) 
  {
    FragmentTransaction ft = fm.beginTransaction(); 
    ft.add(android.R.id.tabcontent, new DummyFrag(),"DummyFrag");
   //   m_mainactivity.getSupportActionBar().selectTab(curTab);
    ft.commit();
  }

  mTabHost.addTab(spec);


}





public static class DummyFrag extends Fragment {

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    {
        View view = inflater.inflate(R.layout.dummy, container, false);   
        return view;    
    }

}

请提前告知我确切问题的确切位置

记录猫的详细信息

    06-16 11:11:15.797: E/AndroidRuntime(461): java.lang.RuntimeException: Unable to start activity ComponentInfo{simple.preview.screen/simple.preview.screen.SimplePreviewActivity}: java.lang.NullPointerException
06-16 11:11:15.797: E/AndroidRuntime(461):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
06-16 11:11:15.797: E/AndroidRuntime(461):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
06-16 11:11:15.797: E/AndroidRuntime(461):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
06-16 11:11:15.797: E/AndroidRuntime(461):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
06-16 11:11:15.797: E/AndroidRuntime(461):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-16 11:11:15.797: E/AndroidRuntime(461):  at android.os.Looper.loop(Looper.java:123)
06-16 11:11:15.797: E/AndroidRuntime(461):  at android.app.ActivityThread.main(ActivityThread.java:4627)
06-16 11:11:15.797: E/AndroidRuntime(461):  at java.lang.reflect.Method.invokeNative(Native Method)
06-16 11:11:15.797: E/AndroidRuntime(461):  at java.lang.reflect.Method.invoke(Method.java:521)
06-16 11:11:15.797: E/AndroidRuntime(461):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-16 11:11:15.797: E/AndroidRuntime(461):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-16 11:11:15.797: E/AndroidRuntime(461):  at dalvik.system.NativeStart.main(Native Method)
06-16 11:11:15.797: E/AndroidRuntime(461): Caused by: java.lang.NullPointerException
06-16 11:11:15.797: E/AndroidRuntime(461):  at android.widget.TabHost.addTab(TabHost.java:209)
06-16 11:11:15.797: E/AndroidRuntime(461):  at simple.preview.screen.SimplePreviewActivity.onCreate(SimplePreviewActivity.java:50)
06-16 11:11:15.797: E/AndroidRuntime(461):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-16 11:11:15.797: E/AndroidRuntime(461):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)

1 个答案:

答案 0 :(得分:0)

问题是,在包含tabhost的xml文件中,必须将其从@android:id / tabhost更改为@ + id / tabhost。