2

I am very new to Android development and Java programming. I was trying to create an app with a ListFragment (it may seem unnecessary to this app but I would want to apply this in a tabbed activity soon, in hopes of developing a bigger app), but unfortunately, my Android device failed to open the app, stating that it "has stopped." I cannot figure out the problem no matter how many tutorials I went through.

Here is my logcat

09-21 20:34:18.825 4369-4369/com.clickme.list E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.clickme.list/com.clickme.list.MainActivity}: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
at android.app.ActivityThread.access$600(ActivityThread.java:162)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5371)
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:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:289)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at com.clickme.list.MainActivity.onCreate(MainActivity.java:11)
at android.app.Activity.performCreate(Activity.java:5122)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395) 
at android.app.ActivityThread.access$600(ActivityThread.java:162) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364) 
at android.os.Handler.dispatchMessage(Handler.java:107) 
at android.os.Looper.loop(Looper.java:194) 
at android.app.ActivityThread.main(ActivityThread.java:5371) 
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:833) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 
at dalvik.system.NativeStart.main(Native Method) 
Caused by: 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:866)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1137)
at android.app.Activity.onCreateView(Activity.java:4735)
at android.support.v4.app.BaseFragmentActivityApi14.onCreateView(BaseFragmentActivityApi14.java:41)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:75)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:746) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:489) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:352) 
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:289) 
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139) 
at com.clickme.list.MainActivity.onCreate(MainActivity.java:11) 
at android.app.Activity.performCreate(Activity.java:5122) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395) 
at android.app.ActivityThread.access$600(ActivityThread.java:162) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364) 
at android.os.Handler.dispatchMessage(Handler.java:107) 
at android.os.Looper.loop(Looper.java:194) 
at android.app.ActivityThread.main(ActivityThread.java:5371) 
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:833) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 
at dalvik.system.NativeStart.main(Native Method) 

Here is my main_activity.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 

<fragment
        android:id="@+id/myListFrag"
        android:name="com.clickme.list.MyListFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:layout="@android:layout/simple_list_item_1" />

</LinearLayout>

Here is my fragment_my_list.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/myListView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

Here is my MainActivity.java

public class MainActivity extends AppCompatActivity {

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

And here is my MyListFragment.java

public class MyListFragment extends ListFragment implements AdapterView.OnItemClickListener {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_my_list, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        ArrayAdapter myListAdapter;
        myListAdapter = ArrayAdapter.createFromResource(getActivity(), R.array.myList, android.R.layout.simple_list_item_1);
        setListAdapter(myListAdapter);
        getListView().setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Toast.makeText(getActivity(), "Item: " + position, Toast.LENGTH_SHORT).show();
    }
}

Thank you for helping a newbie. By the way, I imported android.app.ListFragment; and not the other one. Hope this is clear.

MD. Khairul Basar
  • 4,976
  • 14
  • 41
  • 59
  • Possible duplicate of [Unfortunately MyApp has stopped. How can I solve this?](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – Tim Sep 21 '17 at 12:24
  • Hello. I looked up your link, and it is quite similar (in terms of the stack trace concept, which I just learned from you; thank you so much), but it does not tackle the problem with the ListFragment. Thank you for helping me. – DireNemesis74 Sep 21 '17 at 12:49
  • can you post the getListView() method – letsCode Sep 21 '17 at 12:51
  • actually, no need. the error is inside of your main activity XML. – letsCode Sep 21 '17 at 12:52
  • Hello, @DroiDev. What should I replace with – DireNemesis74 Sep 21 '17 at 13:58
  • my advise is this. stop copy and pasting code from below if you dont know the problem. secondly. what is the flow of your app? why are you putting the listview in a fragment? – letsCode Sep 21 '17 at 14:02
  • @DroiDev Thank you for your advise. I really wanted to create a listview in a tabbed activity, but before that, I wanted to learn how to apply a listview in a fragment because I think it is quite similar to the prior, if I am not mistaken. So I tried creating a separate activity to try out the ListFragment class. Hope this clears it out. – DireNemesis74 Sep 21 '17 at 14:13
  • The root of the problem is actually further down that stacktrace. The static fragment in the xml looks fine to me. I posted an answer that should explain it :) – Jon Sep 21 '17 at 15:41

3 Answers3

0

Try using FrameLayout and SupportFragmentManager like this:

main_activity.xml:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 

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

</LinearLayout>

fragment_my_list.xml:

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

    <ListView
        android:id="@+id/myListView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</FrameLayout>

MainActivity.java:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    MyListFragment fragment = new MyListFragment();
    getSupportFragmentManager()
            .beginTransaction()
            .replace(R.id.myListFrag, fragment)
            .commit();
}

it should work fine.

  • Hello. Thank you for your suggestion. I tried capitalizing the 'f' in – DireNemesis74 Sep 21 '17 at 12:57
  • Thank you for your solution. I tried your code, but unfortunately it still did not work, and the problem still appears to be the same. – DireNemesis74 Sep 21 '17 at 13:34
  • You should try to clean and build your project again, and make sure you have the code exactly as suggested because the line with fragment tag shouldn't be there – Ali Younes Sep 21 '17 at 13:44
  • I cleaned and rebuilt my project, but no luck. I also copied the exact same code you suggested, but it still did not work. Thank you so much. – DireNemesis74 Sep 21 '17 at 13:57
0

If you want to use ListFragment, you need to honor the contract for that component. In other words, you need to use an id that the Android framework already agreed upon so that the Fragment can properly hook up to the ListView. There is a special id you should use, see below:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

  <ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</LinearLayout>

The xml for your activity is fine, there are no problems there. The problems exist from trying to inflate the xml from your fragment into a ListFragment

Caused by: java.lang.RuntimeException: Content has view with id attribute 'android.R.id.list' that is not a ListView class

Jon
  • 1,715
  • 12
  • 14
-1
Please check this 

MainActivity.java

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);

    MyListFragment myListFragment = new MyListFragment ();
        (MainActivity.this).getSupportFragmentManager().beginTransaction().replace(R.id.main_content, loginFragment).commit();
    }
}



main_activity.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">

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

</LinearLayout>



public class MyListFragment extends ListFragment implements AdapterView.OnItemClickListener {

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

    return rootView;
    }

    @Override
   public void onActivityCreated(Bundle savedInstanceState) {
      super.onActivityCreated(savedInstanceState);
      ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity(), R.array.myList, 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();
   }
}