Can an activity loose its intent extras? - java

The app crashes in setParametersFromIntent which is called in onCreate of the activity. The method throws a custom RumtimeException if a specific intent extra is not set.
The crash is only reported rarely, I was not able to reproduce the bug.
I checked the code and there seems to be no possible route the activity could be started without the parameter being set.
Caused by java.lang.RuntimeException: intent extra not set
at com.myapp.myapp.activities.MyActivity.setParametersFromIntent(MyActivity.java)
at com.myapp.myapp.activities.MyActivity.onCreate(MyActivity.java)
at android.app.Activity.performCreate(Activity.java:5442)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2393)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493)
at android.app.ActivityThread.access$800(ActivityThread.java:166)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5584)
at java.lang.reflect.Method.invokeNative(Method.java)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(NativeStart.java)
I read that upon recreation of an activity, the intent extras are also restored, so I rule that out as a possible cause.
Does the log give any hints about how the activity has been started?
The android.app.Instrumentation.callActivityOnCreate also seems suspicious, as instrumentation should be disabled in the released app. Could this be someone trying to analyze and mess around with the activity, causing this exception?

Related

DialogFragment is relaunching itself when startActivity on new Intent is called

In my Android application I have a dialog fragment that contains a ListView. The onclick handler of the items in the list view calls my internalPlayer method which calls a VideoView activity to play a video. The VideoView activity is used in other places within the app so I know it is working.
private void internalPlayer(final String channelUrl)
{
final Intent localIntent = new Intent(getActivity(), InternalVideoActivity.class);
localIntent.putExtra(EXTRA_VIDEO_URI, Uri.encode(channelUrl, ALLOWED_URI_CHARS).toString());
localIntent.putExtra(GridViewFragment.CHANNEL_NAME, mCategory.toString() + " Episode ");
getActivity().startActivity(localIntent);
}
When running the app in a tablet everything works well and the video activity starts and plays the video. When I run the app on a phone device when I click an item in the ListView the dialog fragment relaunches itself and because the serialized objects originally passed to it no longer exist there is a NullPointerException throw.
Here's the log:
04-16 19:34:33.790 6000-6000/com.xystra W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41998700)
04-16 19:34:33.790 6000-6000/com.xystra E/MAIN PHONE ACTIVITY: 1﹕ Unable to start activity ComponentInfo{com.xystra/com.xystra.activity.CatchUpSynopsisActivity}: java.lang.NullPointerException
04-16 19:34:33.814 6000-6000/com.xystra E/MAIN PHONE ACTIVITY﹕ STACK TRACE
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xystra/com.xystra.activity.CatchUpSynopsisActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3740)
at android.app.ActivityThread.access$700(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.xystra.ui.fragment.ProgramFragment.onCreateView(ProgramFragment.java:369)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1108)
at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1917)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:544)
at roboguice.activity.RoboFragmentActivity.onStart(RoboFragmentActivity.java:60)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
at android.app.Activity.performStart(Activity.java:5143)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
            at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3740)
            at android.app.ActivityThread.access$700(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
Any help or ideas are greatly appreciated.
More Info
The dialog fragment loads properly. The listview is populated everything is good.
When I click an item in the listview my internalPlayer method is called. When getActivity().startActivity(localIntent) is executed the dialog fragment relaunches itself instead of launching the new intent/activity.
Because the fragment is relaunching itself, the serialized objects that were originally passed the first time are not available or null so the dialog fragment returns the exception you see above.
Everything works perfectly on a tablet. Only on phone devices do I get this event.
Well I got rid of the dialog fragment and used the existing activity fragment for my view, ran some tests and was able to deduce that I had an arithmetic exception in an underlying activity for some reason that was causing the issue. Really don't know how that method got recalled to cause the exception. Thanks for all your help to those who commented.

Android detect crash

I am getting this crash log for my application:
java.lang.IllegalStateException
android.widget.ListView.layoutChildren(ListView.java:1544)
android.widget.AbsListView.onTouchModeChanged(AbsListView.java:3543)
android.view.ViewTreeObserver.dispatchOnTouchModeChanged(ViewTreeObserver.java:732)
android.view.ViewRootImpl.ensureTouchModeLocally(ViewRootImpl.java:3170)
android.view.ViewRootImpl.ensureTouchMode(ViewRootImpl.java:3154)
android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3397)
android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3347)
android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4456)
android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4434)
android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4538)
android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
android.os.MessageQueue.nativePollOnce(Native Method)
android.os.MessageQueue.next(MessageQueue.java:125)
android.os.Looper.loop(Looper.java:124)
android.app.ActivityThread.main(ActivityThread.java:4921)
java.lang.reflect.Method.invokeNative(Native Method)
java.lang.reflect.Method.invoke(Method.java:511)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
dalvik.system.NativeStart.main(Native Method)
But i can't see in this lines nothing that point to my code. any idea how i can detect the crash?
The way to get an IllegalStateException in ListView.layoutChildren() is to make the adapter item count differ from what the listview expected.
This can happen if you changed the data in your listview adapter but forgot to call notifyDataSetChanged(), or modified the data in a background thread.

