Application crashes onResume in ICS while working perfect on Gingerbread - android

My main Activity (say A) raises intent to fetch a ContactID from ContactsContract through
startActivityForResult(new Intent(Intent.ACTION_PICK, contactsContract.Contacts.CONTENT_URI),PICK_CONTACT);
While returning (onActivityResult(int reqCode, int resultCode, Intent data)), I am using details from 'data' to query the contacts through
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
......
my code works fine with Android 2.x. However this code gets crashed in ICS (Android 4.x), while resuming the main Activity(A).
I am not sure, whether it is due to the deprecated 'managedQuery'.
Can somebody offer some solution, I am stuck and unable to even find which line causes the error as the Log cat is not showing even the error line number. it simply says
Caused by: android.database.StaleDataException: Attempted to access a cursor after it has been closed.
Thanks in advance
Joshy
I have tried the above code just after completing the requirement of the cursor. Still the problem persists. My Log cat says as follows
08-13 00:26:31.427: W/dalvikvm(3176): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
08-13 00:26:31.447: E/AndroidRuntime(3176): FATAL EXCEPTION: main
08-13 00:26:31.447: E/AndroidRuntime(3176): java.lang.RuntimeException: Unable to resume activity {com.desquare.sp/com.desquare.sp.SmartPadActivity}: android.database.StaleDataException: Attempted to access a cursor after it has been closed.
08-13 00:26:31.447: E/AndroidRuntime(3176): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2444)
08-13 00:26:31.447: E/AndroidRuntime(3176): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2472)
08-13 00:26:31.447: E/AndroidRuntime(3176): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1173)
08-13 00:26:31.447: E/AndroidRuntime(3176): at android.os.Handler.dispatchMessage(Handler.java:99)
08-13 00:26:31.447: E/AndroidRuntime(3176): at android.os.Looper.loop(Looper.java:137)
08-13 00:26:31.447: E/AndroidRuntime(3176): at android.app.ActivityThread.main(ActivityThread.java:4424)
08-13 00:26:31.447: E/AndroidRuntime(3176): at java.lang.reflect.Method.invokeNative(Native Method)
08-13 00:26:31.447: E/AndroidRuntime(3176): at java.lang.reflect.Method.invoke(Method.java:511)
08-13 00:26:31.447: E/AndroidRuntime(3176): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-13 00:26:31.447: E/AndroidRuntime(3176): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-13 00:26:31.447: E/AndroidRuntime(3176): at dalvik.system.NativeStart.main(Native Method)
08-13 00:26:31.447: E/AndroidRuntime(3176): Caused by: android.database.StaleDataException: Attempted to access a cursor after it has been closed.
08-13 00:26:31.447: E/AndroidRuntime(3176): at android.database.BulkCursorToCursorAdaptor.throwIfCursorIsClosed(BulkCursorToCursorAdaptor.java:75)
08-13 00:26:31.447: E/AndroidRuntime(3176): at android.database.BulkCursorToCursorAdaptor.requery(BulkCursorToCursorAdaptor.java:144)
08-13 00:26:31.447: E/AndroidRuntime(3176): at android.database.CursorWrapper.requery(CursorWrapper.java:186)
08-13 00:26:31.447: E/AndroidRuntime(3176): at android.app.Activity.performRestart(Activity.java:4505)
08-13 00:26:31.447: E/AndroidRuntime(3176): at android.app.Activity.performResume(Activity.java:4531)
08-13 00:26:31.447: E/AndroidRuntime(3176): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2434)
08-13 00:26:31.447: E/AndroidRuntime(3176): ... 10 more

It sounds like this is the problem:
*
android.database.staledataexception : Access closed cursor
You seem to be getting the cursors using managedQuery.
I had problems with restoring an application, which for some reason in
Icecream Sandwich did not close the cursors properly. The fix was to
do this for all cursors right after using them:
if (cursor != null && !cursor.isClosed()) {
myActivity.stopManagingCursor( cursor );
cursor.close();
}

