大家。我的应用程序崩溃了,我不知道为什么。我最近在学习FragmentTransaction。基本上,activity_main.xml包含我的ActionBar,其中包含两个包含两个片段的选项卡:BasicFragment和BFragment。我希望BasicFragment动态显示片段。有人可以告诉我这里出了什么问题吗?谢谢。
activity_main.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());
}
}
class 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);
}
}
BasicFragment.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;
}
}
afragment.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>
examplefragment.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>
答案 0 :(得分:1)
在您的Main活动中,您尝试访问afragment.xml中的线性布局,错误也是如此。主要活动是显示布局activity_main.xml,将afrag_container更改为activity_main.xml中的fragment_container(线性布局)。