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)