2011-07-11 66 views
1

有在eclipse沒有錯誤,但是當我運行它是「逼停」,以下情況例外:錯誤的Android數據庫

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

這裏的logcat的輸出

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 

這是我的代碼在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 static 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); 
    }*/ 
} 

這裏是事件清單

<?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> 

這裏是我的佈局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> 
+1

你怎麼想象沒有你的代碼?請提供一些代碼... – barmaley

回答

0

就在您的logcat的頂部:

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

您是否有活動與ID一個ListView?

+0

我仍然不明白ñ我提供你的代碼thx – sayvortana

+1

@sayv後悔你提供的代碼不包括相關部分。請從引發錯誤的活動中提供您的佈局XML。 – Farray

+0

thx所有幫助它解決:) – sayvortana

0

除了有一個ID爲'list'的元素,您可以/應該在Layout XML文件中包含ID爲'empty'的東西 - 如果這樣做,Android會顯示您定義爲'empty '當ListView中沒有顯示時。