3

In one of my Fragments I have a method I call from the parent Activity. The method is nothing special, it simply scrolls my ListView to a position.

public void scrollToSomething() {
    mListView.smoothScrollToPosition(The position I supply);
}

When I call this method initially, it works just like I expect, but when I rotate my device and call it again, I throw a NullPointerException from my ListView. I'm very much lost as to why this is happening. I've tried calling setRetainInstance(true). I've tried checking to see if my ListView is null before I call the method and if it is, then I initialize it again, but this does not work either. I've tried changing my onCreateView in a few different ways, thinking maybe something is going wrong there.

onCreateView

  @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    /* The View for the fragment's UI */
    final ViewGroup mRootView = (ViewGroup)inflater.inflate(R.layout.fragment_list_base,
            container, false);
    /* Initialize our ListView */
    mListView = (ListView)mRootView.findViewById(R.id.fragment_list_base);
    return mRootView;
}

The Fragment is attached to a ViewPager using a simple FragmentStatePagerAdapter. I've tried carefully looking through the onDestroy methods of my Fragment and the parent Activity to see if I'm calling something that may cause the error, but nothing stands out. I'm not sure if what I'm experiencing is some sort of bug, or if I'm just overlooking something. Any help or advice would be huge. I'm not entirely sure what else to add because I'm just pretty lost as why this is happening, but if anyone has a question, I'll post any code you may see fit.

adneal
  • 30,484
  • 10
  • 122
  • 151
  • Based on your title, perhaps the problem is simply that Activities "reset" when you change orientation": [Android app resets on orientation change](http://stackoverflow.com/questions/2774645/android-app-resets-on-orientation-change-best-way-to-handle), and [Android: handling runtime changes](http://developer.android.com/guide/topics/resources/runtime-changes.html) – paulsm4 Sep 10 '12 at 06:21
  • Sounds like scrollToSomething() is getting called before your fragment has had a chance to call onCreateView(). It could also be that your activity is holding a reference to an old fragment. When exactly in the lifecycle is the scrollToSomething() method being called, btw? – Krylez Sep 10 '12 at 06:22
  • @paulsm4 Tried it, doesn't work. It was a good idea though. I thought it might work. – adneal Sep 10 '12 at 06:27
  • @Krylez `scrollToSomething` is only called when I touch a particular `LinearLayout` with on `onClickListener`. The data in my `ListView` shows up no matter the orientation, it only seems to be that particular method that's giving me problems. – adneal Sep 10 '12 at 06:28
  • If the LinearLayout and ListView are both in the Fragment, try adding the listener in the Fragment creation. That way your reference shouldn't ever be null (famous last words). – Krylez Sep 10 '12 at 06:32

1 Answers1

5

This may help you

If your android:targetSdkVersion="12" or less:

android:configChanges="orientation|keyboardHidden">

If your android:targetSdkVersion="13" or more:

android:configChanges="orientation|keyboardHidden|screensize">
adneal
  • 30,484
  • 10
  • 122
  • 151
Rajendra
  • 1,700
  • 14
  • 17
  • Actually, this did the trick. Would you mind briefly explaining, if you could, why my `ListView` became null otherwise, please? – adneal Sep 10 '12 at 06:34
  • This depends upon your code how you used to populate the data in list view. Anyway is it working fine now? – Rajendra Sep 10 '12 at 06:39
  • It seems to only work as long as I add `screenSize` the to `configChanges` and some of the `Views` inside the `Adapter` I attach to my `ListView` become much larger on an orientation change, but as far as that particular method, `scrollToSomething`, it works like it should. – adneal Sep 10 '12 at 06:42
  • As your is android:targetSdkVersion="13" or higher . that’s-why you android:configChanges="orientation|keyboardHidden|screensize" is like this – Rajendra Sep 10 '12 at 06:53
  • Yes, that's why I added `screenSize`. – adneal Sep 10 '12 at 06:54
  • So, after a little more testing it's occurred to me that this won't work. Also, as it turns out it's not my `ListView` that's null, but rather my `Fragment`. `getActivity()` is returning null as well as `getView()`. I suppose it has something to do with my `FragmentStatePagerAdapter` and my `ViewPager`, but that's a different question now. – adneal Sep 11 '12 at 02:08