[英]Android - Add activity to a TabHost?
我有一個包含ListView和TabHost的Activity。 在此TabHost內,我試圖添加另一個Activity,但它一直崩潰。 知道是什么原因造成的嗎?
public class MainActivity extends RoboFragmentActivity {
@InjectView(R.id.listView)
private ListView listView;
@InjectView(R.id.tabHost)
private TabHost tabHost;
@Inject
IIntentProxy intentProxy;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
tabHost.setup();
TabHost.TabSpec spec = tabHost.newTabSpec("tag");
spec.setContent(intentProxy.getIntent(DetailActivity.class));
spec.setIndicator("Title");
// It crashes on line below
// tabHost is not null, neither is spec
tabHost.addTab(spec);
}
}
。
<LinearLayout 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:orientation="horizontal"
android:weightSum="100" >
<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabHost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="50" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="5dp" >
<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"
android:padding="5dp" >
</FrameLayout>
</TabWidget>
</LinearLayout>
</TabHost>
<ListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="50" >
</ListView>
</LinearLayout>
將詳細信息活動添加到tabhost xml文件不包含任何內容,僅包含LinearLayout
public class DetailActivity extends RoboActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.detail_activity);
}
}
堆棧跟蹤
12-24 18:06:03.535: E/AndroidRuntime(27143): FATAL EXCEPTION: main
12-24 18:06:03.535: E/AndroidRuntime(27143): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.company.app/com.company.app.pages.MainActivity}: java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?
12-24 18:06:03.535: E/AndroidRuntime(27143): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
12-24 18:06:03.535: E/AndroidRuntime(27143): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-24 18:06:03.535: E/AndroidRuntime(27143): at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-24 18:06:03.535: E/AndroidRuntime(27143): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-24 18:06:03.535: E/AndroidRuntime(27143): at android.os.Handler.dispatchMessage(Handler.java:99)
12-24 18:06:03.535: E/AndroidRuntime(27143): at android.os.Looper.loop(Looper.java:137)
12-24 18:06:03.535: E/AndroidRuntime(27143): at android.app.ActivityThread.main(ActivityThread.java:5039)
12-24 18:06:03.535: E/AndroidRuntime(27143): at java.lang.reflect.Method.invokeNative(Native Method)
12-24 18:06:03.535: E/AndroidRuntime(27143): at java.lang.reflect.Method.invoke(Method.java:511)
12-24 18:06:03.535: E/AndroidRuntime(27143): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-24 18:06:03.535: E/AndroidRuntime(27143): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-24 18:06:03.535: E/AndroidRuntime(27143): at dalvik.system.NativeStart.main(Native Method)
12-24 18:06:03.535: E/AndroidRuntime(27143): Caused by: java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?
12-24 18:06:03.535: E/AndroidRuntime(27143): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:747)
12-24 18:06:03.535: E/AndroidRuntime(27143): at android.widget.TabHost.setCurrentTab(TabHost.java:413)
12-24 18:06:03.535: E/AndroidRuntime(27143): at android.widget.TabHost.addTab(TabHost.java:240)
12-24 18:06:03.535: E/AndroidRuntime(27143): at com.pages.ChargeSheetActivity.onCreate(ChargeSheetActivity.java:33)
12-24 18:06:03.535: E/AndroidRuntime(27143): at android.app.Activity.performCreate(Activity.java:5104)
12-24 18:06:03.535: E/AndroidRuntime(27143): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
12-24 18:06:03.535: E/AndroidRuntime(27143): at android.app.ActivityThread.performLaunchActivity(ActivityThr
ead.java:2144)12-24 18:06:03.535:E / AndroidRuntime(27143):...還有11個
你不能用
擴展活動
您必須使用
擴展ActivityGroup
該鏈接對此做了一些解釋:
Android異常:您是否忘記了調用“公共無效設置(LocalActivityManager activityGroup)”
1-我擴展了RoboTabActivity
而不是RoboFragmentActivity
2-將我的xml從android:id="@+id/tabHost"
更改為android:id="@android:id/tabhost"
3-我將活動添加到清單文件
4-不是注入Tabhost或使用findById,而是使用this.getTabHost this.getTabHost()
現在剩下的唯一問題是,如果我需要使用DialogFragment,該怎么辦? 通常需要Activity擴展RoboFragmentActivity
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.