Showing DialogFragment Causes IllegalStateException - android

I have a DialogFragment which I am showing when a button is clicked. I have the following code in the onClick method of the button:
InfoTextDialog infoDialog = new InfoTextDialog(conditions[counter], information[counter]);
infoDialog.show(getFragmentManager(), null);
However, I sometime get the following exception:
java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
This does not occur all the time, but it happens quite frequently. Can someone please explain to me what is causing this exception to be thrown, and how to solve this issue.
EDIT 1: Here is the stack trace:
java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at android.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1318)
at android.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1329)
at android.app.BackStackRecord.commitInternal(BackStackRecord.java:607)
at android.app.BackStackRecord.commit(BackStackRecord.java:586)
at android.app.DialogFragment.show(DialogFragment.java:230)
at <...>.MyActivity$1.onClick(MyActivity.java:73)
at android.view.View.performClick(View.java:4235)
at android.view.View$PerformClick.run(View.java:17484)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:5299)
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:841)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
at dalvik.system.NativeStart.main(Native Method)

hard to say without knowing more about your app - but this should help:
if (!getActivity().isFinishing()) {
InfoTextDialog infoDialog = new InfoTextDialog(conditions[counter], information[counter]);
infoDialog.show(getFragmentManager(), null);
}

Related

Java nullpointer exception from Webview

I tried to search for this problem but this error doesn't look to be the same as this error (my google play version is newer and notice that the error is at a different line of WebViewClassic, so might be different error or the same in a newer version) but I have received the exactly same error exception from webview
java.lang.NullPointerException
at android.webkit.WebViewClassic$WebViewInputConnection.setNewText(WebViewClassic.java:583)
at android.webkit.WebViewClassic$WebViewInputConnection.setComposingText(WebViewClassic.java:323)
at android.webkit.WebViewClassic$WebViewInputConnection.commitText(WebViewClassic.java:339)
at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:279)
at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:77)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:5297)
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:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
However I am not using any webview class directly. However, this problem might be related to AdView, but I don't understand how. My code is the following:
#Override
public void onPause() {
//TODO: these lines are created because onPuase of Adview doesn't work
if ( adView != null ) {
adView.pause();
adView.destroy();
adView = null;
Log.i(ApplicationData.APP_TAG, TAG + ": OnPause, pausing the Adview");
}
super.onPause();
}
The error appears after the Log.i line, I would have expected this error to appear when AdView was called but not after. Is possible that this error is coming from a threaded action? Any idea how to avoid the problem?
As the example in https://developer.android.com/reference/com/google/android/gms/ads/AdView.html and the document for AdView.destroy(), you should only call adView.pause() in onPause(), and call adView.destroy() later in onDestroy().
The stack dump looks like another thread is accessing the destroyed AdView instance.

Choreographer NullPointerException

