1

I'm writing an android application that uses a ViewPager to contain 3 screens that can be accessed via swiping left and right. These tabs are defined as fragment subclasses with separate XML files. It works, but for some reason whenever I unplug my device or connect/disconnect a bluetooth input device (possibly other things but these are the main causes I've seen) the fragments become detached, and the screen goes white, then the app crashes with this error:

java.lang.RuntimeException: Unable to start activity ComponentInfo{me.samboycoding.<snip>/me.samboycoding.<snip>.MainActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f08006d (me.samboycoding.<snip>:id/pager) for fragment TabDeviceOverview{d0b5892 #0 id=0x7f08006d android:switcher:2131230829:0}

I've got no idea why this happens. I've tried setting setRetainInstance to true, and I've also tried using the fragment manager to store my fragment, but to no avail. Firstly, is there no way to ensure a fragment is kept loaded - as I think that's what's happening here, and secondly if not, how do I go about detecting this state and recreating the view?

I can post code if desired, just tell me what to post.

Adapter class: https://hastebin.com/zayewipimo.java

Relevant part of my onCreate: https://hastebin.com/gubikixude.java

SamboyCoding
  • 583
  • 1
  • 4
  • 10

1 Answers1

2

1) remove this line:

pager.setOffscreenPageLimit(2);

it will cause fragments to retain it's view rather than recreating it's view.

2) change in your adapter class:

extends the FragmentStatePagerAdapter class

PageAdapter extends FragmentStatePagerAdapter

3) overide the getItemPosition() in your adapter class and make it return POSITION_NONE.

  @Override
    public int getItemPosition(Object object) {

        return POSITION_NONE;
    }
rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
  • Unfortunately not. I still get the exact same error. – SamboyCoding Jul 14 '17 at 19:25
  • post the error logcat..and `clean build-->Run` the apk – rafsanahmad007 Jul 14 '17 at 19:27
  • I'll clean build it first, then post a logcat if it fails again. – SamboyCoding Jul 14 '17 at 19:28
  • Yup. Still fails. Here's a full stack: https://hastebin.com/raw/anobotugix – SamboyCoding Jul 14 '17 at 19:30
  • Sorry if you're still looking at this, but I just realised I didn't mention you in that last comment and I can't seem to edit it, so... @rafsanahmad007 – SamboyCoding Jul 14 '17 at 19:44
  • in your activity `if (!initComplete)`..is this condition works when app goes in `onPause()` or connected to Bluetooth..? – rafsanahmad007 Jul 14 '17 at 19:49
  • No. `initComplete` is set to true when `onPostInit` in the activity finishes for the first time to prevent the entire application from being recreated should it be suspended. Is it this that's causing the issue, and if so how can I prevent data being lost when the app is suspended, because currently the entire activity is re-created without this statement. – SamboyCoding Jul 14 '17 at 19:59
  • i am not sure exactly this causes the error...but you can try remove it and re run the application..also to prevent recreating the activity take a look at this https://stackoverflow.com/questions/10498636/prevent-android-activity-from-being-recreated-on-turning-screen-off – rafsanahmad007 Jul 14 '17 at 20:11
  • This seems to work, but I have no idea why. I guess i'll just workaround. Thanks for your help. – SamboyCoding Jul 14 '17 at 20:24
  • 1
    happy to help...you are welcome to accept the answer if it helps. Thanks – rafsanahmad007 Jul 14 '17 at 20:46