我有一些麻煩理解和使用片段我有3個按鈕和1個片段視圖使用按鈕作爲選項卡我試圖給片段一個特定的類,當點擊一個按鈕什麼我的確是這樣的:使用一個片段視圖替換多個片段類
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/linearLayout1"
android:layout_alignTop="@+id/linearLayout1" >
<Button
android:id="@+id/Tab1"
style="?android:attr/buttonStyleSmall"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="Tab 1" android:background="@drawable/btn"/>
<Button
android:id="@+id/Tab2"
style="?android:attr/buttonStyleSmall"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="Tab 2" android:onClick="switchToTab2" />
<Button
android:id="@+id/Tab3"
style="?android:attr/buttonStyleSmall"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="Tab 3" android:onClick="switchToTab3" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/linearLayout1"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/linearLayout1"
android:layout_below="@+id/linearLayout1" >
<fragment
android:id="@+id/fragment1"
android:name="com.example.newproject.fragmentclass"
android:layout_width="match_parent"
android:layout_height="394dp" />
</LinearLayout>
java類:
fragmentclass FRT1;
fragmentclass2 frt2;
fragment3class frt3;
FragmentTransaction ft;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment_tabs_exp);
ft = getSupportFragmentManager().beginTransaction();
frt1 = new fragmentclass();
frt2 = new fragmentclass2();
frt3 = new fragment3class();
//ft.add(R.id.fragment1,frt1);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.fragment_tabs_exp, menu);
return true;
}
public void switchToTab2 (View v){
ft.replace(R.id.fragment1, frt2).commit();
}
public void switchToTab3 (View v) {
ft.replace(R.id.fragment1, frt3).commit();
}
當我點擊按鈕2片段的Class2是成功可見,除了默認類的片段仍然可見以及反正是有隱藏它,使新的片段類活躍
我點擊按鈕時遇到了麻煩,出現錯誤提示說:無法執行活動的方法。當我點擊標籤1一切都沒問題,因爲frameLayout是空的,但當我點擊tab2錯誤提出任何想法? – Sora
請在您的問題中發佈您的整個logcat錯誤。 – GrIsHu