Androidアプリでタブを作ってページの切り替えを行いたいです。
タブはフラグメントで作っていて、それをページの下に置いています。
ボタンを押されると、フラグメントのjavaで受け取って処理してページの切り替えをしたいと考えています
ですが、1回目は押したページに飛んでくれるのですが、2回目を押すとアプリが終了してしまいます
ページの構成です。同じようなものを4つ作りました。
lang
1<?xml version="1.0" encoding="utf-8"?> 2<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:layout_gravity="center_vertical" 7 android:background="#ffeac132"> 8 9 <FrameLayout //ページの内容 10 android:layout_above="@+id/tab" 11 android:layout_width="match_parent" 12 android:layout_height="match_parent" 13 android:layout_alignParentTop="true" 14 android:layout_alignParentStart="true"> 15 16 <TextView 17 android:text="1" 18 android:textColor="#ff5f401a" 19 android:layout_width="wrap_content" 20 android:layout_height="wrap_content" 21 android:layout_gravity="center" /> 22 23 </FrameLayout> 24 25 <fragment android:name="com.XXXX.test.Tab" //タブのフラグメント 26 android:id="@+id/tab" 27 android:layout_width="match_parent" 28 android:layout_height="wrap_content" 29 android:layout_alignParentBottom="true" /> 30 31 32</RelativeLayout>
タブのフラグメントです。
lang
1<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 3 android:layout_height="wrap_content" tools:context="com.XXXX.test.Tab" 4 android:background="#f0616161"> 5 6 <Button 7 android:id="@+id/page1_buttom" 8 android:text="1" 9 style="@style/Tab_Button_Style"/> 10 11 <Button 12 android:id="@+id/page2_buttom" 13 android:text="2" 14 style="@style/Tab_Button_Style" 15 /> 16 17 <Button 18 android:id="@+id/page3_buttom" 19 android:text="3" 20 style="@style/Tab_Button_Style" /> 21 22 <Button 23 android:id="@+id/page4_buttom" 24 android:text="4" 25 style="@style/Tab_Button_Style" /> 26 27</LinearLayout>
タブのjavaです。
lang
1public class Tab extends Fragment implements View.OnClickListener { 2 3 @Override 4 public View onCreateView( 5 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 6 return inflater.inflate(R.layout.fragment_tab, container, false); 7 } 8 9 @Override 10 public void onStart() { 11 super.onStart(); 12 13 14 Button bu1 = (Button) getActivity().findViewById(R.id.page1_buttom); 15 Button bu2 = (Button) getActivity().findViewById(R.id.page2_buttom); 16 Button bu3 = (Button) getActivity().findViewById(R.id.page3_buttom); 17 Button bu4 = (Button) getActivity().findViewById(R.id.page4_buttom); 18 bu1.setOnClickListener(this); 19 bu2.setOnClickListener(this); 20 bu3.setOnClickListener(this); 21 bu4.setOnClickListener(this); 22 } 23 24 @Override 25 public void onClick(View view){ 26 System.out.println("comeon"); 27 FragmentManager fragmentManager = getFragmentManager(); 28 FragmentTransaction fragmentTransaction 29 = fragmentManager.beginTransaction(); 30 switch (view.getId()) { 31 case R.id.page1_buttom: 32 Page1 page1 = new Page1(); 33 fragmentTransaction.replace(android.R.id.content, page1); 34 break; 35 36 case R.id.page2_buttom: 37 Page2 page2 = new Page2(); 38 fragmentTransaction.replace(android.R.id.content, page2); 39 break; 40 41 case R.id.page3_buttom: 42 Page3 page3 = new Page3(); 43 fragmentTransaction.replace(android.R.id.content, page3); 44 break; 45 46 case R.id.page4_buttom: 47 Page4 page4 = new Page4(); 48 fragmentTransaction.replace(android.R.id.content, page4); 49 break; 50 } 51 fragmentTransaction.commit(); 52 } 53}
MainActivityの方でonClickを受けると、何回でもページの切り替えが行えるので今はそっちで処理していますが、それぞれのフラグメントにそれぞれの処理を任せたいのでご教授おねがいします。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/06/29 22:23 編集