-1

I'm trying to develop an Android app in which it displays a list of fruits in a ListView format. However, no matter how I try and how closely I follow the tutorials, it always crashes upon opening.

This is my main activity

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

    ListView listView = (ListView) findViewById(R.id.list);
    String[] values = new String[]{"apples", "oranges", "grapes", "pomegrantes"};
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, values);
    listView.setAdapter(adapter);
}

This is my main activity xml

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.test.MainActivity" >

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="36dp"
    android:orientation="vertical" >
</LinearLayout>

<ListView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/linearLayout1"
    android:layout_centerVertical="true" >
</ListView>
</RelativeLayout>

This is my list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/list_entry_title"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textSize="20sp" /> 
</LinearLayout>

Any ideas on what I'm doing wrong?

EDIT

This is what my logcat is saying:

AndroidRuntime(19894): FATAL EXCEPTION: main

AndroidRuntime(19894): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.MainActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'

AndroidRuntime(19894): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)

AndroidRuntime(19894): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)

AndroidRuntime(19894): at android.app.ActivityThread.access$600(ActivityThread.java:141)

AndroidRuntime(19894): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)

AndroidRuntime(19894): at android.os.Handler.dispatchMessage(Handler.java:99)

AndroidRuntime(19894): at android.os.Looper.loop(Looper.java:137)

AndroidRuntime(19894): at android.app.ActivityThread.main(ActivityThread.java:5103)

AndroidRuntime(19894): at java.lang.reflect.Method.invokeNative(Native Method)

AndroidRuntime(19894): at java.lang.reflect.Method.invoke(Method.java:525)

AndroidRuntime(19894): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)

AndroidRuntime(19894): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)

AndroidRuntime(19894): at dalvik.system.NativeStart.main(Native Method)

AndroidRuntime(19894): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list' AndroidRuntime(19894): at android.app.ListActivity.onContentChanged(ListActivity.java:243) AndroidRuntime(19894): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)

AndroidRuntime(19894): at android.app.Activity.setContentView(Activity.java:1895)

AndroidRuntime(19894): at com.example.test.MainActivity.onCreate(MainActivity.java:15)

AndroidRuntime(19894): at android.app.Activity.performCreate(Activity.java:5133)

AndroidRuntime(19894): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)

AndroidRuntime(19894): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)

AndroidRuntime(19894): ... 11 more

SOLVED!!

Thank you to all who helped! In the end a combination of the answers worked, my final code is as follows

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

    ListView listView = (ListView) findViewById(android.R.id.list);
    String[] values = new String[]{"apples", "oranges", "grapes", "pomegrantes"};
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item,    R.id.list_entry_title, values);
    listView.setAdapter(adapter);
}
iggy
  • 3
  • 2

2 Answers2

1

java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'

<ListView android:id="@android:id/list"
Martin
  • 2,146
  • 4
  • 21
  • 32
0

Use @android:id/list you should use that id when using a ListActivity and also change

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, values);

with

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.list_entry_title, values);

Check the doc

the 3º arguments specifies the id of the TextView that the Array adapter will use to set the text

forcewill
  • 1,637
  • 2
  • 16
  • 31