0

I am trying to display multiple columns of a database inside a listview. i am using sqliteasset helper and a row.xml and main.xml. my mainactivity extends a listactivity. The error i'm getting is that the findviewbyid can't find a linear layout with "R.id.list" but my activity_main.xml has it.

this is my main class that displays the database info on the list.

public class MainActivity extends ListActivity {

    private Cursor schedule;
    private MyDatabase db;
    ListView listView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        db = new MyDatabase(this);
        schedule = db.getSchedule();

        ListView listView = (ListView) findViewById(android.R.id.list);
        listView.setAdapter(new SimpleCursorAdapter(
           this,  // The context
           R.layout.row,  // The layout of the row to show
           db.getSchedule(),  // Here the cursor where the data is
                               // but it can be anything
           new String[] {"fName", "fType" },  // here the tables to show
           new int[] { android.R.id.text1, android.R.id.text2 },  // here where to show? the IDs
                                             // of the layout elements where put data.
           0));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        schedule.close();
        db.close();
    }
}

Row:

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp"
        android:text="TextView"
        android:textColor="#0000FF"
        android:textSize="25dp" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp"
        android:text="TextView"
        android:textColor="#0000FF"
        android:textSize="25dp" />

</LinearLayout>

Activity_Main: (you can see the id.list)

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

LogCat:

12-02 01:04:44.436: E/AndroidRuntime(1183): FATAL EXCEPTION: main
12-02 01:04:44.436: E/AndroidRuntime(1183): Process: com.example.mealplan, PID: 1183
12-02 01:04:44.436: E/AndroidRuntime(1183): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mealplan/com.example.mealplan.MainActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
12-02 01:04:44.436: E/AndroidRuntime(1183):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
12-02 01:04:44.436: E/AndroidRuntime(1183):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
12-02 01:04:44.436: E/AndroidRuntime(1183):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
12-02 01:04:44.436: E/AndroidRuntime(1183):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
12-02 01:04:44.436: E/AndroidRuntime(1183):     at android.os.Handler.dispatchMessage(Handler.java:102)
12-02 01:04:44.436: E/AndroidRuntime(1183):     at android.os.Looper.loop(Looper.java:136)
12-02 01:04:44.436: E/AndroidRuntime(1183):     at android.app.ActivityThread.main(ActivityThread.java:5017)
12-02 01:04:44.436: E/AndroidRuntime(1183):     at java.lang.reflect.Method.invokeNative(Native Method)
12-02 01:04:44.436: E/AndroidRuntime(1183):     at java.lang.reflect.Method.invoke(Method.java:515)
12-02 01:04:44.436: E/AndroidRuntime(1183):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
12-02 01:04:44.436: E/AndroidRuntime(1183):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
12-02 01:04:44.436: E/AndroidRuntime(1183):     at dalvik.system.NativeStart.main(Native Method)
12-02 01:04:44.436: E/AndroidRuntime(1183): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
12-02 01:04:44.436: E/AndroidRuntime(1183):     at android.app.ListActivity.onContentChanged(ListActivity.java:243)
12-02 01:04:44.436: E/AndroidRuntime(1183):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:293)
12-02 01:04:44.436: E/AndroidRuntime(1183):     at android.app.Activity.setContentView(Activity.java:1929)
12-02 01:04:44.436: E/AndroidRuntime(1183):     at com.example.mealplan.MainActivity.onCreate(MainActivity.java:35)
12-02 01:04:44.436: E/AndroidRuntime(1183):     at android.app.Activity.performCreate(Activity.java:5231)
12-02 01:04:44.436: E/AndroidRuntime(1183):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-02 01:04:44.436: E/AndroidRuntime(1183):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
Snoballin
  • 7
  • 4

2 Answers2

0

Change this line :

android:id="@+id/list"

to

android:id="@id/android:list"
Arash GM
  • 10,316
  • 6
  • 58
  • 76
  • my program no longer crashes and it shows two "text views" next to eachother! next step is to make them say the right stuff. Why do i need to change that manually? like how is it different, so i don't make this mistake again. – Snoballin Dec 02 '14 at 06:24
  • ListActivity needs a predefined id which defined in android R class. you defined an extra id with @+id which ListActivity doesn't recognize it as a proper id. – Arash GM Dec 02 '14 at 06:29
0

You are trying to fetch list id from android default library. You should retrieve list Id from your own layout.

Change This line

ListView listView = (ListView) findViewById(android.R.id.list);

to

ListView listView = (ListView) findViewById(R.id.list);
Amy
  • 4,034
  • 1
  • 20
  • 34