1

In android studio, I have a main class where I create a button wherein i create an intent and go to another class called 'listTest' which has a listView in it wherein i add a list adapter to create the list adapter.

However, whenever I click on the button, The app force closes. I have added the manifest permission, tried everything but can't find the error. Here is the code

Main:

public class Main extends AppCompatActivity
{

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button b2=(Button)findViewById(R.id.button2);
    b2.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            Intent intent=new Intent(Main.this,listTest.class);
            startActivity(intent);
        }
    });
}
}


listView:

public class listTest extends ListActivity
{

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

    setListAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1,
            getResources().getStringArray(R.array.countries)
            ));
     }
}


XML Code:

<?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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.user.uinteraface.listTest">

<ListView
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="143dp"/>
 </RelativeLayout>

LOG Error:

11-18 06:01:43.118 19449-19449/? E/AndroidRuntime: FATAL EXCEPTION: main 11-18 06:01:43.118 19449-19449/? E/AndroidRuntime: Process: com.example.user.uinteraface, PID: 19449 11-18 06:01:43.118 19449-19449/? E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.uinteraface/com.example.user.uinteraface.listTest}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'

gabbar0x
  • 4,046
  • 5
  • 31
  • 51
  • 2
    Use LogCat to examine the Java stack trace associated with your crash: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – CommonsWare Nov 18 '15 at 00:24
  • How to access it from android studio? I can see it down but can't expand it – gabbar0x Nov 18 '15 at 00:25
  • Click "Android Monitor" in the bottom of your Android Studio and then select your emulator/device. – Darwind Nov 18 '15 at 00:26
  • Also, check your `AndroidManifest.xml` file to see if you've added your `listTest` in there. It needs to be added like the rest of your `Activities`. – Darwind Nov 18 '15 at 00:28
  • Yes, as i mentioned I added it the manifests file. Actually android studio does it for you if you create a new activity automatically – gabbar0x Nov 18 '15 at 00:28
  • It says my ListView should have an id 'list' but I have set the id in the XML file. I'll post the XML code and the error log in a minute – gabbar0x Nov 18 '15 at 00:32

1 Answers1

2

Since you are using a ListActivity, your ListView must have an id of @android:id/list.

In your XML file, change android:id="@+id/list" to android:id="@android:id/list" :

<ListView
    android:id="@android:id/list"
    ... />
Mohammed Aouf Zouag
  • 17,042
  • 4
  • 41
  • 67