2012-05-25 183 views
1

我使用下面的代码就先添加上tabHost标签,但崩溃到最后一行,spec.setContent(意向)到tabhost。调试器说没有太多。谢谢。Android的附加选项卡

从Registrat,

Intent intentIOS = new Intent(Registrat.this, TabBar_Activity.class); 
Bundle bundle = new Bundle(); 
bundle.putString("idusuari", idusuari); 
String appid = null; 

从TabBar_Activity,

private void setTabs(String numTabs){ 

     addTab(tab1name, "tab1", myTab1.class); 
     addTab(tab2name, "tab2", myTab2.class); 
} 

private void addTab(String labelId, String imageName, Class<?> c) 
    { 
     TabHost tabHost = getTabHost(); 
      Intent intent = new Intent(this, c); 
      TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId); 

      View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false); 
      TextView title = (TextView) tabIndicator.findViewById(R.id.title); 
      title.setText(labelId);  

      spec.setIndicator(tabIndicator); 
      spec.setContent(intent); 
      tabHost.addTab(spec); 
    } 

05-25 11:18:49.725: E/AndroidRuntime(428): FATAL EXCEPTION: main 
05-25 11:18:49.725: E/AndroidRuntime(428): java.lang.IllegalStateException: Could not execute method of the activity 
05-25 11:18:49.725: E/AndroidRuntime(428): at android.view.View$1.onClick(View.java:2072) 
05-25 11:18:49.725: E/AndroidRuntime(428): at android.view.View.performClick(View.java:2408) 
05-25 11:18:49.725: E/AndroidRuntime(428): at android.view.View$PerformClick.run(View.java:8816) 
05-25 11:18:49.725: E/AndroidRuntime(428): at android.os.Handler.handleCallback(Handler.java:587) 
05-25 11:18:49.725: E/AndroidRuntime(428): at android.os.Handler.dispatchMessage(Handler.java:92) 
05-25 11:18:49.725: E/AndroidRuntime(428): at android.os.Looper.loop(Looper.java:123) 
05-25 11:18:49.725: E/AndroidRuntime(428): at android.app.ActivityThread.main(ActivityThread.java:4627) 
05-25 11:18:49.725: E/AndroidRuntime(428): at java.lang.reflect.Method.invokeNative(Native Method) 
05-25 11:18:49.725: E/AndroidRuntime(428): at java.lang.reflect.Method.invoke(Method.java:521) 
05-25 11:18:49.725: E/AndroidRuntime(428): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 

,并遵循...

+0

发表您的logcat .... –

+0

贴,如果满人会帮助请询问 – Jaume

+0

问题似乎来,因为我有主活动中tabhost和我打电话包含其他tabHost另一个活动。独自站在工作,可能问题来自getTabHost,都有相同的ID。但是,如果我尝试更改其ID,不接受不同于“tabhost” – Jaume

回答

1

要设置选项卡中,我使用的代码之后,它工作正常。

TabHost tabHost = getTabHost(); 

// Tab for Photos 
TabSpec photospec = tabHost.newTabSpec("Photos"); 
// setting Title and Icon for the Tab 
photospec.setIndicator("Photos", getResources().getDrawable(R.drawable.icon_photos_tab)); 
Intent photosIntent = new Intent(this, PhotosActivity.class); 
photospec.setContent(photosIntent); 

// Tab for Songs 
TabSpec songspec = tabHost.newTabSpec("Songs"); 
songspec.setIndicator("Songs", getResources().getDrawable(R.drawable.icon_songs_tab)); 
Intent songsIntent = new Intent(this, SongsActivity.class); 
songspec.setContent(songsIntent); 
// Adding all TabSpec to TabHost 
tabHost.addTab(photospec); // Adding photos tab 
tabHost.addTab(songspec); // Adding songs tab 
+0

在您的代码中,检查您在此处使用的c的值:Intent intent = new Intent(this,c); spec.setContent(意向); – AndroidLearner

+0

c正确填充。问题似乎是由于两个tabhosts。两个工作站在一起,但getTabHost时,tabHosts具有相同的ID,但我无法将任何人设置为“tabhost” – Jaume

相关问题