2013-03-20 143 views
0

我在Android中使用「TabHost」小部件來掙扎我的第一個應用程序。用標籤掙扎

我定義我的XML佈局:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:tools="http://schemas.android.com/tools" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_marginRight="5dp" 
    android:orientation="horizontal" 
    tools:context=".fragments.ConditionsDialogFragment" > 

<TextView 
    android:id="@+id/name_banner" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/rounded_corner_button" 
    android:text="ddddd" 
    android:textAppearance="?android:attr/textAppearanceMedium" 
    android:textColor="#ffffff" /> 

<TextView 
    android:id="@+id/name_lbl0" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/name_banner" 
    android:layout_marginLeft="10dp" 
    android:layout_marginTop="10dp" 
    android:text="Display Prompts" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 

<View 
    android:id="@+id/firstDivider" 
    android:layout_width="fill_parent" 
    android:layout_height="2dp" 
    android:layout_below="@id/name_lbl0" 
    android:layout_marginBottom="3dp" 
    android:layout_marginLeft="5dp" 
    android:layout_marginRight="5dp" 
    android:layout_marginTop="3dp" 
    android:background="#BFBDBE" /> 

<TextView 
    android:id="@+id/name_lbl1" 
    android:layout_width="140dp" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/firstDivider" 
    android:layout_marginLeft="10dp" 
    android:layout_marginTop="3dp" 
    android:gravity="right" 
    android:text="Order Placer :" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 

<TextView 
    android:id="@+id/cond_c01" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/firstDivider" 
    android:layout_marginLeft="10dp" 
    android:layout_marginTop="3dp" 
    android:layout_toRightOf="@+id/name_lbl1" 
    android:text="XXXX" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 

<TextView 
    android:id="@+id/name_lbl2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignRight="@+id/name_lbl1" 
    android:layout_below="@+id/name_lbl1" 
    android:text="Specimen Taker :" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 

<TextView 
    android:id="@+id/cond_c02" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/name_lbl1" 
    android:layout_marginLeft="10dp" 
    android:layout_toRightOf="@+id/name_lbl2" 
    android:text="XXXX" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 

<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentRight="true" 
    android:layout_marginBottom="15dp" 
    android:layout_marginRight="10dp" 
    android:text="OK" /> 

<TabHost 
    android:id="@android:id/tabhost" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_above="@+id/button1" 
    android:layout_alignLeft="@+id/name_lbl1" 
    android:layout_below="@+id/cond_c02" 
    android:layout_marginTop="6dp" > 

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

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

     <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" > 

      </LinearLayout> 

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

      <LinearLayout 
       android:id="@+id/tab3" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" > 
      </LinearLayout> 
     </FrameLayout> 
    </LinearLayout> 
</TabHost> 

</RelativeLayout> 

佈局使用DialogFragment內用下面的代碼:

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 

    View thisView = inflater.inflate(R.layout.nlmc_conditions, container, false); 

    TextView bannerName = (TextView)thisView.findViewById(R.id.name_banner); 
    TextView c01 = (TextView)thisView.findViewById(R.id.cond_c01); 
    TextView c02 = (TextView)thisView.findViewById(R.id.cond_c02); 

    c01.setText(cursor.getString(cursor.getColumnIndex("CHOICEORDERDISPLAY"))); 
    c02.setText(cursor.getString(cursor.getColumnIndex("CHOICETAKERDISPLAY"))); 

    TabHost tabHost = (TabHost)thisView.findViewById(android.R.id.tabhost); 

    TabSpec ts = tabHost.newTabSpec("tag1"); 
     ts.setContent(R.id.tab1); 
     ts.setIndicator("Tab one"); 
     tabHost.addTab(ts); 


    bannerName.setText(conditions.Specimen + " specimen preconditions"); 

    return thisView; 
} 

當它運行時的屏幕不顯示和應用程序失敗,日誌在這裏:

 03-20 13:35:48.129: E/AndroidRuntime(17149): FATAL EXCEPTION: main 
     03-20 13:35:48.129: E/AndroidRuntime(17149): java.lang.NullPointerException 
     03-20 13:35:48.129: E/AndroidRuntime(17149): at android.widget.TabHost$ViewIdContentStrategy.<init>(TabHost.java:629) 
     03-20 13:35:48.129: E/AndroidRuntime(17149): at android.widget.TabHost$ViewIdContentStrategy.<init>(TabHost.java:624) 
     03-20 13:35:48.129: E/AndroidRuntime(17149): at android.widget.TabHost$TabSpec.setContent(TabHost.java:473) 
     03-20 13:35:48.129: E/AndroidRuntime(17149): at com.apps4care.nlmcbrowser.fragments.ConditionsDialogFragment.onCreateView(ConditionsDialogFragment.java:69) 
     03-20 13:35:48.129: E/AndroidRuntime(17149): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460) 
     03-20 13:35:48.129: E/AndroidRuntime(17149): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911) 
     03-20 13:35:48.129: E/AndroidRuntime(17149): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088) 
     03-20 13:35:48.129: E/AndroidRuntime(17149): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682) 
     03-20 13:35:48.129: E/AndroidRuntime(17149): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444) 
     03-20 13:35:48.129: E/AndroidRuntime(17149): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429) 
     03-20 13:35:48.129: E/AndroidRuntime(17149): at android.os.Handler.handleCallback(Handler.java:615) 
     03-20 13:35:48.129: E/AndroidRuntime(17149): at android.os.Handler.dispatchMessage(Handler.java:92) 
     03-20 13:35:48.129: E/AndroidRuntime(17149): at android.os.Looper.loop(Looper.java:137) 
     03-20 13:35:48.129: E/AndroidRuntime(17149): at android.app.ActivityThread.main(ActivityThread.java:4745) 
     03-20 13:35:48.129: E/AndroidRuntime(17149): at java.lang.reflect.Method.invokeNative(Native Method) 
     03-20 13:35:48.129: E/AndroidRuntime(17149): at java.lang.reflect.Method.invoke(Method.java:511) 
     03-20 13:35:48.129: E/AndroidRuntime(17149): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
     03-20 13:35:48.129: E/AndroidRuntime(17149): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
     03-20 13:35:48.129: E/AndroidRuntime(17149): at dalvik.system.NativeStart.main(Native Method) 
     03-20 13:40:48.179: I/Process(17149): Sending signal. PID: 17149 SIG: 9 

任何幫助讚賞

回答

1

爲了其他的好處,我找到了問題。

在我使用TabHost類之前,我忘了添加以下內容。

tabHost.setup();