Starting Android activity gives NullPointerException

I have an activity, LookingForGPS, that starts another activity, Run. After starting Run, LookingForGPS doesn't finish, but rather continually updates some TextViews in Run:
Run run = new Run();
if(runHasBeenStarted)run.getAndSetValues(someParameters, this);
To my understanding, this returns the LookingForGPS activity. However, after a while (when the parameters are a certain value) I want to start a new activity, PostRun. The following method is called from the Run instance's getAndSetValues:
private void killEverythingAndProceed(Context context){
Intent finishRun = new Intent(context, PostRun.class);
//putting some extras into the intent
startActivity(finishRun);
}
I then get a NullPointerException at the line of startActivity:
11-17 08:34:33.647 2472-2472/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: lv.rv1g.kj0112.forward, PID: 2472
java.lang.NullPointerException
at android.app.Activity.startActivityForResult(Activity.java:3424)
at android.app.Activity.startActivityForResult(Activity.java:3385)
at android.app.Activity.startActivity(Activity.java:3627)
at android.app.Activity.startActivity(Activity.java:3595)
at lv.rv1g.kj0112.forward.Run.killEverythingAndProceed(Run.java:143)
at lv.rv1g.kj0112.forward.Run.getAndSetValues(Run.java:88)
at lv.rv1g.kj0112.forward.LookingForGPS.onLocationChanged(LookingForGPS.java:100)
at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:279)
at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:208)
at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:224)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
What am I doing horribly wrong? I guess it's my lack of knowledge about object-oriented programming that's causing this. It's not the extras causing it, and the context should be correct as well, as it works for another method from the same instance.
Edit: detailed explanation
The LookingForGPS activity has a LocationManager (not using a service because it has some limitations that are too complicated for me to work around). Each time onLocationChanged is called, the Run activity is instantiated (I check whether it has actually been started first, it gets started by the user independetly of the LocationManager), and the getAndSetValues method inside Run is called, giving a bunch of parameters. getAndSetValues then performs some calculations and updates some TextViews in the Run activity (which all works fine). A calculation is also performed to see whether a certain condition has been met. If it has been, the postRun activity is supposed to be started, which is when I run into the exception above.
Here's the problem:
Run run = new Run();
Looks like Run is an Activity. Never instantiate activities with new - you cannot use activities instantiated such way for anything you'd want to use an activity for. For example, to be used as a Context for startActivity().
Either use an Activity instance that has been set up by the system for you, or use an Intent to launch a new activity instance.

Force close when app restarts

