我遇到了几行问题。当我启动应用程序时,它将一直运行,直到我点击导航抽屉中的某些内容然后它将崩溃。对此的任何帮助都会很棒谢谢。
logcat的:
FATAL EXCEPTION: main
10-20 23:02:47.497 3031-3031/? E/AndroidRuntime: Process: com.infintro.keondrae.myapplication, PID: 3031
10-20 23:02:47.497 3031-3031/? E/AndroidRuntime: java.lang.NullPointerException: Attempt to write to field 'android.support.v4.app.FragmentManagerImpl android.support.v4.app.Fragment.mFragmentManager' on a null object reference
10-20 23:02:47.497 3031-3031/? E/AndroidRuntime: at android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:414)
10-20 23:02:47.497 3031-3031/? E/AndroidRuntime: at android.support.v4.app.BackStackRecord.replace(BackStackRecord.java:449)
10-20 23:02:47.497 3031-3031/? E/AndroidRuntime: at android.support.v4.app.BackStackRecord.replace(BackStackRecord.java:441)
10-20 23:02:47.497 3031-3031/? E/AndroidRuntime: at com.infintro.keondrae.myapplication.MainActivity.selectDrawerItem(MainActivity.java:121)
10-20 23:02:47.497 3031-3031/? E/AndroidRuntime: at com.infintro.keondrae.myapplication.MainActivity$2.onNavigationItemSelected(MainActivity.java:73)
10-20 23:02:47.497 3031-3031/? E/AndroidRuntime: at android.support.design.widget.NavigationView$1.onMenuItemSelected(NavigationView.java:150)
10-20 23:02:47.497 3031-3031/? E/AndroidRuntime: at android.support.v7.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:811)
10-20 23:02:47.497 3031-3031/? E/AndroidRuntime: at android.support.v7.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:153)
10-20 23:02:47.497 3031-3031/? E/AndroidRuntime: at android.support.v7.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:958)
10-20 23:02:47.497 3031-3031/? E/AndroidRuntime: at android.support.design.internal.NavigationMenuPresenter$1.onClick(NavigationMenuPresenter.java:300)
10-20 23:02:47.497 3031-3031/? E/AndroidRuntime: at android.view.View.performClick(View.java:5198)
10-20 23:02:47.497 3031-3031/? E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:21147)
10-20 23:02:47.497 3031-3031/? E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)
10-20 23:02:47.497 3031-3031/? E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
10-20 23:02:47.497 3031-3031/? E/AndroidRuntime: at android.os.Looper.loop(Looper.java:148)
10-20 23:02:47.497 3031-3031/? E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5417)
10-20 23:02:47.497 3031-3031/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
10-20 23:02:47.497 3031-3031/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
10-20 23:02:47.497 3031-3031/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
MainActivity.java
public class MainActivity extends AppCompatActivity {
private NavigationView navDrawer;
private DrawerLayout drawer;
private Toolbar toolbar;
private ActionBarDrawerToggle drawerToggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Set toolbar
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// For the floating button
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
//Find drawer layout
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerToggle = setupDrawerToggle();
drawer.setDrawerListener(drawerToggle);
//Find nav drawer
navDrawer = (NavigationView) findViewById(R.id.nav_view);
//Setup drawer
setupDrawerContent(navDrawer);
}
private ActionBarDrawerToggle setupDrawerToggle(){
return new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
}
private void setupDrawerContent(NavigationView navigationView){
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
selectDrawerItem(menuItem);
return true;
}
}
);
}
public void selectDrawerItem(MenuItem menuItem){
//Creating new fragment
Fragment fragment;
fragment = null;
Class fragmentClass;
switch (menuItem.getItemId()){
case R.id.nav_camara:
fragmentClass = One.class;
break;
case R.id.nav_gallery:
fragmentClass = Two.class;
break;
case R.id.nav_manage:
fragmentClass = One.class;
break;
case R.id.nav_send:
fragmentClass = Two.class;
break;
case R.id.nav_share:
fragmentClass = One.class;
break;
case R.id.nav_slideshow:
fragmentClass = Two.class;
break;
case R.id.nav_view:
fragmentClass = One.class;
break;
default:
fragmentClass = One.class;
}
try{
fragment = (Fragment) fragmentClass.newInstance();
}catch (Exception e){
e.printStackTrace();
}
//replacing frag
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.flContent,fragment)
.commit();
//Highlight
menuItem.setChecked(true);
setTitle(menuItem.getTitle());
drawer.closeDrawers();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml
//noinspection SimplifiableIfStatement
switch (item.getItemId()){
case android.R.id.home:
drawer.openDrawer(GravityCompat.START);
return true;
}
return super.onOptionsItemSelected(item);
}
protected void onPostCreate(Bundle saveInstanceStare){
super.onPostCreate(saveInstanceStare);
drawerToggle.syncState();
}
public void onConfigurationChanged(Configuration newConfig){
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}
}
错误行
.replace(R.id.flContent,fragment)
和
selectDrawerItem(menuItem);
activity_main.xml中
<!-- This LinearLayout represents the contents of the screen -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- The ActionBar displayed at the top -->
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!-- The main content view where fragments are loaded -->
<FrameLayout
android:id="@+id/flContent"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
我已经在这里待了几个小时了,我仍然无法弄清楚问题。