The API startManagingCursor/stopManagingCursor is deprecated and is the root cause of the issue.
For example, please comment this out and verify.

Related

Starting a service crashed the Android emulator

I have a quite vanilla service that works seamlessly when the app is executed on my LG device. Yet when I do it on the Eclipse emulator for the Nexus 5 I get a crash, leaving me with the suspect this behavior could also emerge on real devices different from mine. This is the code:
Intent startBackgroundLocationIntent= new Intent(this, BackgroundLocationService.class);
startService(startBackgroundLocationIntent);
and the crash log:
08-13 14:18:10.731: E/AndroidRuntime(6345): FATAL EXCEPTION: main
08-13 14:18:10.731: E/AndroidRuntime(6345): Process:
com.example.taxiprofessional, PID: 6345 08-13 14:18:10.731:
E/AndroidRuntime(6345): java.lang.RuntimeException: Unable to start
service
com.example.taxiprofessional.BackgroundLocationService#b20e39b0 with
Intent { cmp=com.example.taxiprofessional/.BackgroundLocationService
}: java.lang.NullPointerException 08-13 14:18:10.731:
E/AndroidRuntime(6345): at
android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2719)
08-13 14:18:10.731: E/AndroidRuntime(6345): at
android.app.ActivityThread.access$2100(ActivityThread.java:135) 08-13
14:18:10.731: E/AndroidRuntime(6345): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1293)
08-13 14:18:10.731: E/AndroidRuntime(6345): at
android.os.Handler.dispatchMessage(Handler.java:102) 08-13
14:18:10.731: E/AndroidRuntime(6345): at
android.os.Looper.loop(Looper.java:136) 08-13 14:18:10.731:
E/AndroidRuntime(6345): at
android.app.ActivityThread.main(ActivityThread.java:5017) 08-13
14:18:10.731: E/AndroidRuntime(6345): at
java.lang.reflect.Method.invokeNative(Native Method) 08-13
14:18:10.731: E/AndroidRuntime(6345): at
java.lang.reflect.Method.invoke(Method.java:515) 08-13 14:18:10.731:
E/AndroidRuntime(6345): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-13 14:18:10.731: E/AndroidRuntime(6345): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 08-13
14:18:10.731: E/AndroidRuntime(6345): at
dalvik.system.NativeStart.main(Native Method) 08-13 14:18:10.731:
E/AndroidRuntime(6345): Caused by: java.lang.NullPointerException
08-13 14:18:10.731: E/AndroidRuntime(6345): at
com.example.taxiprofessional.BackgroundLocationService.uploadCoordinatesForLocation(BackgroundLocationService.java:100)
08-13 14:18:10.731: E/AndroidRuntime(6345): at
com.example.taxiprofessional.BackgroundLocationService.onStartCommand(BackgroundLocationService.java:138)
08-13 14:18:10.731: E/AndroidRuntime(6345): at
android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2702)
08-13 14:18:10.731: E/AndroidRuntime(6345): ... 10 more
Reading that stack trace, it looks like the code that's crashing is your upload method:
E/AndroidRuntime(6345): Caused by: java.lang.NullPointerException 08-13 14:18:10.731: E/AndroidRuntime(6345): at com.example.taxiprofessional.BackgroundLocationService.uploadCoordinatesForLocation(BackgroundLocationService.java:100)
Just speculating here, but have you validated that you're null checking the coordinates that you're trying to upload? Your emulator won't have a location unless you configure mock locations correctly.
I fixed the whole issue by checking if variable last was null before calling the function and by requesting the best provider; this is the code snippet I hope to be of use for beginners like me:
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
String provider = lm.getBestProvider(criteria, true);
if (lm!=null) {
Location last=lm.getLastKnownLocation(provider);
if (last!=null) uploadCoordinatesForLocation(last);
lm.requestLocationUpdates(provider, 0, 0, locationListener);
}
Thanks everyone.

Retrieving video uri at particular row and column from sqllite database

