大家。我的应用程序崩溃了,我不知道为什么。我最近在学习 FragmentTransaction。基本上,activity_main.xml 包含我的 ActionBar,其中有两个选项卡,其中包含两个片段,BasicFragment 和 BFragment。我希望 BasicFragment 动态显示片段。有人可以告诉我这里出了什么问题吗?谢谢你。
活动主.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</LinearLayout>
</LinearLayout>
MainActivity.java:
public class MainActivity extends Activity {
public static Context appContext;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
actionBar();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
BasicFragment fragmentee = new BasicFragment();
fragmentTransaction.add(R.id.afrag_container, fragmentee);
fragmentTransaction.commit();
}
private void actionBar() {
// TODO Auto-generated method stub
appContext = getApplicationContext();
// ActionBar
ActionBar actionbar = getActionBar();
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionbar.setDisplayShowTitleEnabled(false);
ActionBar.Tab PlayerTab = actionbar.newTab().setText("Fragment A");
ActionBar.Tab StationsTab = actionbar.newTab().setText("Fragment B");
Fragment PlayerFragment = new BasicFragment();
Fragment StationsFragment = new BFragment();
PlayerTab.setTabListener(new TabListener(PlayerFragment));
StationsTab.setTabListener(new TabListener(StationsFragment));
actionbar.addTab(PlayerTab);
actionbar.addTab(StationsTab);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_pow, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menuitem_search:
Toast.makeText(appContext, "search", Toast.LENGTH_SHORT).show();
return true;
case R.id.menuitem_add:
Toast.makeText(appContext, "add", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("tab", getActionBar().getSelectedNavigationIndex());
}
}
类 TabListener 实现 ActionBar.TabListener { public Fragment 片段;
public TabListener(Fragment fragment) {
this.fragment = fragment;
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
Toast.makeText(MainActivity.appContext, "Reselected!",
Toast.LENGTH_LONG).show();
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
ft.replace(R.id.fragment_container, fragment);
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
ft.remove(fragment);
}
}
基本片段.java:
public class BasicFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.examplefragment, container, false);
return view;
}
}
片段.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/afrag_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
示例片段.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<Button android:id="@+id/butt1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="HI!" />
</LinearLayout>