1

I'm new to android and I'm having a a frustrating challenge that I'm not sure how to deal with. I am trying to create an app that uses two fragments, one a fragmentlist that will list off cities, and one that will display information about the city that is selected. I'm getting a fatal exception in the main.

I showed asked my prof about what could be going on and he looked at my project for about 20 minutes and couldn't figure out what was wrong with it either. I've tried commenting out parts of code that I thought might be contributing to it but I believe that the problem has something to do with my xml, but I am not very familiar with them yet.


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

    @Override
    public void onListFragmentInteraction(DummyContent.DummyItem item){
        Log.v("idk", "item: \n" + item.details);

        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        CityFragment cityFragment = (CityFragment) fragmentManager.findFragmentById(R.id.cityListFragment);
        TextFragment textFragment = (TextFragment) fragmentManager.findFragmentById(R.id.textFragment);

        fragmentTransaction.add(R.id.cityListFragment, cityFragment);
        fragmentTransaction.commit();
        fragmentTransaction.add(R.id.textFragment, textFragment);
        fragmentTransaction.commit();
    }



    @Override
    public void onFragmentInteraction(Uri uri){

    }
}

this is my main activity

<android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <View
        android:id="@+id/view"
        style="@style/Divider"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

    </View>

    <fragment
        android:id="@+id/cityListFragment"
        android:name="com.example.tourguide.CityFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toTopOf="@+id/view"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <fragment
        android:id="@+id/textFragment"
        android:name="com.example.tourguide.TextFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/view" />



</android.support.constraint.ConstraintLayout>

this is my xml file for the main

    Process: com.example.tourguide, PID: 15175
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tourguide/com.example.tourguide.MainActivity}: android.view.InflateException: Binary XML file line #23: Binary XML file line #23: Error inflating class fragment
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: android.view.InflateException: Binary XML file line #23: Binary XML file line #23: Error inflating class fragment
     Caused by: android.view.InflateException: Binary XML file line #23: Error inflating class fragment
     Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
        at android.support.v4.app.ListFragment.ensureList(ListFragment.java:349)
        at android.support.v4.app.ListFragment.onViewCreated(ListFragment.java:150)
        at android.support.v4.app.FragmentManagerImpl.ensureInflatedFragmentView(FragmentManager.java:1695)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1431)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1684)
        at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1930)
        at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3745)
        at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:120)
        at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:405)
        at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:387)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:780)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
        at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
        at com.example.tourguide.MainActivity.onCreate(MainActivity.java:21)
        at android.app.Activity.performCreate(Activity.java:7136)
        at android.app.Activity.performCreate(Activity.java:7127)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
2019-05-22 19:25:04.806 15175-15175/com.example.tourguide E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
2019-05-22 19:25:04.819 15175-15175/com.example.tourguide I/Process: Sending signal. PID: 15175 SIG: 9

and this is my Logcat.

I apologize for the large amount of code, or if I am making a very basic mistake in advance but I have spent a lot of time trying to solve this problem on my own and have just got more confused as a result. Thanks for your help!

Zoe
  • 27,060
  • 21
  • 118
  • 148

2 Answers2

1

line 23 reads: goto com.example.tourguide.CityFragment and add resId android.R.id.list to whatever ListView in that fragment (no matter if that's constructed from XML or by Java).

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
  • So would I add it to the main activity xml? or the xml for that specific fragment? – Caleb Stasiuk May 23 '19 at 03:58
  • @CalebStasiuk as stated, that resId might either be missing in the XML or the Java code of that fragment, which has the `ListView`. – Martin Zeitler May 23 '19 at 05:02
  • It ended up being a really stupid mistake that I was making. I was using a line that I had made in styles instead of using a guideline. When I changed that everything works as it supposed to! – Caleb Stasiuk May 23 '19 at 20:56
0

first of all you should set width and height to your fragment ,

android:layout_width="match_parent"
android:layout_height="xxdp"
ismail alaoui
  • 5,748
  • 2
  • 21
  • 38