0
votes

I have a little problem with my android app (one Main Activity for the navigation drawer and many fragments).

I havee a list (FragmentList) wich shows me a list of categories. When the user click on a Category he should see the CategoryDetails site. Well,... this is my Error.

java.lang.IllegalArgumentException: No view found for id 0x7f080042 (com.nma:id/container) for fragment CategoryDetailFragment

I'm trying to replace my CategoriesFragment with the CategoryDetailFragment but it do not work... Can anybody help me?;)

My mainActivity.xml

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_height="match_parent"
    android:layout_width="match_parent">

<FrameLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<fragment
    android:id="@+id/navigation_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:name="com.nma.NavigationDrawerFragment"/>

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

my click listener from CategoriesFragment.java

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    FragmentManager fm = getFragmentManager();
    fm.beginTransaction().replace(R.id.container, new CategoryDetailFragment()).commit();

}

CategoryDetailFragment

public class CategoryDetailFragment extends Fragment {

    private View mView;
    private Uri mCategoryUri;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_category_detail, container, false);

        return mView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }
}

Log

05-19 17:25:25.525 29865-29865/com.nma E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.IllegalArgumentException: No view found for id 0x7f080042 (com.nma:id/container) for fragment CategoryDetailFragment{425649e0 #0 id=0x7f080042} at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:930) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115) at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682) at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478) at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:446) at android.os.Handler.handleCallback(Handler.java:730) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:213) at android.app.ActivityThread.main(ActivityThread.java:5225) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:525) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557) at dalvik.system.NativeStart.main(Native Method)

1
Could you please show us more of the logcat output?Xaver Kapeller
The code you posted seems fine, you have to show me more. Otherwise I will only be able to guess what the problem might be. What Activity are you subclassing? ActionBarActivity? FragmentActivity? What I mean is are you sure you are using the correct FragmentManager? And also as I understand it you have something like this: Activity -> Fragment1 and want the Fragment1 to be replaced? I'm asking because if you have something like Activity -> Fragment1 -> Fragment2 and you wanted to replace Fragment2 you would have to use the ChildFragmentManager.Xaver Kapeller
My MainActivity extends the android.support.v7.app.ActionBarActivity. Within this, i start the CategoriesFragment which extends ListFragment (support.v4). When click on a list item, the onListItemClick function are called (see my code above). There i'm trying to replace my ListFragment with the DetailFragment, which extends to Fragment (support.v4). With the ChildFragmentManager i get the same errorTobias

1 Answers

1
votes

When you are using ActionBarActivity you are supposed to be using the SupportFragmentManager instead of the normal one. I am not sure if this is the source of your error but you should try anyways.

FragmentManager fm = getActivity().getSupportFragmentManager();
fm.beginTransaction().replace(R.id.container, new CategoryDetailFragment()).commit();

Of course for this to work you need to use all the classes from the support library instead of the classes that come with the framework. So you need the FragmentManager from the support library instead of the normale one and all your Fragments need to extend the Fragment class from the support library.