Android Switching between Viewpager produces Android crash - android

I have five fragments in an View pager. One of them is a BarcodeFragment BarcodeFragment and I am getting a very weird android crash when I swipe between ViewPager fragments quickly. The crash Im getting is on the android side. This is the log.
java.lang.IndexOutOfBoundsException: Invalid index 7, size is 7
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.set(ArrayList.java:481)
at android.support.v4.app.FragmentManagerImpl.makeInactive(FragmentManager.java:1169)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1078)
at android.support.v4.app.FragmentManagerImpl.removeFragment(FragmentManager.java:1212)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:652)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:446)
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:5001)
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:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
I suspect its happening because of the following :
Whenever I am at the barcodeFragment and I want to switch to another fragment in the viewpager I remove the fragment using the following code:
if (getChildFragmentManager().findFragmentById(R.id.barcode_Fragment) != null) {
if (mBarcodeFragment != null && mBarcodeFragment.isResumed()) {
mBarcodeFragment.onDestroy();
mFragmentManager.beginTransaction().remove(mBarcodeFragment).commitAllowingStateLoss();
mBarcodeFragment = null;
}
}
And whenever I am coming back to the BaseScanFragment (Which is the fragment which calls the Barcode Fragment) , I am adding the fragment again through the following code.:
mBarcodeFragment = new BarcodeFragment();
getChildFragmentManager().beginTransaction().replace(R.id.barcode_Fragment, mBarcodeFragment).commit();
mBarcodeFragment.setDecodeFor(IScanResultHandler.MODE.SHOP_NOW_MODE);
mBarcodeFragment.setScanResultHandler((IScanResultHandler) getSherlockActivity());
Can anyone guess what the problem is?

I have faced this problem earlier when I was working with nested fragments. In my case it was occurring because I was using the wrong Fragment manager i.e I was accidentally using the Activity fragment manager for child fragment transactions
java.lang.IndexOutOfBoundsException: Invalid index 7, size is 7
The index points to the fragment in which this error is occurring i.e 7.

Related

My App Crashes When I Try Showing A Snackbar

I am using Android Studio's navigation drawer activity, and I want a snackbar to appear whenever I click on one of the buttons in the drawer, but when I click on it, the app crashes.
Here is a simplified version of the code run when I click on one of the navigation buttons:
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
int id = item.getItemId();
if (id == R.id.nav_playstore) {
Snackbar.make(findViewById(R.id.nav_playstore), "This app is not available on the Play Store yet", Snackbar.LENGTH_LONG).show();
}
I think the problem is that I should replace findViewById(R.id.nav_playstore) with something else, but I'm not sure what I would put there instead. If you need more info, I can give you some. Thanks :)
Edit: Here's the stacktrace:
08-03 10:03:45.658 20870-20870/ca.davesautoservice.davesautoservice E/AndroidRuntime: FATAL EXCEPTION: main
Process: ca.davesautoservice.davesautoservice, PID: 20870
java.lang.NullPointerException
at android.support.design.widget.Snackbar.<init>(Snackbar.java:183)
at android.support.design.widget.Snackbar.make(Snackbar.java:215)
at ca.davesautoservice.davesautoservice.MainActivity.onNavigationItemSelected(MainActivity.java:106)
at android.support.design.widget.NavigationView$1.onMenuItemSelected(NavigationView.java:151)
at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:811)
at android.support.v7.view.menu.SubMenuBuilder.dispatchMenuItemSelected(SubMenuBuilder.java:84)
at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:958)
at android.support.design.internal.NavigationMenuPresenter$1.onClick(NavigationMenuPresenter.java:318)
at android.view.View.performClick(View.java:4463)
at android.view.View$PerformClick.run(View.java:18770)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5333)
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:828)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
at dalvik.system.NativeStart.main(Native Method)
Kelvin.
Reading the DOCS, you can see that first parameters is:
View: The view to find a parent from.
So, you should pass any view that you have. This way, snack bar can track and find the parent view.
You probably have any other view in your activity.
You are passing
Snackbar.make(findViewById(R.id.nav_playstore), "This app is not available on the Play Store yet", Snackbar.LENGTH_LONG).show();
However R.id.nav_playstore is the ID a of a menu item (and not a VIEW). This way, findViewById(R.id.nav_playstore) returns NULL.
Change the ID from findViewById(R.id.nav_playstore) to an ID of any other View that you have on current Activity. It can be a TextView, EditText
etc...
Snack bar needs either Cordinator layout's view or FloatingAction Button's view to operate properly and respond accurately.
And you passed nav's id in Snackbar's body.
What to do
1- Wrap layout inside Coordinator Layout assign it an ID and use that view in Snackbar.
2- Pass findViewById of FAB (if exists) then it will show snackbar appropriately.
Snackbar.make(findViewById(R.id.fab), "message", Snackbar.LENGTH_SHORT).show();
Hope it helps.

Showing DialogFragment Causes IllegalStateException

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);
}

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:

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