1

I am a Beginner am writing a code of listView that perform an action on OnclickListner..But it gives error "Unfortunately Stops". I want to perform different action on selecting different list items. Im new to android development so im following basic tutorials online.

I've got this switch/case structure:

package com.list;

import android.os.Bundle;    
import android.app.ListActivity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.CheckedTextView;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends ListActivity {

    final int Aatma = 0; //should be equal to the index in your array.
    final int Special26 = 1;
    final int Dabangg2 = 2;

    String menu[] = {"Aatma","Special26","Dabangg2" };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       // ListView l = (ListView)findViewById(R.id.list);
        setListAdapter(new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,menu));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    protected void onListItemClick(ListView l, View v, int position, long id) {

        // super.onListItemClick(l, v, position, id);



       CheckedTextView selectedItem = (CheckedTextView) v;
       boolean isChecked = selectedItem.isChecked();
       Log.e("TAG","item clicked position = " + position + " isChecked = " + isChecked);

        switch (position) {
        case Aatma:
            Toast.makeText(this, "AAtma !", Toast.LENGTH_LONG).show();

            break;
        case Special26:
            Toast.makeText(this, "Special26 !", Toast.LENGTH_LONG).show();

            break;

        case Dabangg2:
            Toast.makeText(this, "Dabangg 2 !", Toast.LENGTH_LONG).show();

            break;
        default:
            break;
        }

    }

}



Error Log-
04-29 18:28:21.514: W/dalvikvm(782): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
04-29 18:28:21.544: E/AndroidRuntime(782): FATAL EXCEPTION: main
04-29 18:28:21.544: E/AndroidRuntime(782): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.list/com.list.MainActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
04-29 18:28:21.544: E/AndroidRuntime(782):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
04-29 18:28:21.544: E/AndroidRuntime(782):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-29 18:28:21.544: E/AndroidRuntime(782):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-29 18:28:21.544: E/AndroidRuntime(782):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-29 18:28:21.544: E/AndroidRuntime(782):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-29 18:28:21.544: E/AndroidRuntime(782):  at android.os.Looper.loop(Looper.java:137)
04-29 18:28:21.544: E/AndroidRuntime(782):  at android.app.ActivityThread.main(ActivityThread.java:5041)
04-29 18:28:21.544: E/AndroidRuntime(782):  at java.lang.reflect.Method.invokeNative(Native Method)
04-29 18:28:21.544: E/AndroidRuntime(782):  at java.lang.reflect.Method.invoke(Method.java:511)
04-29 18:28:21.544: E/AndroidRuntime(782):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-29 18:28:21.544: E/AndroidRuntime(782):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-29 18:28:21.544: E/AndroidRuntime(782):  at dalvik.system.NativeStart.main(Native Method)
04-29 18:28:21.544: E/AndroidRuntime(782): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
04-29 18:28:21.544: E/AndroidRuntime(782):  at android.app.ListActivity.onContentChanged(ListActivity.java:243)
04-29 18:28:21.544: E/AndroidRuntime(782):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:273)
04-29 18:28:21.544: E/AndroidRuntime(782):  at android.app.Activity.setContentView(Activity.java:1881)
04-29 18:28:21.544: E/AndroidRuntime(782):  at com.list.MainActivity.onCreate(MainActivity.java:25)
04-29 18:28:21.544: E/AndroidRuntime(782):  at android.app.Activity.performCreate(Activity.java:5104)
04-29 18:28:21.544: E/AndroidRuntime(782):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-29 18:28:21.544: E/AndroidRuntime(782):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
04-29 18:28:21.544: E/AndroidRuntime(782):  ... 11 more
chunky8666
  • 13
  • 4
  • Reading exception messages proved to be very useful: "Your content must have a ListView whose id attribute is 'android.R.id.list'" – CodeZombie Apr 29 '13 at 18:36
  • The problem is very clear `Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'` One minute on Google will find your answer. – Simon Apr 29 '13 at 18:36
  • possible duplicate of [RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'](http://stackoverflow.com/questions/3040374/runtimeexception-your-content-must-have-a-listview-whose-id-attribute-is-andro) – Simon Apr 29 '13 at 18:37
  • @Simon: it's easy for us to understand that stack trace but a beginner might need some extra help on top of the "google it" advice. The duplicate question isn't really helpful either especially because there's no answer marked as correct. It's not always lack of will that leads to such questions. – Emanuel Moecklin Apr 29 '13 at 19:04

1 Answers1

0

Your layout needs to contain a listview with id android.R.id.list like so:

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

Important is that the id is @android:id/list as explained in http://developer.android.com/reference/android/app/ListActivity.html (see section "screen layout").

Emanuel Moecklin
  • 28,488
  • 11
  • 69
  • 85
  • but can i have a correction code ..i have added id to listview allready – chunky8666 Apr 29 '13 at 18:41
  • @+id/list is not the same as @android:id/list. android:id defines the namespace for the id "identifier" and it has to be android which is referring to the namespace you usually define in your root xml tag: xmlns:android="http://schemas.android.com/apk/res/android" – Emanuel Moecklin Apr 29 '13 at 18:45
  • Thank You very much SIR for your help....But on clicking on List items "Unfortunately list stops " error is showing – chunky8666 Apr 29 '13 at 18:52
  • Well that would be another exception and it would be helpful if you posted the stack trace for that one too. – Emanuel Moecklin Apr 29 '13 at 18:55