-2

I started learning Fragments so i can implent a NavigationDrawer in my app.

I configured the NavigationDrawer and then created 2 Fragments: MainFragment, GalleryFragment.

in my app_bar_main i made a fragment_container:

app_bar_main:

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</FrameLayout>

Up to this point i can start the app and it works without error, but the NavigationDrawer haas zero effect.

When i want to replace the placeholder with one of my fragments, the app crashes:

MainAtivity:

MainFragment fragment = new MainFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();

DEBUG:

    09/26 22:25:24: Launching app
Cold swapped changes.
$ adb shell am start -n "com.example.kasimir.schulplaner/com.example.kasimir.schulplaner.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -D
Waiting for application to come online: com.example.kasimir.schulplaner | com.example.kasimir.schulplaner.test
Waiting for application to come online: com.example.kasimir.schulplaner | com.example.kasimir.schulplaner.test
Connecting to com.example.kasimir.schulplaner
I/art: Ignoring second debugger -- accepting and dropping

ANDROIDMONITOR:

FATAL EXCEPTION: main
                                                   Process: com.example.kasimir.schulplaner, PID: 26444
                                                   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kasimir.schulplaner/com.example.kasimir.schulplaner.MainActivity}: java.lang.RuntimeException: com.example.kasimir.schulplaner.MainActivity@753f53 must implement OnFragmentInteractionListener
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2456)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2539)
                                                       at android.app.ActivityThread.access$900(ActivityThread.java:159)
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1384)
                                                       at android.os.Handler.dispatchMessage(Handler.java:102)
                                                       at android.os.Looper.loop(Looper.java:152)
                                                       at android.app.ActivityThread.main(ActivityThread.java:5507)
                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                    Caused by: java.lang.RuntimeException: com.example.kasimir.schulplaner.MainActivity@753f53 must implement OnFragmentInteractionListener
                                                       at com.example.kasimir.schulplaner.MainFragment.onAttach(MainFragment.java:84)
                                                       at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1043)
                                                       at android.support.v4.app.BackStackRecord.setLastIn(BackStackRecord.java:838)
                                                       at android.support.v4.app.BackStackRecord.calculateFragments(BackStackRecord.java:878)
                                                       at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:719)
                                                       at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1677)
                                                       at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:388)
                                                       at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:604)
                                                       at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:178)
                                                       at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1277)
                                                       at android.app.Activity.performStart(Activity.java:6321)
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2419)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2539) 
                                                       at android.app.ActivityThread.access$900(ActivityThread.java:159) 
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1384) 
                                                       at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                       at android.os.Looper.loop(Looper.java:152) 
                                                       at android.app.ActivityThread.main(ActivityThread.java:5507) 
                                                       at java.lang.reflect.Method.invoke(Native Method) 
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
09-26 22:32:37.725 26444-26444/? D/AppTracker: App Event: crash
09-26 22:32:37.729 3402-4542/? W/ActivityManager:   Force finishing activity com.example.kasimir.schulplaner/.MainActivity
09-26 22:32:37.732 26444-26444/? I/Process: Sending signal. PID: 26444 SIG: 9
09-26 22:32:37.748 3402-26460/? D/DropBoxManagerService: file :: /data/system/dropbox/data_app_crash@2016-09-26-22_32_37_735.txt
09-26 22:32:37.751 3402-4554/? I/ActivityManager: Process com.example.kasimir.schulplaner (pid 26444) has died
Conwear
  • 73
  • 6
  • What is the error? – Code-Apprentice Sep 26 '16 at 20:32
  • Remember that in Java, all code goes in a class and method. It will help us understand what you are doing if you wrap your code snippets in the class and method declarations just like you have them in your actual code. – Code-Apprentice Sep 26 '16 at 20:33
  • I edited the post with some info from androidstudio. Also the App just flashes white and then terminates. If you need more info please tell me. and ill try to edit it correctly@Code-Apprentice – Conwear Sep 26 '16 at 20:34
  • Rather than writing "MainAtivity:", you should use actual Java syntax to show us the class name. You should also include the method that contains the lines of code. Basically, we should be able to copy and paste whatever you post here and compile it with very few changes, such as adding the correct imports. – Code-Apprentice Sep 26 '16 at 20:37

1 Answers1

1

MainActivity@753f53 must implement OnFragmentInteractionListener

This is pretty self-explanatory. OnFragmentInteractionListener was generated when you created your project. This is the usual technique used to communicate between a fragment and its hosting activity. You have two choices: 1) implement the interface or 2) remove it.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268