2014-12-09 1 views
0

У меня есть 2 фрагмента FragmentA и FragmentB. FragmentA находится поверх FragmentB. Теперь я хочу добавить вкладки в 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, фрагмент на самом деле еще не имеет вида. Вам просто нужно вызвать findViewById относительно rootView. Как это:

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

все еще не работает :( –