我有一个应用程序,只有一个活动管理5个片段,我试图为每个片段编写逻辑部分,但我不知道为什么不工作。我试着穿上" On Create"但应用程序崩溃了。
在oncreateView中,我既不能编码也不能编码。因为是静态方法。
这是我的代码结构:
**imports
public class MainActivity extends ActionBarActivity implements
NavigationDrawerFragment.NavigationDrawerCallbacks {
private NavigationDrawerFragment mNavigationDrawerFragment;
private DrawerLayout mDrawerLayout;
private CharSequence mTitle;
private static final int FRAGMENT_MAIN_POSITION = 0;
private static final int FRAGMENT_1_POSITION = 1;
private static final int FRAGMENT_2_POSITION = 2;
private static final int FRAGMENT_3_POSITION = 3;
private static final int FRAGMENT_4_POSITION = 4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNavigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager()
.findFragmentById(R.id.navigation_drawer);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mTitle = getTitle();
// Establece el Drawer
mNavigationDrawerFragment.setUp(R.id.navigation_drawer,
(DrawerLayout)
findViewById(R.id.drawer_layout)
);
} //End of onCreate!!
@Override
public void onNavigationDrawerItemSelected(int position) {
// Actualiza el contenido principal reemplazando fragmentos
FragmentManager fragmentManager = getSupportFragmentManager();
switch (position) {
case FRAGMENT_MAIN_POSITION:
fragmentManager
.beginTransaction()
.replace(R.id.container,
FragmentMain.newInstance(position + 1)).commit();
break;
case FRAGMENT_1_POSITION:
fragmentManager
.beginTransaction()
.replace(R.id.container,
Fragment_1.newInstance(position + 1)).commit();
break;
case FRAGMENT_2_POSITION:
fragmentManager
.beginTransaction()
.replace(R.id.container,
Fragment_2.newInstance(position + 1)).commit();
break;
case FRAGMENT_3_POSITION:
fragmentManager
.beginTransaction()
.replace(R.id.container,
Fragment_3.newInstance(position + 1)).commit();
break;
case FRAGMENT_4_POSITION:
fragmentManager
.beginTransaction()
.replace(R.id.container,
Fragment_4.newInstance(position + 1)).commit();
break;
}
}
public void onSectionAttached(int number) {
switch (number) {
case 1:
mTitle = "Title";
break;
case 2:
mTitle = "Title";
break;
case 3:
mTitle = "Title";
break;
case 4:
mTitle = "Title";
break;
case 5:
mTitle = "Title";
break;
}
}
public void restoreActionBar() {
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle(mTitle);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
/**CODE */
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
/** CODE */
}
/**
* Fragmento Main
*/
public static class FragmentMain extends Fragment {
// Argumento que representa el numero de seccion para este Fragment
private static final String ARG_SECTION_NUMBER_1 = "section_number_1";
// Regresa una nueva instancia de este Fragment
public static FragmentMain newInstance(int sectionNumber) {
FragmentMain fragment = new FragmentMain();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER_1, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public FragmentMain() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
// Aqui se intancia el contenido del Layout si se requiere
return rootView;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((MainActivity) activity).onSectionAttached(getArguments().getInt(
ARG_SECTION_NUMBER_1));
}
}
public static class Fragment1 ...
public static class Fragment2 ...
public static class Fragment3 ...
我该怎么办?谢谢你的帮助。