Working on my first Android app and facing a challenge.
I am pulling calls from CallLog contentprovider and displaying using a cursor adapter.
I have overriden bindView and newView. I am not using SimpleCursorAdapter as I need to format the date. I am new to Android so prone to silly mistakes. 🙂
I am calling this cursorAdapter in the main Activity using this code.
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;
}
But my app crashes before it starts.
Edited – Heres the error log.
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
Regards,
Aman
In your
MissedCallsAdapteryou seem to call the cursor in the constructor long before the adapter is really initializedCheck line 24 of your adapter code. Or rather line 27 where this call to the constructor happens in
onCreate.