-1

I've got a problem when trying to run my app in Android Studio. I get a

FATAL EXCEPTION: main

more precisely there seems to be a problem at:

dancam.com.chords.ChordsListActivity.onCreate(ChordsListActivity.java:13)

which is:

setContentView(R.layout.activity_chords_list);

here is the ChordsListActivity class:

import android.app.ListActivity;
import android.os.Bundle;

public class ChordsListActivity extends ListActivity {

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

    String[] accordi = new String[] {"Do maggiore", "Re maggiore", "Si maggiore"};
    String[] note = new String[] {"note: Do, Mi, Sol", "note: La, Re, Fa#", "note: Fa#, Si, Re#"};
    int[] immagini = new int[] {R.drawable.do_maggiore, R.drawable.re_maggiore, R.drawable.si_maggiore};

    CustomListAdapter adapter = new CustomListAdapter(getApplicationContext(), accordi, note, immagini);
    setListAdapter(adapter);
    }
}

I was kinda able to solve it by extending Activity instead of ListActivity but then I cannot use the setListAdapter() method.

I'd really appreciate if someone could help me fix the code!

please let me know if you need me to post the custom Adapter or any other file.

Edit: added the whole stack trace

FATAL EXCEPTION: main
Process: dancam.com.chords, PID: 3080
java.lang.RuntimeException: Unable to start activity ComponentInfo{dancam.com.chords/dancam.com.chords.ChordsListActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
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: Your content must have a ListView whose id attribute is 'android.R.id.list'
at android.app.ListActivity.onContentChanged(ListActivity.java:243)
at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:398)
at android.app.Activity.setContentView(Activity.java:2166)
at dancam.com.chords.ChordsListActivity.onCreate(ChordsListActivity.java:13)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
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) 
Daniele
  • 4,163
  • 7
  • 44
  • 95
  • 1
    You should provide the entire stacktrace so we can figure out *why* the fatal exception is occurring – Arpan Jun 26 '16 at 09:35
  • @Arpan I have added it as code I don't know if it's right. Anyway thank you for helping me out – Daniele Jun 26 '16 at 09:42
  • @MikeM Since I have a custom `listView` I should extend `Activity`. But why then doesn't `setListAdapter` work? – Daniele Jun 26 '16 at 09:53
  • If you want to extend just `Activity`, then you call `setAdapter()` - not `setListAdapter()` - on the `ListView` variable itself, after you've initialized it with `findViewById()`. A regular `Activity` doesn't have the `setListAdapter()` method. That's just a convenience method in `ListActivity` that calls `setAdapter()` on the `ListView` for you. – Mike M. Jun 26 '16 at 09:54
  • Why do you want to use a ListView? That's a bit old approach. Use a RecyclerView instead. – Todor Kostov Jun 26 '16 at 09:57
  • @Todor because I'm new to Android and I have never heard of recyclerView before. I'll have a look at the documentation too – Daniele Jun 26 '16 at 09:59
  • This question has a bad title, see your stacktrace "Your content must have a ListView whose id attribute is 'android.R.id.list'", which is the dupe (so if you searched on that error you would have found the problem, as dupe I can't really see how to improve it. – Petter Friberg Sep 11 '16 at 10:36

2 Answers2

0

When you extend from ListActivity, it already has ListView, but Activity doesn't. You have to create list inbActivity's layout- better use RecyclerView instead of List view. In your activity create RecyclerView reference, initialize it in onCreate. Create adapter for your data in activity (onCreate method). Finnaly, set adapter to listview reference. In order to modify data in a list, set new data in adapter and call .notifyDatasetChange () on it. Notice, for RecyclerView you will also need LayoutManager (see docs)

Alex Shutov
  • 3,217
  • 2
  • 13
  • 11
0

This is problem with ListView. Go to R.layout.activity_chords_list and add <ListView> and set id android:id="@android:id/list"

Like Exception says: 'Your content must have a ListView whose id attribute is 'android.R.id.list'

It should works if rest of the code is ok.

MilanNz
  • 1,323
  • 3
  • 12
  • 29
  • Thank you for your answer, does it work even if I have a custom row layout? – Daniele Jun 26 '16 at 09:58
  • Yes, you need to create your own LayoutManager. There is few by default Linear (as listview ), grid. – Alex Shutov Jun 26 '16 at 10:02
  • No, if there is like img, couple of textview-s and etc. You should use your custom ListAdapter. If it is like one TextView you can avoid it. – MilanNz Jun 26 '16 at 10:03
  • I have a custom layout that's why I was asking – Daniele Jun 26 '16 at 10:06
  • Ah sorry i didn't see, you have CustomAdapter and all these staff. No you don't need to use id android:id/list, use just @+id/list instead. – MilanNz Jun 26 '16 at 10:09