2014-12-09 3 views
0

2 조각 FragmentA 및 FragmentB가 있습니다. 단편 A가 단편 B 위에 있습니다. 이제 FragmentB에 탭을 추가하고 싶습니다. 도와주세요. 지금까지 내 코드 : -android의 탭이있는 조각

레이아웃/fragmentA.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Top Fragment" /> 

</LinearLayout> 

레이아웃/fragmentB.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#F5F6CE" 
    android:orientation="vertical" > 

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

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

       <include 
        android:id="@+id/tabdata1" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        layout=“@layout/tabdata1_view"/> 

       <include 
        android:id="@+id/tabdata2" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        layout=“@layout/tabdata2_view" 
        /> 
      </FrameLayout> 
     </LinearLayout> 
    </TabHost> 

</LinearLayout> 

FragmentB.java

public class Fragment2 extends Fragment{ 

    TabHost myTabHost; 

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

     View rootView = inflater.inflate(R.layout.fragment2_layout, container, false); 

     myTabHost = (TabHost) getView().findViewById(R.id.tabhost) 

     return rootView; 

    } 

} 

오류 : -

"myTabHost = (TabHost) getView().findViewById(R.id.tabhost)"줄은 tabhost를 찾을 수 없으므로 오류를 표시합니다.

이 방법을 제안하십시오.

답변

0

는이

myTabHost = (TabHost) rootview.findViewById(android.R.id.tabhost); 

이것은 당신이 작동하지 않습니다 아직 지금

0

왜냐하면 onCreateView에서 돌아 오기 전까지 조각에 아직 실제로보기가 없기 때문입니다. 단지 rootView를 기준으로 findViewById를 호출하면됩니다. 이처럼 :

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

    View rootView = inflater.inflate(R.layout.fragment2_layout, container, false); 

    myTabHost = (TabHost) rootView.findViewById(R.id.tabhost) 

    return rootView; 

} 
+0

에 직면하고 오류를 제거해야합니다 시도 :( –

관련 문제