I really don't know how to start this, but I have searched through the whole of Google and stackoverflow.
I have this radio app which plays radio(duh) and display information about the song playing currently. But I have a problem here:
I have an Activity(MainActivity) and 3 Fragments(playerFragment, the rest are not important) and the fragments are tabs.
I parse the JSON data on MainActivity and display it on playerFragment. I am able to display it straight, no problems. But when I press the back the button and open the app again, it crashes immediately. Of course, I have a service playing the audio only and I ensure that the app loads the data when the user returns to the app(in this case the Activity and fragment is destroyed while service is still running). I checked the log it stated NullPointerException on the point when it starts to setText onCreate. I use the exact same code as it was to load the data at first (before destroying the Activity and Fragment).
I don't know how am I gonna post the code here because I do not know where went wrong and the code was long, please tell me which part of the code you would want to see to check what's wrong.
Code where it start to have error:
TextView titleTextView = (TextView) findViewById(R.id.titleTextView);
TextView artistTextView = (TextView) findViewById(R.id.artistTextView);
TextView lyricsTextView = (TextView) findViewById(R.id.lyricsTextView);
ImageView albumImageView = (ImageView) findViewById(R.id.albumImageView);
JSONObject jsonObject = new JSONObject(jsonData);
String title = jsonObject.getString("title");
String artist = jsonObject.getString("artist");
String lyrics = jsonObject.getString("lyrics");
String albumURL = jsonObject.getString("coverUrl");
STARTING HERE: ---> titleTextView.setText(title);
artistTextView.setText("\n" + artist);
lyricsTextView.setText("\n" + lyrics);
This code was able to display the data when I first open the app and parse it, the app the force close when I return/start to the app(while service is running).
The logcat:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.brandon.sgpradio/com.brandon.sgpradio.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2395)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2453)
at android.app.ActivityThread.access$900(ActivityThread.java:173)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5579)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.brandon.sgpradio.MainActivity.parseJson(MainActivity.java:225)
at com.brandon.sgpradio.MainActivity.isMyServiceRunning(MainActivity.java:282)
at com.brandon.sgpradio.MainActivity.onCreate(MainActivity.java:97)
at android.app.Activity.performCreate(Activity.java:5451)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2359)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2453)
at android.app.ActivityThread.access$900(ActivityThread.java:173)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5579)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
Thanks!
Update:
1. Add a tag to your fragment when you add it to the activity via ViewPager.
fragment = new playerFragment();
fragment.setTag("player_fragment");
Get the fragment via getFragmentManger().findFragmentByTag() method.
PlayerFragment pFragment =
getFragmentManager().findFragmentByTag("player_fragment");
if(pFragment != null)
{
pFragment.setTextViewText();
}

Trying to add a Facebook Friend Picker, but get null pointer exception

I'm trying to learn and add some basic Facebook functionality to my Android App (an address book). What I'm eventually going to do is import a friends profile picture. I've currently only copy/pasted what I need from the "FriendPickerSample" example project, which is just a button that brings up a friend picker fragment, and, if I remember correctly, it places the names you picked into a list. I've altered it to come up when an imageview is clicked, and it'll just Toast the single name that is chosen. When I try to bring up the friend picker, I get a null pointer exception, and I don't understand why. Here's the log:
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.JDE.RAB/com.JDE.RAB.PickFriendsActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
at android.app.ActivityThread.access$2300(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.facebook.widget.PickerFragment.onCreateView(PickerFragment.java:152)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:871)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1083)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:635)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1431)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:523)
at com.JDE.RAB.PickFriendsActivity.onStart(PickFriendsActivity.java:112)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
at android.app.Activity.performStart(Activity.java:3781)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2636)
... 11 more
This is the code in my onclick, which is supposed to open the PickFriendsActivity:
if (Session.getActiveSession() == null || Session.getActiveSession().isClosed())
{
Session.openActiveSession(AddNewTabLayoutActivity.this, true, null);
}//endif
FriendPickerApplication application = (FriendPickerApplication) GetMyApplication();
application.setSelectedUsers(null);
Intent intent = new Intent(AddNewTabLayoutActivity.this, PickFriendsActivity.class);
// Note: The following line is optional, as multi-select behavior is the default for
// FriendPickerFragment. It is here to demonstrate how parameters could be passed to the
// friend picker if single-select functionality was desired, or if a different user ID was
// desired (for instance, to see friends of a friend).
//multi-select is OFF
PickFriendsActivity.populateParameters(intent, null, false, true);
Debugger.SendNotification(getApplicationContext(), "Alert", "Alert", "Starting Activity For Result", 3009);
startActivityForResult(intent, 2);
PickFriendsActivity, line 112 is super.onStart(); Should I post the entirety of that activities source code?
Thanks for any help!
You should copy res/layout/pick_friends_activity.xml to your own project too.

Categories

Resources