Issue with displaying TabHost Layout in new Intent

2020-04-11 18:14发布

I am having an issue with using TabHost in a new Intent of type TabActivity which I hope you can point me in the right direction. Funnily it works fine when I try to view it in the original Intent : setContentView(R.layout.main)

I get a "forced closed" and within logcat, I get the following error even though my Tabhost id = "@android:id/tabhost":

02-18 22:23:11.937: ERROR/AndroidRuntime(5944): Caused by: java.lang.RuntimeException: Your content must have a TabHost whose id attribute is 'android.R.id.tabhost'

I have declared the second intent in the Manifest.xml file: XML:

<activity android:name=".NextActivity" android:label="@string/app_name" > 

Within the first activity (MainActivity), I start the second intent (NextActivity), with extras, as follows:

Intent nextActivity = new Intent(MainActivity.this,NextActivity.class); 
Bundle b_next=new Bundle(); 

b_next.putString("s_string", myString); 

nextActivity.putExtras(b_next); 

In my NextActivity.java file, I get the extras and try to display the TabHost View:

public class NextActivity extends TabActivity { 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 

        String myString; 
        Bundle b_initial; 

        b_initial = getIntent().getExtras(); 

        myString = b_initial.getString("s_string"); 

     setContentView(R.layout.main); 

        } 
}

I get the same error with using the TabHost example on the Android Developer site (Hellow View):

Main.xml:

<?xml version="1.0" encoding="utf-8"?> 
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/tabhost" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_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"> 
            <TextView 
                android:id="@+id/textview1" 
                android:layout_width="fill_parent" 
                android:layout_height="fill_parent" 
                android:text="this is a tab" /> 
            <TextView 
                android:id="@+id/textview2" 
                android:layout_width="fill_parent" 
                android:layout_height="fill_parent" 
                android:text="this is another tab" /> 
            <TextView 
                android:id="@+id/textview3" 
                android:layout_width="fill_parent" 
                android:layout_height="fill_parent" 
                android:text="this is a third tab" /> 
        </FrameLayout> 
    </LinearLayout> 
</TabHost>

Thanks in advance folks...

CLARIFICATION: This is what I really get from LogCat:

java.lang.NullPointerException
at android.widget.TabHost.dispatchWindowFocusChanged(TabHost.java 285) at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java 640) at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java 640) at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java 640) at android.view.ViewRoot.handleMessage(ViewRoot.java 1645) at android.os.Handler.dispatchMessage(Handler.java 99) at android.os.Looper.loop(Looper.java 123) at android.app.ActivityThread.main(ActivityThread.java 3948) at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java 521) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java 782) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java 540) at dalvik.system.NativeStart.main(Native Method)

9条回答
做个烂人
2楼-- · 2020-04-11 18:26

I had the same problem. Luckily there was a quick fix:

  1. Delete the R file.
  2. Wait for your IDE to generate the file.
  3. Profit!
查看更多
Ridiculous、
3楼-- · 2020-04-11 18:27

I have previously constructed tabhosts with an id of android:id="@+id/tabhost". Does this work for you?

You could also consider constructing your tab view programmatically:

    TabHost t = getTabHost();
    TabSpec tab = t.newTabSpec(label)
                   .setIndicator(label, icon)
                   .setContent(intent);
    t.addTab(tab);
查看更多
Melony?
4楼-- · 2020-04-11 18:27

I also had this error. For me it would appear after obfuscation(via ProGuard), pre obfuscation it was fine.

In the end I renamed my layout file to something more complex and unique, and ProGuard didn't mess up the linking. Bit easer than going down the ProGuard configeration path.

So my issue was similar to SapphireSun, though expressed via a different process(and post eclipse, just to make things that bit more difficult).

This is perhaps one of the more obscure ways to get this error, but I thought it worth a mention.

查看更多
欢心
5楼-- · 2020-04-11 18:35

I had this issue along with 'ERROR: Unknown option '--no-crunch''. If you updated ADT plug-in in Eclipse but have not updated the SDK at the same time. Updating the SDK Manager in Eclipse, fixed the issue for me.

查看更多
霸刀☆藐视天下
6楼-- · 2020-04-11 18:37

I had copied and pasted code and noticed that in the id attribute I had:

android:id="@+id/tabs" 

when what I really wanted was:

android:id="@android:id/tabs"
查看更多
闹够了就滚
7楼-- · 2020-04-11 18:37

even though i had changed my main.xml to use android:id="@android:id/tabhost" like the error stated, it kept trying to use the old name. finally i found the file res/layout-port/main.xml that was identical to main.xml except used the wrong android:id. i must have added a portrait view accidentally and it was holding on to the old value. either fixing the portrait file or removing it fixes my problem.

查看更多
登录 后发表回答