java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'- error [duplicate]

StackOverflow https://stackoverflow.com/questions/22782347

Question

I'm developing a simple android application. I want to display listview attributes using another activity when clicking the list item. But I stucked in the above error.

This is my java class,

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

// Main class for creating job_id lists
public class TourNumberList extends ListActivity{

TourNumberListAdapter tourNumberListAdapter;

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tour_list);


TourInfoDatabaseHelper jobInfoDatabaseHelper = new TourInfoDatabaseHelper(this);

Cursor cursorTourNumberInfo = jobInfoDatabaseHelper.getAllJobInfoRecords();

    if(cursorTourNumberInfo.moveToFirst()){
        do{
            String jobId = cursorTourNumberInfo.getString(0);
            String startLocation = cursorTourNumberInfo.getString(1);
            String endLocation = cursorTourNumberInfo.getString(2);
            String type = cursorTourNumberInfo.getString(3);
            String tourNumber = cursorTourNumberInfo.getString(5);

            android.util.Log.d("DB value", jobId+" "+startLocation+" "+endLocation+" "+type+" "+tourNumber);
        }while(cursorTourNumberInfo.moveToNext());
    }



    ListView listView = (ListView) findViewById(R.id.tourList);
    tourNumberListAdapter = new TourNumberListAdapter(this, cursorTourNumberInfo);
    listView.setAdapter(tourNumberListAdapter);

     // Get listview
    ListView lv = getListView();

    // on seleting single product
    // launching Edit Product Screen
    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // getting values from selected ListItem
            String startLocation = ((TextView) view.findViewById(R.id.start_location_invisible)).getText().toString();
            String endLocation = ((TextView) view.findViewById(R.id.end_location_invisible)).getText().toString();
            String type = ((TextView) view.findViewById(R.id.type_invisible)).getText().toString();
            String tourNumber = ((TextView) view.findViewById(R.id.tour_number_invisible)).getText().toString();

            // Starting new intent
            Intent in = new Intent(getApplicationContext(),
                    TourInfo.class);
            // sending pid to next activity
            in.putExtra("startLocation", startLocation);
            in.putExtra("endLocation", endLocation);
            in.putExtra("type", type);
            in.putExtra("tourNumber", tourNumber);

            // starting new activity and expecting some response back
            startActivity(in);
        }


    });


}

}

I'm using CursorAdapter for listView,

public class TourNumberListAdapter extends CursorAdapter{

public TourNumberListAdapter(Context context, Cursor cursor) {
    super(context, cursor);
}

@Override
public void bindView(View view, Context context, Cursor cursor) {

    TextView tourNumTextView = (TextView) view.findViewById(R.id.job_id);
    tourNumTextView.setText(cursor.getString(0));
    TextView startLocationInvisibleTextView = (TextView) view.findViewById(R.id.start_location_invisible);
    startLocationInvisibleTextView.setText(cursor.getString(1));
    TextView endLocationInvisibleTextView = (TextView) view.findViewById(R.id.end_location_invisible);
    endLocationInvisibleTextView.setText(cursor.getString(2));
    TextView typeInvisibleTextView = (TextView) view.findViewById(R.id.type_invisible);
    typeInvisibleTextView.setText(cursor.getString(3));
    TextView tourNumberInvisibleTextView = (TextView) view.findViewById(R.id.tour_number_invisible);
    tourNumberInvisibleTextView.setText(cursor.getString(4));

}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
    View view = layoutInflater.inflate(R.layout.tour_list_item,parent,false);
    return view;
}

}

this is my .xml file for listView,

<?xml version="1.0" encoding="UTF-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tourList"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

</ListView>

And this is my Logcat Error,

04-01 14:56:38.003: E/AndroidRuntime(5765): FATAL EXCEPTION: main
04-01 14:56:38.003: E/AndroidRuntime(5765): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tourinfo/com.example.tourinfo.TourNumberList}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
04-01 14:56:38.003: E/AndroidRuntime(5765):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
04-01 14:56:38.003: E/AndroidRuntime(5765):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
04-01 14:56:38.003: E/AndroidRuntime(5765):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-01 14:56:38.003: E/AndroidRuntime(5765):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
04-01 14:56:38.003: E/AndroidRuntime(5765):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-01 14:56:38.003: E/AndroidRuntime(5765):     at android.os.Looper.loop(Looper.java:123)
04-01 14:56:38.003: E/AndroidRuntime(5765):     at android.app.ActivityThread.main(ActivityThread.java:3683)
04-01 14:56:38.003: E/AndroidRuntime(5765):     at java.lang.reflect.Method.invokeNative(Native Method)
04-01 14:56:38.003: E/AndroidRuntime(5765):     at java.lang.reflect.Method.invoke(Method.java:507)
04-01 14:56:38.003: E/AndroidRuntime(5765):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-01 14:56:38.003: E/AndroidRuntime(5765):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-01 14:56:38.003: E/AndroidRuntime(5765):     at dalvik.system.NativeStart.main(Native Method)
04-01 14:56:38.003: E/AndroidRuntime(5765): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
04-01 14:56:38.003: E/AndroidRuntime(5765):     at android.app.ListActivity.onContentChanged(ListActivity.java:243)
04-01 14:56:38.003: E/AndroidRuntime(5765):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:210)
04-01 14:56:38.003: E/AndroidRuntime(5765):     at android.app.Activity.setContentView(Activity.java:1657)
04-01 14:56:38.003: E/AndroidRuntime(5765):     at com.example.tourinfo.TourNumberList.onCreate(TourNumberList.java:20)
04-01 14:56:38.003: E/AndroidRuntime(5765):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-01 14:56:38.003: E/AndroidRuntime(5765):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
04-01 14:56:38.003: E/AndroidRuntime(5765):     ... 11 more

Please help me solving this error.

Was it helpful?

Solution

Try this..

Your using ListActivity then id for your ListView in your layout should be @android:id/list

<?xml version="1.0" encoding="UTF-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

</ListView>

and also Remove ListView listView = (ListView) findViewById(R.id.tourList); then

 // Get listview
ListView lv = getListView();
tourNumberListAdapter = new TourNumberListAdapter(this, cursorTourNumberInfo);
setListAdapter(tourNumberListAdapter);

lv.setOnItemClickListener(new OnItemClickListener() {

OTHER TIPS

Change your listview's id to @android:id/list

So use this

<?xml version="1.0" encoding="UTF-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>

instead of

<?xml version="1.0" encoding="UTF-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tourList"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

</ListView>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top