从 navigationDrawer 打开一个新项目(片段)时应用程序崩溃(见代码)?

App crashes when a opening a new item ( fragment) from the navigationDrawer (see code)?

我正在尝试使用 navigationView 制作应用。导航视图有许多选项,单击这些选项会打开一个新的 fragment.I 并将主页片段设置为默认值。但是每当我从导航视图中单击任何选项时,应用程序就会崩溃。我是 android 开发的新手,请帮忙!

我的日志

ava.lang.IllegalArgumentException: No view found for id 0x7f0c0086 (rishabh.example.com.navigationdrawer:id/home_id) for fragment ProfileFragment{10c46e27 #1 id=0x7f0c0086}
                                                   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1059)
                                                   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
                                                   at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742)
                                                   at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
                                                   at android.support.v4.app.FragmentManagerImpl.run(FragmentManager.java:517)
                                                   at android.os.Handler.handleCallback(Handler.java:739)
                                                   at android.os.Handler.dispatchMessage(Handler.java:95)
                                                   at android.os.Looper.loop(Looper.java:135)
                                                   at android.app.ActivityThread.main(ActivityThread.java:5343)
                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                   at java.lang.reflect.Method.invoke(Method.java:372)
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)

我的MainActivity.java

import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {

DrawerLayout drawerLayout;
android.support.v7.app.ActionBarDrawerToggle actionBarDrawerToggle;
Toolbar toolbar;
android.support.v4.app.FragmentTransaction fragmentTransaction;
NavigationView navigationView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    toolbar=(Toolbar)findViewById(R.id.toolbar_layout);
    setSupportActionBar(toolbar);
    drawerLayout=(DrawerLayout)findViewById(R.id.drawerLayout);


    actionBarDrawerToggle=new android.support.v7.app.ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.drawer_open,R.string.drawer_close);

    drawerLayout.addDrawerListener(actionBarDrawerToggle);


    fragmentTransaction=getSupportFragmentManager().beginTransaction();
    fragmentTransaction.add(R.id.container_fragment,new HomeFragment());
    fragmentTransaction.commit();
    getSupportActionBar().setTitle("Home");

    navigationView=(NavigationView)findViewById(R.id.navView);
    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem item) {


            switch(item.getItemId()){
                case R.id.home_id:

                    fragmentTransaction=getSupportFragmentManager().beginTransaction();
                    fragmentTransaction.replace(R.id.home_id,new HomeFragment());
                    fragmentTransaction.commit();
                    getSupportActionBar().setTitle("Home");
                    item.setChecked(true);
                    drawerLayout.closeDrawers();
                    break;
                case R.id.profile_id:
                    Log.i("tag","inside profile");
                    fragmentTransaction=getSupportFragmentManager().beginTransaction();
                    fragmentTransaction.replace(R.id.home_id,new ProfileFragment());
                    fragmentTransaction.commit();
                    Log.i("tag","inside profile commit");
                    getSupportActionBar().setTitle("Profile");
                    item.setChecked(true);
                    drawerLayout.closeDrawers();
                    break;
                case R.id.tracker_id:
                    fragmentTransaction=getSupportFragmentManager().beginTransaction();
                    fragmentTransaction.replace(R.id.home_id,new TracerFragment());
                    fragmentTransaction.commit();
                    getSupportActionBar().setTitle("Tracker");
                    item.setChecked(true);
                    drawerLayout.closeDrawers();
                    break;
                case R.id.siren_id:
                    fragmentTransaction=getSupportFragmentManager().beginTransaction();
                    fragmentTransaction.replace(R.id.home_id,new SirenFragment());
                    fragmentTransaction.commit();
                    getSupportActionBar().setTitle("Siren");
                    item.setChecked(true);
                    drawerLayout.closeDrawers();
                    break;
                case R.id.settings_id:
                    fragmentTransaction=getSupportFragmentManager().beginTransaction();
                    fragmentTransaction.replace(R.id.home_id,new SettingsFragment());
                    fragmentTransaction.commit();
                    getSupportActionBar().setTitle("Settings");
                    item.setChecked(true);
                    drawerLayout.closeDrawers();
                    break;
                case R.id.support_id:
                    fragmentTransaction=getSupportFragmentManager().beginTransaction();
                    fragmentTransaction.replace(R.id.home_id,new SupportFragment());
                    fragmentTransaction.commit();
                    getSupportActionBar().setTitle("Support");
                    item.setChecked(true);
                    drawerLayout.closeDrawers();
                    break;
                case R.id.about_id:
                    fragmentTransaction=getSupportFragmentManager().beginTransaction();
                    fragmentTransaction.replace(R.id.home_id,new AboutUsFragment());
                    fragmentTransaction.commit();
                    getSupportActionBar().setTitle("About Us");
                    item.setChecked(true);
                    drawerLayout.closeDrawers();
                    break;




            }
            return true;
        }
    });

}

activity_layout

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawerLayout"
tools:context="rishabh.example.com.navigationdrawer.MainActivity">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <include android:layout_height="wrap_content"
        android:layout_width="match_parent"
        layout="@layout/toolbar_layout"
        />
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/container_fragment">

    </FrameLayout>

</LinearLayout>
<android.support.design.widget.NavigationView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/navView"
    android:layout_gravity="start"
    app:menu="@menu/drawer_menu"
    app:headerLayout="@layout/navigation_drawer_head"
    >


</android.support.design.widget.NavigationView>

ProfileFragment

    public class ProfileFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_profile, container, false);
    }

}

您的布局中没有 home_id。您应该将片段加载到 container_fragment

fragmentTransaction.replace(R.id.home_id,new HomeFragment());
                fragmentTransaction.commit();

替换为

fragmentTransaction.replace(R.id.container_fragment,new HomeFragment());

对所有 replace() 调用进行相同的更改(container_fragment 代替 home_id) .