-3

So i am trying to start a fragment by clicking a button on a navigation bar from the default template, I am using fragment transaction and committing, but the app crashes with the unable to find activity hidden away in the android monitor error section on the drop down. I get this crash using replace and add.

Here is the fragment I am trying to add:

public class FoodScroll extends ListFragment implements AdapterView.OnItemClickListener{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        return inflater.inflate(R.layout.fragment_foodscroll, container, false);
    }
    @Override
    public void onActivityCreated(Bundle savedInstanceState){
        super.onActivityCreated(savedInstanceState);
        ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity(), R.array.foods, android.R.layout.simple_list_item_1);
        setListAdapter(adapter);
        getListView().setOnItemClickListener(this);
    }
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id){
        Toast.makeText(getActivity(), "Item" + position, Toast.LENGTH_SHORT).show();
    }
}

Here is the activity I am trying to place it in:

public class MainActivity extends AppCompatActivity{

    private FragmentManager manager = getFragmentManager();
    private FragmentTransaction transaction = manager.beginTransaction();
    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

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

            switch (item.getItemId()) {
                case R.id.navigation_home:
                    FoodScroll foods = new FoodScroll();
                    transaction.replace(R.id.container, foods);
                    transaction.commit();
                    return true;
                case R.id.navigation_dashboard:

                    return true;
                case R.id.navigation_notifications:

                    return true;
            }
            return false;
        }

    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

    }
}

The package statement is not missing, I took it out of the snippet. Apologies if I incorrectly placed the code or something.

The logcat wasn't working properly and I had to restart android studio to get it working again. This is what shows up

03-15 22:57:17.090 3238-3238/? I/art: Not late-enabling -Xcheck:jni (already on)
03-15 22:57:17.090 3238-3238/? W/art: Unexpected CPU variant for X86 using defaults: x86
03-15 22:57:17.110 3238-3245/? E/art: Failed sending reply to debugger: Broken pipe
03-15 22:57:17.110 3238-3245/? I/art: Debugger is no longer active
03-15 22:57:17.110 3238-3245/? I/art: Starting a blocking GC Instrumentation
03-15 22:57:17.199 3238-3238/? W/System: ClassLoader referenced unknown path: /data/app/com.namename.www.name-2/lib/x86
03-15 22:57:17.253 3238-3238/? W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
03-15 22:57:17.393 3238-3238/? I/InstantRun: Starting Instant Run Server for com.namename.www.name
03-15 22:57:17.454 3238-3262/? I/OpenGLRenderer: Initialized EGL, version 1.4
03-15 22:57:17.454 3238-3262/? D/OpenGLRenderer: Swap behavior 1
03-15 22:57:17.454 3238-3262/? W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
03-15 22:57:17.454 3238-3262/? D/OpenGLRenderer: Swap behavior 0
03-15 22:57:17.488 3238-3238/? W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
03-15 22:57:25.564 3238-3238/com.chilosomexicanbistro.www.chiloso D/AndroidRuntime: Shutting down VM
03-15 22:57:25.564 3238-3238/com.chilosomexicanbistro.www.chiloso E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                    Process: com.namename.www.name, PID: 3238
                                                                                    java.lang.RuntimeException: Content has view with id attribute 'android.R.id.list' that is not a ListView class
                                                                                        at android.app.ListFragment.ensureList(ListFragment.java:402)
                                                                                        at android.app.ListFragment.onViewCreated(ListFragment.java:203)
                                                                                        at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1010)
                                                                                        at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1171)
                                                                                        at android.app.BackStackRecord.run(BackStackRecord.java:816)
                                                                                        at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1578)
                                                                                        at android.app.FragmentManagerImpl$1.run(FragmentManager.java:483)
                                                                                        at android.os.Handler.handleCallback(Handler.java:751)
                                                                                        at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                        at android.os.Looper.loop(Looper.java:154)
                                                                                        at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Fail Pail
  • 7
  • 8
  • 1
    Please post the stack trace. http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – Mike M. Mar 16 '17 at 03:26
  • 1
    That stack trace does not match the code you've provided. That code doesn't call `startActivity()` in `onNavigationItemSelected()`. If your app is crashing with the code you've shown, you need to provide that stack trace. If it's not crashing from that code, you need to better explain exactly what the problem is. – Mike M. Mar 16 '17 at 03:45
  • I don't know, thats what is in the android monitor tab under errors. The app just crashes saying it has stopped when I click on the button that supposed to add or replace the fragment. – Fail Pail Mar 16 '17 at 03:52
  • Clean/rebuild your project, and try it again. – Mike M. Mar 16 '17 at 03:56
  • Added the stack trace I think. This is different but is also weird, the only thing that has id list is a ListView. – Fail Pail Mar 16 '17 at 04:00
  • 1
    In a `ListFragment`, the `ListView`'s ID must be `@android:id/list`, if using your own layout. – Mike M. Mar 16 '17 at 04:03
  • That was it! thank you! – Fail Pail Mar 16 '17 at 04:15
  • have you declared this activity in your AndroidManifest.xml? – user7718720 Mar 16 '17 at 03:45

1 Answers1

1

There are a couple of issues here:

Problem 1 - using static initialisation incorrectly

private FragmentManager manager = getFragmentManager(); //no! don't do this
private FragmentTransaction transaction = manager.beginTransaction(); //no!

Using static initialisation does not work very well with the Android Activity lifecycles. Please study these carefully and follow closely the examples in the Android documentation - you will hardly ever find any static initialisation like that in the sample Activity and Fragments.

Problem 2 - not checking the FragmentManager to see if it already contains the fragment

        switch (item.getItemId()) {
            case R.id.navigation_home:
                FoodScroll foods = new FoodScroll(); //no! check first if the fragment is already in the FragmentManager
                transaction.replace(R.id.container, foods);
                transaction.commit();
                return true;

This code is wrong - again, in the examples you almost always see the calls written idiomatically as getFragmentManager().beginTransaction().... Please try to follow the sample Fragments and Activities in the documentation more carefully. Trust me, it will save you time :)

Instead of the above code, I think you want something like this:

case R.id.navigation_home:
    FoodScroll foods = getFragmentManager().findFragmentByTag("FOOD");
    if (foods == null) {
        foods = new FoodScroll();
    }
    getFragmentManager().beginTransaction().replace(R.id.container, foods, "FOOD").commit();
    return true;

Problem 3 - updated after OP edit with correct stacktrace:

As per Mike M's comment, in a ListFragment, theListView's ID must be @android:id/list, if using your own layout.

David Rawson
  • 20,912
  • 7
  • 88
  • 124