This is project for video recorder.The only problem I am having is retrieving stored uri of video from sqllite.The project works fine if I don't call retrieveVid() method but it crashes out when retrieveVid() method is called.Here I am trying to retrieve the uri stored at column 1 and row 2 of sqllite database.Inside checkEvents() method I have commented the other codes which I have tried . Problem might be in checkEvents() or getEvents() method or retrieveVid() method.Since I am running the project on mobile as camera doesn't work on my emulator so i don't have logcat.I am not posting SecondActvity.java as it is not related to database or my problem.
logcat
06-25 21:36:24.501: E/CursorWindow(23206): Failed to read row 2, column 1 from a CursorWindow which has 123 rows, 1 columns.
06-25 21:36:24.504: D/AndroidRuntime(23206): Shutting down VM
06-25 21:36:24.504: W/dalvikvm(23206): threadid=1: thread exiting with uncaught exception (group=0x40e89908)
06-25 21:36:24.514: E/AndroidRuntime(23206): FATAL EXCEPTION: main
06-25 21:36:24.514: E/AndroidRuntime(23206): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=200, result=-1, data=Intent { dat=file:///storage/sdcard0/Pictures/Hello Camera/VID_20140625_213611.mp4 }} to activity {com.example.Assignment/com.example.Assignment.MainActivity}: java.lang.IllegalStateException: Couldn't read row 2, col 1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
06-25 21:36:24.514: E/AndroidRuntime(23206): at android.app.ActivityThread.deliverResults(ActivityThread.java:3302)
06-25 21:36:24.514: E/AndroidRuntime(23206): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3345)
06-25 21:36:24.514: E/AndroidRuntime(23206): at android.app.ActivityThread.access$1100(ActivityThread.java:149)
06-25 21:36:24.514: E/AndroidRuntime(23206): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1348)
06-25 21:36:24.514: E/AndroidRuntime(23206): at android.os.Handler.dispatchMessage(Handler.java:99)
06-25 21:36:24.514: E/AndroidRuntime(23206): at android.os.Looper.loop(Looper.java:153)
06-25 21:36:24.514: E/AndroidRuntime(23206): at android.app.ActivityThread.main(ActivityThread.java:5086)
06-25 21:36:24.514: E/AndroidRuntime(23206): at java.lang.reflect.Method.invokeNative(Native Method)
06-25 21:36:24.514: E/AndroidRuntime(23206): at java.lang.reflect.Method.invoke(Method.java:511)
06-25 21:36:24.514: E/AndroidRuntime(23206): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
06-25 21:36:24.514: E/AndroidRuntime(23206): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
06-25 21:36:24.514: E/AndroidRuntime(23206): at dalvik.system.NativeStart.main(Native Method)
06-25 21:36:24.514: E/AndroidRuntime(23206): Caused by: java.lang.IllegalStateException: Couldn't read row 2, col 1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
06-25 21:36:24.514: E/AndroidRuntime(23206): at android.database.CursorWindow.nativeGetString(Native Method)
06-25 21:36:24.514: E/AndroidRuntime(23206): at android.database.CursorWindow.getString(CursorWindow.java:434)
06-25 21:36:24.514: E/AndroidRuntime(23206): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
06-25 21:36:24.514: E/AndroidRuntime(23206): at com.example.Assignment.MainActivity.checkEvents(MainActivity.java:367)
06-25 21:36:24.514: E/AndroidRuntime(23206): at com.example.Assignment.MainActivity.retrieveVid(MainActivity.java:374)
06-25 21:36:24.514: E/AndroidRuntime(23206): at com.example.Assignment.MainActivity.onActivityResult(MainActivity.java:321)
06-25 21:36:24.514: E/AndroidRuntime(23206): at android.app.Activity.dispatchActivityResult(Activity.java:5204)
06-25 21:36:24.514: E/AndroidRuntime(23206): at android.app.ActivityThread.deliverResults(ActivityThread.java:3298)
06-25 21:36:24.514: E/AndroidRuntime(23206): ... 11 more
Column indexing starts at 0. Your table has only 1 column. You don't specify a projection so all columns are returned in the cursor. Attempting to retrieve the value at column index 1 causes the exception.
To fix it, change getString(1) to getString(0).
First off, you try to read the second row of your cursor without checking if it is there. Also, like #laalto said, the columns on the cursor are zero-based, so with ´getString(1)´ you won't get anything, because the table has only one column.
I'd recommend you to get the String like this, this will be save, even if you add further rows to your Table:
if(cursor.moveToPosition(2)){
a = cursor.getString(cursor.getColumnIndex(sql.COLUMN_VID));
// add a constant (COLUMN_VID) to your sql class which defines the row name and also use this constant in your table creation
}

