2012-02-09 87 views
0

處理我的第一個Android應用程序並面臨挑戰。 我從CallLog內容提供者拉動呼叫並使用遊標適配器顯示。 我重寫了bindView和newView。我沒有使用SimpleCursorAdapter,因爲我需要格式化日期。我是Android的新手,所以很容易出現愚蠢的錯誤。 :) 我使用此代碼在主Activity中調用此cursorAdapter。應用程序崩潰 - 擴展CursorAdapter

startManagingCursor(calls); 
adapter = new CallsAdapter(this, calls); 
setListAdapter(adapter); 

public void bindView(View view, Context context, Cursor cursor) { 
    displayDate(view, date); 
} 

private void displayDate(View view, String strDate) { 
    final long timestamp = Long.parseLong(strDate); 
    final String formattedDate = DATEFORMAT.format(new Date(timestamp)); 
    final TextView t = (TextView) view.findViewById(R.id.date_text); 
    t.setText(formattedDate); 
} 

public View newView(Context context, Cursor cursor, ViewGroup parent) { 
    final View view = LayoutInflater.from(context).inflate(R.layout.scratch_layout2, parent, false); 
    return view; 
} 

但它開始之前我的應用程序崩潰。

已編輯 - 繼承人錯誤日誌。

ERROR/AndroidRuntime(12885): Uncaught handler: thread main exiting due to uncaught exception 
02-09 06:05:28.281: ERROR/AndroidRuntime(12885): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.amannain.android.missedcalls/com.amannain.android.missedcalls.MissedCallsActivity}: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 6 
02-09 06:05:28.281: ERROR/AndroidRuntime(12885):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2268) 
02-09 06:05:28.281: ERROR/AndroidRuntime(12885):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2284) 
02-09 06:05:28.281: ERROR/AndroidRuntime(12885):  at android.app.ActivityThread.access$1800(ActivityThread.java:112) 
02-09 06:05:28.281: ERROR/AndroidRuntime(12885):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1692) 
02-09 06:05:28.281: ERROR/AndroidRuntime(12885):  at android.os.Handler.dispatchMessage(Handler.java:99) 
02-09 06:05:28.281: ERROR/AndroidRuntime(12885):  at android.os.Looper.loop(Looper.java:123) 
02-09 06:05:28.281: ERROR/AndroidRuntime(12885):  at android.app.ActivityThread.main(ActivityThread.java:3948) 
02-09 06:05:28.281: ERROR/AndroidRuntime(12885):  at java.lang.reflect.Method.invokeNative(Native Method) 
02-09 06:05:28.281: ERROR/AndroidRuntime(12885):  at java.lang.reflect.Method.invoke(Method.java:521) 
02-09 06:05:28.281: ERROR/AndroidRuntime(12885):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782) 
02-09 06:05:28.281: ERROR/AndroidRuntime(12885):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540) 
02-09 06:05:28.281: ERROR/AndroidRuntime(12885):  at dalvik.system.NativeStart.main(Native Method) 
02-09 06:05:28.281: ERROR/AndroidRuntime(12885): Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 6 
02-09 06:05:28.281: ERROR/AndroidRuntime(12885):  at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580) 
02-09 06:05:28.281: ERROR/AndroidRuntime(12885):  at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:172) 
02-09 06:05:28.281: ERROR/AndroidRuntime(12885):  at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41) 
02-09 06:05:28.281: ERROR/AndroidRuntime(12885):  at android.database.CursorWrapper.getString(CursorWrapper.java:135) 
02-09 06:05:28.281: ERROR/AndroidRuntime(12885):  at com.amannain.android.missedcalls.MissedCallsAdapter.<init>(MissedCallsAdapter.java:24) 
02-09 06:05:28.281: ERROR/AndroidRuntime(12885):  at com.amannain.android.missedcalls.MissedCallsActivity.onCreate(MissedCallsActivity.java:27) 
02-09 06:05:28.281: ERROR/AndroidRuntime(12885):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123) 
02-09 06:05:28.281: ERROR/AndroidRuntime(12885):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231) 
02-09 06:05:28.281: ERROR/AndroidRuntime(12885):  ... 11 more 

問候, 阿曼

+1

請定義「崩潰」。如果您在LogCat中遇到異常,請將其添加到您的問題中。 – 2012-02-09 11:54:32

+1

你在控制檯/ logcat /調試器中遇到什麼錯誤 – tipycalFlow 2012-02-09 11:55:32

+0

繼承人錯誤日誌。 – Harry 2012-02-09 12:24:56

回答

0

在你MissedCallsAdapter你似乎調用光標在構造函數中的適配器是真正初始化

Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 6 
at android.database.CursorWrapper.getString(CursorWrapper.java:135) 
at com.amannain.android.missedcalls.MissedCallsAdapter.<init>(MissedCallsAdapter.java:24) 
at com.amannain.android.missedcalls.MissedCallsActivity.onCreate(MissedCallsActivity.java:27) 

檢查你的適配器代碼行24很久以前。或者更確切地說,第27行是對onCreate中構造函數的調用。

+0

感謝您編輯我的錯誤日誌並指出問題。我在遊標適配器的構造方法中初始化我的變量時犯了一個愚蠢的錯誤。它的工作現在。 :) – Harry 2012-02-10 12:53:38