I've been working on an existing codebase for a while and from browsing our crash log service I've noticed an exception that happens pretty frequently, I am not able to reproduce this issue, nor do I have the context for the scenario to try and dig in, as this is a pretty big project it has become increasingly hard to find out the cause of this exception.
I have been searching online for similar issues and couldn't find any useful information.
If anyone is familiar with this issue, your help would be greatly appreciated.
Stacktrace is as follows:
java.lang.NullPointerException
at android.animation.PropertyValuesHolder.setupSetterAndGetter(PropertyValuesHolder.java:505)
at android.animation.ObjectAnimator.initAnimation(ObjectAnimator.java:487)
at android.animation.ValueAnimator.setCurrentPlayTime(ValueAnimator.java:517)
at android.animation.ValueAnimator.start(ValueAnimator.java:936)
at android.animation.ValueAnimator.start(ValueAnimator.java:946)
at android.animation.ObjectAnimator.start(ObjectAnimator.java:465)
at android.animation.AnimatorSet$1.onAnimationEnd(AnimatorSet.java:579)
at android.animation.ValueAnimator.endAnimation(ValueAnimator.java:1056)
at android.animation.ValueAnimator.access$400(ValueAnimator.java:50)
at android.animation.ValueAnimator$AnimationHandler.doAnimationFrame(ValueAnimator.java:644)
at android.animation.ValueAnimator$AnimationHandler.run(ValueAnimator.java:660)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:543)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5105)
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:792)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
at dalvik.system.NativeStart.main(NativeStart.java)
Cheers.
The issue turned out to be an animation being invoked from a Handler using postDelayed(), which eventually resulted in attempting to animate a null View reference, since it wasn't cleared properly according to it's host lifecycle events.
maybe you tagret is null, print log with animatorSet.getTarget() to see
I also get this stack trace when I use :
ObjectAnimator animator = ObjectAnimator.ofFloat(this, "scale", 1);
....
animator.start()
run in android 4.0.X, but it's ok for higher version, you can also refer this guy's commit, though I don't how to fix, however this is the root cause definitely.
I went through this exception now and find out that I was getting the instance of the View with animation from getActivity().findViewById. While being in a fragment I had to instantiate it from the rootview inflated containing the view.
I basically moved the view from the Activity to the Fragment layout, but didn't change the code.
So the rootview.findViewById resolved my problem.
My stacktrace:
java.lang.NullPointerException
at android.animation.PropertyValuesHolder.setupSetterAndGetter(PropertyValuesHolder.java:505)
at android.animation.ObjectAnimator.initAnimation(ObjectAnimator.java:392)
at android.animation.ValueAnimator.setCurrentPlayTime(ValueAnimator.java:495)
at android.animation.ValueAnimator.start(ValueAnimator.java:913)
at android.animation.ValueAnimator.start(ValueAnimator.java:923)
at android.animation.ObjectAnimator.start(ObjectAnimator.java:370)
at eu.davidea.passwordcloud.ui.ItemDetailFragment$3.onClick(ItemDetailFragment.java:162)
at android.view.View.performClick(View.java:4084)
at android.view.View$PerformClick.run(View.java:16966)
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:4745)
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:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Trust me, I found the solution of this crash issue on 4.0.x devices.
The problem is when you use ObjectAnimator without 'start' and 'end' value, the api on 4.0.x devices can't find the 'start' value to implement.
For example, this will lead to crash on 4.0.x devices
int endRadiusValue = 10;
ObjectAnimator
.ofFloat(roundedGradientDrawable, "cornerRadius", endRadiusValue)
.setDuration(200)
.start();
And this code work perfect on all api level devices
int startRadiusValue = 0;
int endRadiusValue = 10;
ObjectAnimator
.ofFloat(roundedGradientDrawable, "cornerRadius", startRadiusValue, endRadiusValue)
.setDuration(200)
.start();
The reason is the target view is null. ObjectAnimator ofFloat(Object target, ...).
This crash will only throw in 4x device.
There is no crash in another device
I solve the problem by adding the animation to a animationSet just like this:

Prevent app crash when cut operation in TextView

I put text-selectable TextView in Android layout with setTextIsSelectable(true) and the text is set by Html.fromHtml(sometexts).
It works good to allow user to copy text.
My JellyBean device gives small baloons with cut/copy/paste buttons on top of the selected text.
But when user accidently click cut icon left side of the copy menu, it crashes.
The log is given as below but there is no point to handle this exception in my application.
How to prevent app crash?
01-06 19:29:16.025 2048-2048/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.ClassCastException: android.text.SpannableString cannot be cast to android.text.Editable
at android.widget.TextView.deleteText_internal(TextView.java:8865)
at android.widget.TextView.onTextContextMenuItem(TextView.java:8315)
at android.widget.Editor$ActionPopupWindow.onClick(Editor.java:3556)
at android.view.View.performClick(View.java:4114)
at android.view.View$PerformClick.run(View.java:17097)
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:4885)
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:790)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
at dalvik.system.NativeStart.main(Native Method)
I'm not sure and I can't test it right now but maybe try to put that in your TextView xml :
android:bufferType="spannable"
If it's still not working you can implement yourself your action for the selectable event.
Look at the answer of this question here :
Android- How can I show text selection on textview?

