0

I am trying to make a basic navigation drawer activity to start an app project. The drawer and everything was working fine, then I tried to add functionality to the options in the drawer using fragments. I am getting a java.lang.RuntimeException: Unable to start activity ComponentInfo error.

MainActivity.java

package edu.fsu.cs.mobile.productselectiontoolv4;

import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mToggle;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer);
        NavigationView navigationView = findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);


        mToggle = new ActionBarDrawerToggle(this,mDrawerLayout, R.string.open,R.string.close);
        mDrawerLayout.addDrawerListener(mToggle);
        mToggle.syncState();
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        /*
        *   Avoid reloading fragment when device is rotated
        */

        if(savedInstanceState == null)
        {
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_home, new HomeFragment()).commit();
            navigationView.setCheckedItem(R.id.fragment_home);
        }

    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {

        switch(menuItem.getItemId()) {
            case R.id.home:
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_home, new HomeFragment()).commit();
                break;
            case R.id.history:
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_history, new HistoryFragment()).commit();
                break;
            case R.id.settings:
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_settings, new SettingsFragment()).commit();
                break;
            case R.id.help:
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_help, new HelpFragment()).commit();
                break;
        }

        mDrawerLayout.closeDrawer(GravityCompat.START);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(mToggle.onOptionsItemSelected(item))
            return true;
        return super.onOptionsItemSelected(item);
    }
}

activity_main.xml

<?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/drawer"
    tools:context=".MainActivity">

    <android.support.design.widget.NavigationView
        app:headerLayout="@layout/header"
        android:layout_width="wrap_content"
        android:id="@+id/nav_view"
        android:layout_height="match_parent"
        android:background="@color/darkerGray"
        app:itemTextColor="@color/white"
        app:itemIconTint="@color/white"
        app:menu="@menu/drawermenu"
        android:layout_gravity="start">
    </android.support.design.widget.NavigationView>

</android.support.v4.widget.DrawerLayout>

HomeFragment.java

package edu.fsu.cs.mobile.productselectiontoolv4;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class HomeFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            return inflater.inflate(R.layout.fragment_home, container, false);
    }
}

fragment_home.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/fragment_home"
    android:background="@color/colorPrimary">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="225dp"
        android:text="Home fragment"
        android:textSize="30dp"/>
</RelativeLayout>

Logcat errors

2019-01-24 21:00:27.158 10247-10247/edu.fsu.cs.mobile.productselectiontoolv4 E/AndroidRuntime: FATAL EXCEPTION: main
    Process: edu.fsu.cs.mobile.productselectiontoolv4, PID: 10247
    java.lang.RuntimeException: Unable to start activity ComponentInfo{edu.fsu.cs.mobile.productselectiontoolv4/edu.fsu.cs.mobile.productselectiontoolv4.MainActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f08004d (edu.fsu.cs.mobile.productselectiontoolv4:id/fragment_home) for fragment HomeFragment{9bc7706 #0 id=0x7f08004d}
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
     Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f08004d (edu.fsu.cs.mobile.productselectiontoolv4:id/fragment_home) for fragment HomeFragment{9bc7706 #0 id=0x7f08004d}
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1454)
        at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
        at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:802)
        at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
        at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
        at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
        at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3273)
        at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3229)
        at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:201)
        at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:620)
        at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:178)
Connor S
  • 353
  • 2
  • 12
  • The `R.id` that you pass to a `FragmentTransaction` in the `Activity` must be a `ViewGroup` in the `Activity`'s layout, not one in the `Fragment`'s layout. That `ViewGroup` acts as the container for the `Fragment`'s `View`. Add an empty `` inside the `` – listed above the `` – give it an `android:id`, then pass that `R.id` in your `replace()` call. – Mike M. Jan 25 '19 at 02:19
  • @MikeM. I've done that now, and the app opens and the menu runs with my Home Fragment showing. However now, nothing happens when I click an item in the drawer. I replaced all occurences of replace(R.id.fragment_home with replace(R.id.placeholder, and I added an empty frame layout to my main activity called placeholder – Connor S Jan 25 '19 at 02:26
  • Did you put the `` _before_ the `` in the ``? The `` must be listed last within the `` for it to work correctly. – Mike M. Jan 25 '19 at 02:30
  • 1
    This is working. Thanks! – Connor S Jan 25 '19 at 03:08
  • If you put it as an answer, I'll mark it as best so others see – Connor S Jan 25 '19 at 03:08
  • Actually, I've already closed this as a duplicate. There's a banner now at the top of your question with a link to another post with the solution I described. It's a pretty common issue. Thank you, though. I appreciate the offer. Glad you got it working. Cheers! – Mike M. Jan 25 '19 at 03:14

0 Answers0