António Paulo
António Paulo

Reputation: 351

Error with fragments in android

I'm building a simple app and I just want to show something from the fragment that I created. I've been searching and I found this question on stackoverflow:

I implemented the code on this answer and this what I have:

            FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            BlankFragment fragment = new BlankFragment();
            fragmentTransaction.add(R.id.content_frame, fragment);
            fragmentTransaction.commit();

My problem is the .add line because Android Studio can't solve the method. I don't know where the problem is so i'll post more code when you need.

EDIT

I want to show the fragment when I click in a cardView. Now, when I click in the view and run this code:

android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
                android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

                BlankFragment fragment = new BlankFragment();
                fragmentTransaction.add(R.id.content_frame, fragment);
                fragmentTransaction.commit();

This logcat is shows up:

03-07 11:21:40.431 13068-13068/com.example.sdilab.pap E/AndroidRuntime: FATAL EXCEPTION: main
                                                                    java.lang.RuntimeException: com.example.sdilab.pap.MainActivity@418d8268 must implement OnFragmentInteractionListener
                                                                        at com.example.sdilab.pap.BlankFragment.onAttach(BlankFragment.java:74)
                                                                        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1019)
                                                                        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
                                                                        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
                                                                        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
                                                                        at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
                                                                        at android.os.Handler.handleCallback(Handler.java:800)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:100)
                                                                        at android.os.Looper.loop(Looper.java:194)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5468)
                                                                        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:936)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
                                                                        at dalvik.system.NativeStart.main(Native Method)

I changed the code and I corrected some mistakes on the onClick() and now the logcat is different.

This is the activity main:

    <?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">



    <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" />

   <include layout="@layout/content_main"/>
    <include layout="@layout/app_bar_main"/>


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

and I have this FrameLayout on content_main:

<FrameLayout
        android:id="@+id/content_frame"
        android:visibility="gone"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </FrameLayout>

This is my onclick():

cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    FrameLayout frame = (FrameLayout)findViewById(R.id.content_frame);
                    android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
                    android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                    //setContentView(R.layout.fragment_blank);
                    BlankFragment fragment = new BlankFragment();
                    fragmentTransaction.add(R.id.content_frame, fragment);
                    fragmentTransaction.commit();
                    frame.setVisibility(View.VISIBLE);

            }
        });

Upvotes: 0

Views: 486

Answers (1)

Himanshu Shekher Jha
Himanshu Shekher Jha

Reputation: 1344

Instead of getFragmentManeger() try to use getSupportFragmentManager();

also check you have added import android.support.v4.app.FragmentManager; in your impor section.

Upvotes: 1

Related Questions