Android Gallery Widget Error: java.lang.NullPointerException at android.widget.Gallery.setUpChild

The main Activity in my Android app has a Gallery widget that loads XML layouts (containing TextViews and Images) through an efficient ImageAdapter. For the most part this works fine and I haven't had any problems on any of my devices or the emulator, but I have seen the following error in my logs. It looks like this is affecting less than 1% of users, but I'd like to know what's causing it, how to resolve it, or at least "catch" it and resolve gracefully. Any ideas?
java.lang.NullPointerException at
android.widget.Gallery.setUpChild(Gallery.java:772) at android.widget.Gallery.makeAndAddView(Gallery.java:751) at android.widget.Gallery.fillToGalleryLeft(Gallery.java:667) at android.widget.Gallery.trackMotionScroll(Gallery.java:378) at android.widget.Gallery$FlingRunnable.run(Gallery.java:1369) 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:3695) 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)
I get this problem several days ago, please check your own gallery's adpater check its getView() methods,see if you return null in some if-cases,just replace return null with new View(context) (or other view but null) will simply solve this problem.Good luck

Saved state exception

I'm getting the following exception with my app. I believe this happens when the user leaves the app for a long time then returns to it. I am unable to replicate it however it is showing up in my crash logs a lot. I have no idea why this is happening so any suggestions are greatly appreciated.
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp/com.myapp.MyActivity}: java.lang.ArrayIndexOutOfBoundsException: length=3; index=-1
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2202)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2237)
at android.app.ActivityThread.access$600(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:4974)
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:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ArrayIndexOutOfBoundsException: length=3; index=-1
at java.util.ArrayList.get(ArrayList.java:306)
at android.support.v4.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1764)
at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:198)
at com.myapp.MyActivity.onCreate(MyActivity.java:235)
at android.app.Activity.performCreate(Activity.java:4538)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2158)
... 11 more
Line 235 is the following:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); // line 235
My onSaveInstanceState() is pretty simple:
#Override
public void onSaveInstanceState(Bundle state) {
super.onSaveInstanceState(state);
state.putInt("id1", id1);
state.putInt("id2", id2);
state.putInt("id3", id3);
state.putInt("id4", id4);
}
I ran into this same problem, I believe it a bug with android. Take a look at this thread for a possible solution http://code.google.com/p/android/issues/detail?id=22404
I got the exact same problem, I followed the advice given in Kenny's link:
I used the following:
FragmentManager.enableDebugLogging(true);
This will display in the logs all the calls on the FragmentManager (fragments added, deleted, hidden etc.).
It allowed me to see what was wrong in my case, one of the fragments in my app was sometimes not saved properly in the instance and its content was null.
Then the crash happened with the same error log when the app tried to restore this null fragment from the instance.
In my case this fragment was a little special and created automatically. To be sure not to get the error again, I removed the fragment from the FragmentManager before exiting the Activity.
I had the same problem, occurring with orientation changes, but it only happened with older Android versions (4.0.4), newer versions like 4.4.2 were fine. I want to share with you how I fixed it.
As Kenny and Yoann suggested, I used
FragmentManager.enableDebugLogging(true);
and found out that too much removing has been done. I had to remove ft.remove(df) from my code and now it's fine with both versions mentioned above. This is my code:
if (savedInstanceState != null) {
final FragmentTransaction ft = getFragmentManager().beginTransaction();
final DialogFragment df = (DialogFragment) getFragmentManager().findFragmentByTag("tag");
if (df != null) {
df.dismiss();
// ft.remove(df);
frag = new MyDialogFragment();
frag.show(getFragmentManager(), "tag");
}
ft.addToBackStack(null);
ft.commit();
}
For the sake of completeness, this is what I have done in onSaveInstanceState(final Bundle outstate):
if (frag != null) {
getFragmentManager().putFragment(outState, "tag", frag);
}

Categories

Resources