Android中另一个标签内的标签(使用片段)

时间:2012-05-08 08:38:42

标签: java android android-fragments android-tabs

在我的应用程序(android 2.1)中,我已成功使用Fragments添加了标签,并使用了Android兼容性库v4。

现在我想在其中一个片段选项卡中添加另一个选项卡,但是tabwidget会抛出NullPointerException,我在这里做错了什么?

logcat的:

05-08 12:58:57.060: W/dalvikvm(1371): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
05-08 12:58:57.060: E/AndroidRuntime(1371): Uncaught handler: thread main exiting due to uncaught exception
05-08 12:58:57.100: E/AndroidRuntime(1371): java.lang.NullPointerException
05-08 12:58:57.100: E/AndroidRuntime(1371):     at android.widget.TabWidget.dispatchDraw(TabWidget.java:206)
05-08 12:58:57.100: E/AndroidRuntime(1371):     at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
05-08 12:58:57.100: E/AndroidRuntime(1371):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
05-08 12:58:57.100: E/AndroidRuntime(1371):     at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
05-08 12:58:57.100: E/AndroidRuntime(1371):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
05-08 12:58:57.100: E/AndroidRuntime(1371):     at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
05-08 12:58:57.100: E/AndroidRuntime(1371):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
05-08 12:58:57.100: E/AndroidRuntime(1371):     at android.view.View.draw(View.java:6538)
05-08 12:58:57.100: E/AndroidRuntime(1371):     at android.view.ViewGroup.drawChild(ViewGroup.java:1531)
05-08 12:58:57.100: E/AndroidRuntime(1371):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
05-08 12:58:57.100: E/AndroidRuntime(1371):     at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
05-08 12:58:57.100: E/AndroidRuntime(1371):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
05-08 12:58:57.100: E/AndroidRuntime(1371):     at android.view.View.draw(View.java:6538)
05-08 12:58:57.100: E/AndroidRuntime(1371):     at android.support.v4.view.ViewPager.draw(ViewPager.java:1574)
05-08 12:58:57.100: E/AndroidRuntime(1371):     at android.view.ViewGroup.drawChild(ViewGroup.java:1531)
05-08 12:58:57.100: E/AndroidRuntime(1371):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
05-08 12:58:57.100: E/AndroidRuntime(1371):     at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
05-08 12:58:57.100: E/AndroidRuntime(1371):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
05-08 12:58:57.100: E/AndroidRuntime(1371):     at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
05-08 12:58:57.100: E/AndroidRuntime(1371):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
05-08 12:58:57.100: E/AndroidRuntime(1371):     at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
05-08 12:58:57.100: E/AndroidRuntime(1371):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
05-08 12:58:57.100: E/AndroidRuntime(1371):     at android.view.View.draw(View.java:6538)
05-08 12:58:57.100: E/AndroidRuntime(1371):     at android.widget.FrameLayout.draw(FrameLayout.java:352)
05-08 12:58:57.100: E/AndroidRuntime(1371):     at android.view.ViewGroup.drawChild(ViewGroup.java:1531)
05-08 12:58:57.100: E/AndroidRuntime(1371):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
05-08 12:58:57.100: E/AndroidRuntime(1371):     at android.view.View.draw(View.java:6538)
05-08 12:58:57.100: E/AndroidRuntime(1371):     at android.widget.FrameLayout.draw(FrameLayout.java:352)
05-08 12:58:57.100: E/AndroidRuntime(1371):     at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1830)
05-08 12:58:57.100: E/AndroidRuntime(1371):     at android.view.ViewRoot.draw(ViewRoot.java:1349)
05-08 12:58:57.100: E/AndroidRuntime(1371):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1114)
05-08 12:58:57.100: E/AndroidRuntime(1371):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1633)
05-08 12:58:57.100: E/AndroidRuntime(1371):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-08 12:58:57.100: E/AndroidRuntime(1371):     at android.os.Looper.loop(Looper.java:123)
05-08 12:58:57.100: E/AndroidRuntime(1371):     at android.app.ActivityThread.main(ActivityThread.java:4363)
05-08 12:58:57.100: E/AndroidRuntime(1371):     at java.lang.reflect.Method.invokeNative(Native Method)
05-08 12:58:57.100: E/AndroidRuntime(1371):     at java.lang.reflect.Method.invoke(Method.java:521)
05-08 12:58:57.100: E/AndroidRuntime(1371):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
05-08 12:58:57.100: E/AndroidRuntime(1371):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
05-08 12:58:57.100: E/AndroidRuntime(1371):     at dalvik.system.NativeStart.main(Native Method)

//子标签片段

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

public class Tab3Fragment extends Fragment
{

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    if (container == null) {
        // We have different layouts, and in one of them this
        // fragment's containing frame doesn't exist.  The fragment
        // may still be created from its saved state, but there is
        // no reason to try to create its view hierarchy because it
        // won't be displayed.  Note this is not needed -- we could
        // just run the code below, where we would create and return
        // the view hierarchy; it would just never be used.
        return null;
    }
    return (LinearLayout)inflater.inflate(R.layout.subtab, container, false);
}

}

// subtab.xml

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

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#4C4C4C" >
<TabHost 
android:id="@android:id/tabhost" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"
android:padding="2dp"> 

<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    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" >

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

0 个答案:

没有答案