gpt4 book ai didi

java - 使用自定义 Activity 时 TabHost 崩溃

转载 作者:行者123 更新时间:2023-11-30 10:41:06 25 4
gpt4 key购买 nike

所以我通过拖放制作了一个 TabHot,然后将其编码到选项卡中以使其工作并且它工作了。但是我不得不为不方便的内容使用制表符线性布局。所以我决定为每个选项卡制作一个单独的 Activity 。现在,当我将设置内容更改为类的 Intent 时,我的项目崩溃了。我用谷歌搜索了这个并寻找修复方法,但一无所获。

这是我的代码:

public class MainActivity extends AppCompatActivity {

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

private void InitTabHost(){

//Setup Initial Tab Host
TabHost tbHost = (TabHost)findViewById(R.id.tabHost);
tbHost.setup();

//Setup Tab 1

TabHost.TabSpec NFtab = tbHost.newTabSpec("one");
NFtab.setIndicator("News Feed");
Intent intent = new Intent(this,NewsFeed.class);
NFtab.setContent(intent);
tbHost.addTab(NFtab);

TabHost.TabSpec AGTab = tbHost.newTabSpec("two");
AGTab.setIndicator("Art Gallery");
AGTab.setContent(R.id.tab2);
tbHost.addTab(AGTab);

TabHost.TabSpec CreateTab = tbHost.newTabSpec("three");
CreateTab.setIndicator("Create");
CreateTab.setContent(R.id.tab3);
tbHost.addTab(CreateTab);

TabHost.TabSpec ProfileTab = tbHost.newTabSpec("Four");
ProfileTab.setIndicator("Profile");
ProfileTab.setContent(R.id.tab4);
tbHost.addTab(ProfileTab);
}}

这是我使用 tabhost 的主要布局:

<?xml version="1.0" encoding="utf-8"?>

<TabHost
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/tabHost">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TabWidget
android:id="@android:id/tabs"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>

<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

</LinearLayout>

<LinearLayout
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

</LinearLayout>

<LinearLayout
android:id="@+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

</LinearLayout>

<LinearLayout
android:id="@+id/tab4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

</LinearLayout>
</FrameLayout>
</RelativeLayout>
</TabHost>

这是另一个 Activity 类:

public class NewsFeed extends TabActivity {

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

编辑 Logcat:

07/28 01:56:29: Launching app
$ adb shell am start -n "com.example.akashbakshi.artnsketch/com.example.akashbakshi.artnsketch.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Connected to process 3940 on device Nexus_6P_API_23 [emulator-5554]
I/art: Not late-enabling -Xcheck:jni (already on)
W/System: ClassLoader referenced unknown path: /data/app/com.example.akashbakshi.artnsketch-2/lib/x86
W/System: ClassLoader referenced unknown path: /data/app/com.example.akashbakshi.artnsketch-2/lib/x86
W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.akashbakshi.artnsketch, PID: 3940
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.akashbakshi.artnsketch/com.example.akashbakshi.artnsketch.MainActivity}: java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?
at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:722)
at android.widget.TabHost.setCurrentTab(TabHost.java:388)
at android.widget.TabHost.addTab(TabHost.java:222)
at com.example.akashbakshi.artnsketch.MainActivity.InitTabHost(MainActivity.java:30)
at com.example.akashbakshi.artnsketch.MainActivity.onCreate(MainActivity.java:15)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

最佳答案

点击此链接:Your content must have a TabHost whose id attribute is 'android.R.id.tabhost'

我能够通过将我的 tabhost 更改为 android:id/tabhost 并将我的 NewsFeed 更改为 Activity 来解决问题。

关于java - 使用自定义 Activity 时 TabHost 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38628005/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com