-1

So i am practicing using list view and i was having a look at several questions online, and was trying to make it so that there were two activities, one with a list view and another where the user would be able to add to that list, so this is what i tried making:

ACTIVITY 1

package com.example.nooli.listview; 
import android.app.ListActivity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;

import java.util.ArrayList;

public class MainActivity extends ListActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final ArrayList<String> things2 = new ArrayList<String>();
        final ArrayAdapter<String> adapter = new ArrayAdapter<String>(getListView().getContext(),android.R.layout.simple_list_item_1,things2);
        getListView().setAdapter(adapter);


        Button add = (Button)findViewById(R.id.button);
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent setnew = new Intent(getApplicationContext(),Main2Activity.class);

            }
        });
        Button newb = (Button)findViewById(R.id.button2);
        final EditText et = (EditText)findViewById(R.id.editText);
        newb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                things2.add(String.valueOf(et.getText()));
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(getListView().getContext(),android.R.layout.simple_list_item_1,things2);
                getListView().setAdapter(adapter);
            }
        });

    }
}

ACTIVITY 2

package com.example.nooli.listview;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Main2Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        Button b = (Button)findViewById(R.id.button2);
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(getApplicationContext(),MainActivity.class);
                startActivity(i);
            }
        });
    }
}

ACTIVITY 1 XML

<?xml version="1.0" encoding="utf-8"?>
<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.nooli.listview.MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button"
        android:textColor="@android:color/white"
        android:background="@drawable/plus"
        android:text="+"
        android:layout_alignParentBottom="true"
        android:elevation="10dp"
        android:layout_alignRight="@+id/list"
        android:layout_alignEnd="@+id/list" />

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/list"
        android:choiceMode="singleChoice"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

</RelativeLayout>

ACTIVITY 2 XML

<?xml version="1.0" encoding="utf-8"?>
<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.nooli.listview.Main2Activity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="make new"
        android:id="@+id/button2"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="126dp" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/editText"
        android:layout_marginTop="66dp"
        android:width="300dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

LOGCAT

02-05 16:52:39.944 24684-24684/? E/Zygote: MountEmulatedStorage()
02-05 16:52:39.944 24684-24684/? E/Zygote: v2
02-05 16:52:39.944 24684-24684/? I/libpersona: KNOX_SDCARD checking this for 10211
02-05 16:52:39.944 24684-24684/? I/libpersona: KNOX_SDCARD not a persona
02-05 16:52:39.984 24684-24684/? I/SELinux: Function: selinux_compare_spd_ram, SPD-policy is existed. and_ver=SEPF_SM-G900F_5.0 ver=27
02-05 16:52:39.984 24684-24684/? I/SELinux: Function: selinux_compare_spd_ram , priority [2] , priority version is VE=SEPF_SM-G900F_5.0-1_0032
02-05 16:52:39.984 24684-24684/? E/SELinux: [DEBUG] get_category: variable seinfo: default sensitivity: NULL, cateogry: NULL
02-05 16:52:39.984 24684-24684/? I/art: Late-enabling -Xcheck:jni
02-05 16:52:40.004 24684-24684/? D/TimaKeyStoreProvider: TimaSignature is unavailable
02-05 16:52:40.004 24684-24684/? D/ActivityThread: Added TimaKeyStore provider
02-05 16:52:40.054 24684-24684/com.example.nooli.listview D/ResourcesManager: creating new AssetManager and set to /data/app/com.example.nooli.listview-2/base.apk
02-05 16:52:40.174 24684-24684/com.example.nooli.listview D/AbsListView: Get MotionRecognitionManager
02-05 16:52:40.174 24684-24684/com.example.nooli.listview D/AndroidRuntime: Shutting down VM
02-05 16:52:40.174 24684-24684/com.example.nooli.listview E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.nooli.listview, PID: 24684
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nooli.listview/com.example.nooli.listview.MainActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2658)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2723)
    at android.app.ActivityThread.access$900(ActivityThread.java:172)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:145)
    at android.app.ActivityThread.main(ActivityThread.java:5832)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
 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.impl.PhoneWindow.setContentView(PhoneWindow.java:432)
    at android.app.Activity.setContentView(Activity.java:2241)
    at com.example.nooli.listview.MainActivity.onCreate(MainActivity.java:19)
    at android.app.Activity.performCreate(Activity.java:6221)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2611)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2723) 
    at android.app.ActivityThread.access$900(ActivityThread.java:172) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:145) 
    at android.app.ActivityThread.main(ActivityThread.java:5832) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194) 
Elye
  • 53,639
  • 54
  • 212
  • 474
K Dev
  • 25
  • 6
  • It's a rare case when the exception message is that suggestive, it practically points right to the issue, yet it seems like you totally ignored it. – Egor Feb 05 '16 at 17:14
  • It may not be a solution but good to take care of: android:layout_alignRight="@+id/list" and android:layout_alignEnd="@+id/list" in the Botton needs to be changed to "@id/list" – Pooya Feb 05 '16 at 17:14
  • 2
    Possible duplicate of [Your content must have a ListView whose id attribute is 'android.R.id.list'](http://stackoverflow.com/questions/11050817/your-content-must-have-a-listview-whose-id-attribute-is-android-r-id-list) – Elye Feb 05 '16 at 17:20
  • The first class is extending `ListActivity`, is it necessary to have `AppCompatActivity` in the imports? try CTRL + alt + o to remove the unnecessary imports and do this: `android:id="@android:id/list" `for that `ListView` as mentioned here: http://stackoverflow.com/a/11050829/4409113 – ʍѳђઽ૯ท Feb 05 '16 at 17:24
  • If you'd like more attention to your questions, try not to be so vague in the description. At the same time, read the logs – OneCricketeer Feb 05 '16 at 17:30

2 Answers2

0

The most important line is the following:

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

Your content must have a ListView whose id attribute is 'android.R.id.list'

meda
  • 45,103
  • 14
  • 92
  • 122
0

First of all, the way to debug Android App crash is to first look at the last "Cause" of crash. In your case it is

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

From here you could do a search on the crash message, i.e. Your content must have a ListView whose id attribute is 'android.R.id.list'

If you google search it, you'll find the same question asked in Your content must have a ListView whose id attribute is 'android.R.id.list' with the best answer in https://stackoverflow.com/a/11050829/3286489.

Hence the answer to your question would be to convert your ListView ID from

<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/list"
    android:choiceMode="singleChoice"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

to

<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@android:id/list"
    android:choiceMode="singleChoice"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

Hope this helps for your future bug hunt and search for Android solution. Cheers!

Community
  • 1
  • 1
Elye
  • 53,639
  • 54
  • 212
  • 474