开发者

Error android database

开发者 https://www.devze.com 2023-03-19 02:05 出处:网络
There is no error in eclipse but when I run it is \"forced to stop\" with the following exception: Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is \'and

There is no error in eclipse but when I run it is "forced to stop" with the following exception:

Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'

here the LogCat output

07-11 11:09:45.242: WARN/dalvikvm(590): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
07-11 11:09:45.303: ERROR/AndroidRuntime(590): FATAL EXCEPTION: main
07-11 11:09:45.303: ERROR/AndroidRuntime(590): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.example.events/org.example.events.Events}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
07-11 11:09:45.303: ERROR/AndroidRuntime(590):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
07-11 11:09:45.303: ERROR/AndroidRuntime(590):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-11 11:09:45.303: ERROR/AndroidRuntime(590):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-11 11:09:45.303: ERROR/AndroidRuntime(590):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-11 11:09:45.303: ERROR/AndroidRuntime(590):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-11 11:09:45.303: ERROR/AndroidRuntime(590):     at android.os.Looper.loop(Looper.java:123)
07-11 11:09:45.303: ERROR/AndroidRuntime(590):     at android.app.ActivityThread.main(ActivityThread.java:4627)
07-11 11:09:45.303: ERROR/AndroidRuntime(590):     at java.lang.reflect.Method.invokeNative(Native Method)
07-11 11:09:45.303: ERROR/AndroidRuntime(590):     at java.lang.reflect.Method.invoke(Method.java:521)
07-11 11:09:45.303: ERROR/AndroidRuntime(590):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-11 11:09:45.303: ERROR/AndroidRuntime(590):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-11 11:09:45.303: ERROR/AndroidRuntime(590):     at dalvik.system.NativeStart.main(Native Method)
07-11 11:09:45.303: ERROR/AndroidRuntime(590): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
07-11 11:09:45.303: ERROR/AndroidRuntime(590):     at android.app.ListActivity.onContentChanged(ListActivity.java:245)
07-11 11:09:45.303: ERROR/AndroidRuntime(590):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:201)
07-11 11:09:45.303: ERROR/AndroidRuntime(590):     at android.app.Activity.setContentView(Activity.java:1647)
07-11 11:09:45.303: ERROR/AndroidRuntime(590):     at org.example.events.Events.onCreate(Events.java:23)
07-11 11:09:45.303: ERROR/AndroidRuntime(590):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-11 11:09:45.303: ERROR/AndroidRuntime(590):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
07-11 11:09:45.303: ERROR/AndroidRuntime(590):     ... 11 more

This is my code in eventsActivity

package org.example.events;

//import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import static android.provider.BaseColumns._ID;
import static org.example.events.Constants.TABLE_NAME;
import static org.example.events.Constants.TIME;
import st开发者_运维知识库atic org.example.events.Constants.TITLE;
//import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
//import android.os.Bundle;
import android.widget.TextView;

public class Events extends ListActivity {
    /** Called when the activity is first created. */
    private EventsData events;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        events = new EventsData(this);
        try {
            addEvent("Hello, Android!");
            Cursor cursor = getEvents();
            showEvents(cursor);
        } finally {
            events.close();
        }
    }

    private void addEvent(String string) {
        // Insert a new record into the Events data source.
        // You would do something similar for delete and update.
        SQLiteDatabase db = events.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(TIME, System.currentTimeMillis());
        values.put(TITLE, string);
        db.insertOrThrow(TABLE_NAME, null, values);
    }

    private static String[] FROM = { _ID, TIME, TITLE, };
    private static String ORDER_BY = TIME + " DESC";
    private Cursor getEvents() {
        // Perform a managed query. The Activity will handle closing
        // and re-querying the cursor when needed.
        SQLiteDatabase db = events.getReadableDatabase();
        Cursor cursor = db.query(TABLE_NAME, FROM, null, null, null,
        null, ORDER_BY);
        startManagingCursor(cursor);
        return cursor;
    }

    private void showEvents(Cursor cursor) {
        // Stuff them all into a big string
        StringBuilder builder = new StringBuilder("Saved events:\n");
        while (cursor.moveToNext()) {
            // Could use getColumnIndexOrThrow() to get indexes
            long id = cursor.getLong(0);
            long time = cursor.getLong(1);
            String title = cursor.getString(2);
            builder.append(id).append(": ");
            builder.append(time).append(": ");
            builder.append(title).append("\n");
        }
        // Display on the screen
        TextView text = (TextView) findViewById(R.id.text);
        text.setText(builder);
}   

    /*private static int[] TO = { R.id.rowid, R.id.time, R.id.title, };
    private void showEvents(Cursor cursor) {
        // Set up data binding
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.item, cursor, FROM, TO);
        setListAdapter(adapter);
    }*/
}

Here is event manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="org.example.events"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Events"
              android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".Constants"
                  android:label="@string/app_name">

        </activity>

        <activity android:name=".EventsData"
                  android:label="@string/app_name">

        </activity>

    </application>
</manifest>

Here is my layout main.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:id="@+id/text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</ScrollView>


Right at the top of your logcat:

Unable to start activity ...
Your content must have a ListView whose id attribute is 'android.R.id.list'

Does your activity have a ListView with that id?


In addition to having an element with an ID of 'list' you can/should include in your Layout XML file something with an ID of 'empty' - If you do, Android wil display whatever you define as 'empty' when there is nothing to display in the ListView.

0

精彩评论

暂无评论...
验证码 换一张
取 消