Android voice recording crashes (straight from Docs)

I've taken the code from Android's own Audio Capture documentation and ran it to achieve just what the docs say. As is, the code crashes when I click the "Start Recording" button and the crash says the following:
FATAL EXCEPTION: main
java.lang.IllegalStateException
at android.media.MediaRecorder.start(Native Method)
**at com.om.shout_o_meter.MainActivity.startRecording(MainActivity.java:130)
at com.om.shout_o_meter.MainActivity.onRecord(MainActivity.java:111)
at com.om.shout_o_meter.MainActivity.access$0(MainActivity.java:109)
at com.om.shout_o_meter.MainActivity$1.onClick(MainActivity.java:38)
at android.view.View.performClick(View.java:4211)
at android.view.View$PerformClick.run(View.java:17267)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4898)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
at dalvik.system.NativeStart.main(Native Method)
If I place AudioRecordTest() at the very beginning of the startRecording() method, the crash ceases until I finish the first recording and then press "Start Recording" again, the crash reappears with the following message:
FATAL EXCEPTION: main
java.lang.NullPointerException
at com.om.shout_o_meter.MainActivity.stopRecording(MainActivity.java:135)
at com.om.shout_o_meter.MainActivity.onRecord(MainActivity.java:113)
at com.om.shout_o_meter.MainActivity.access$0(MainActivity.java:109)
at com.om.shout_o_meter.MainActivity$1.onClick(MainActivity.java:38)
at android.view.View.performClick(View.java:4211)
at android.view.View$PerformClick.run(View.java:17267)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4898)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
at dalvik.system.NativeStart.main(Native Method)
The code I'm using is the exact same code as that of the docs (linked above), and the "MainActivity" file that the exception is pointing to can be found here: http://pastie.org/8682455
Any clue? what baffles me is that the code is pasted as is, straight from the docs, and still it fails..
Thanks

Android app running on Emulator but not on device_getting stackoverflow error

When I am trying to open a tabactivity it is running fine on emulator but not on real device.
the min and max sdk in manifet file as shown
android:minSdkVersion="8"
android:targetSdkVersion="17"
And my device is samsung tablet having 4.1.1 version
08-13 17:53:42.034: E/AndroidRuntime(22602): FATAL EXCEPTION: main
08-13 17:53:42.034: E/AndroidRuntime(22602): java.lang.StackOverflowError
08-13 17:53:42.034: E/AndroidRuntime(22602): at android.content.res.Resources.obtainAttributes(Resources.java:1393)
08-13 17:53:42.034: E/AndroidRuntime(22602): at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:117)
08-13 17:53:42.034: E/AndroidRuntime(22602): at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:881)
08-13 17:53:42.034: E/AndroidRuntime(22602): at android.graphics.drawable.Drawable.createFromXml(Drawable.java:818)
08-13 17:53:42.034: E/AndroidRuntime(22602): at android.content.res.Resources.loadDrawable(Resources.java:1948)
08-13 17:53:42.034: E/AndroidRuntime(22602): at android.content.res.Resources.getDrawable(Resources.java:672)
08-13 17:53:42.034: E/AndroidRuntime(22602): at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:173)
08-13 17:53:42.034: E/AndroidRuntime(22602): at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:881)
08-13 17:53:42.034: E/AndroidRuntime(22602): at android.graphics.drawable.Drawable.createFromXml(Drawable.java:818)
08-13 17:53:42.034: E/AndroidRuntime(22602): at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:881)
08-13 17:53:42.034: E/AndroidRuntime(22602): at android.graphics.drawable.Drawable.createFromXml(Drawable.java:818)
08-13 17:53:42.034: E/AndroidRuntime(22602): at android.content.res.Resources
TabActivity was deprecated in API 13, so it is not guaranteed to work correctly on Android 3.2 or later. Consider using Fragments instead.
Stack overflow errors typically happen when a recursive function doesn't have a proper base case. In other words, the recursive function will keep calling itself, thus adding it's variables onto the stack until the stack space is gone. If you are using any recursive functions, be sure you have a proper base case. If you aren't using any recursive functions, something probably went wrong in the deprecated TabActivity class.

Selecting an item in ListFragment list programmatically

I've made my Android app tablet optimized and I followed the tutorial here:
Everything is great, but I am trying to select an item (say, the 1st item) with a button in the ActionBar.
I tried this answer to use performItemClick but on I've got error reports of java.lang.IllegalStateException
in android.support.v4.app.ListFragment.ensureList, java.lang.IllegalStateException: Content view not yet created, and java.lang.NullPointerException
in android.content.ComponentName.<init>
I've tried checking if the ListView is null and still get the error reports on the Play Store. How do I properly select an item in my list programmatically?
Update to add logcat and the code is virtually identical to the tutorials in the links:
Logcat A:
java.lang.IllegalStateException: Content view not yet created
at android.support.v4.app.ListFragment.ensureList(ListFragment.java:328)
at android.support.v4.app.ListFragment.getListView(ListFragment.java:222)
at com.ccwilcox.meteorshower.MeteorList.showMeteorDetails(MeteorList.java:69)
at com.ccwilcox.meteorshower.MeteorList.onListItemClick(MeteorList.java:62)
at android.support.v4.app.ListFragment$2.onItemClick(ListFragment.java:58)
at android.widget.AdapterView.performItemClick(AdapterView.java:298)
at android.widget.AbsListView.performItemClick(AbsListView.java:1280)
at com.ccwilcox.meteorshower.MainActivity.viewUpcomingEvent(MainActivity.java:648)
at com.ccwilcox.meteorshower.MainActivity.onOptionsItemSelected(MainActivity.java:534)
at android.app.Activity.onMenuItemSelected(Activity.java:2606)
at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:361)
at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:1045)
at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:592)
at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:149)
at android.view.View.performClick(View.java:4222)
at android.view.View$PerformClick.run(View.java:17273)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4895)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
at dalvik.system.NativeStart.main(Native Method)
Logcat B:
java.lang.NullPointerException
at android.content.ComponentName.<init>(ComponentName.java:75)
at android.content.Intent.<init>(Intent.java:2874)
at com.ccwilcox.meteorshower.MeteorList.showMeteorDetails(MeteorList.java:86)
at com.ccwilcox.meteorshower.MeteorList.onListItemClick(MeteorList.java:62)
at android.support.v4.app.ListFragment$2.onItemClick(ListFragment.java:58)
at android.widget.AdapterView.performItemClick(AdapterView.java:284)
at android.widget.ListView.performItemClick(ListView.java:3701)
at com.ccwilcox.meteorshower.MainActivity.viewUpcomingEvent(MainActivity.java:648)
at com.ccwilcox.meteorshower.MainActivity.onOptionsItemSelected(MainActivity.java:534)
at android.app.Activity.onMenuItemSelected(Activity.java:2205)
at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:361)
at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:779)
at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:143)
at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:861)
at com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:532)
at com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:122)
at android.view.View$PerformClick.run(View.java:9152)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
Update 2
And here is the code that is causing the problem:
if (mListFragment.listView != null) {
mListFragment.listView.performItemClick(mListFragment.listView.getAdapter().getView(position, null, null), position, mListFragment.listView.getAdapter().getItemId(position));
}

Categories

Resources