6

I basicall have an AsyncTask (run from main Activity) that populates a ViewPager inside a fragment. I'm inflating an xml layout file to populate the ViewPager.

The problem is that I can't get pointers to the views inside the layout (imageview, textview) so that I can change then at runtime. I know it's probably because getView() returns null. I've read that it's probably because onCreateView() hasn't been called, yet. Anybody know what I can do to solve this?


My code's a bit of a mess right now.

Here's a simpler explanation to what I'm doing:

  1. MainActivity Asynctask populates a database and sends pointer to Fragment. Something like SendToFragement(db);
  2. The Fragment method ReceiveFromActivity(db) receives db pointer and populates the ViewPager.

It works fine if I'm just creating TextViews, setting text, and adding TextViews to the Viewpager. But, of course, I want to make it look better so I've inflated an xml layout. The problem is that I can't change the contents of the xml layout because getView() is returning NULL so getView().findViewById() doesn't work.

user1923613
  • 626
  • 5
  • 11
  • 31
  • How about some code, so we can point out where your problem is, and propose a fix. – tolgap Feb 28 '13 at 13:11
  • You can't do work on the UI thread from the `doInBackground()`-method. Use the [`onProgressUpdate()`](http://developer.android.com/reference/android/os/AsyncTask.html#onProgressUpdate%28Progress...%29) to show results on the UI. – Lukas Knuth Feb 28 '13 at 13:14

1 Answers1

7

Depending on the implementation of your PagerAdapter, it is possible that a Fragment no longer in view will have their View destroyed in order to free up resources. As such, there's no need to update the View because it will be created again in onCreateView() with the updated information. getView() will return a view in between calls to onCreateView() and onDestroyView(). Outside of that, the Fragment can still exist in memory, but not attached to any View, thus getView() will return null.

So basically,

View fragmentView = getView();
if(fragmentView != null) {
  // we are in view or at least exist. Update the elements.
}
// Else don't worry about it. Just update the data.
DeeV
  • 35,865
  • 9
  • 108
  • 95
  • That's weird. I tried this `View fragmentView = getView(); if(fragmentView != null) {ImageView img = (ImageView) fragmentView.findViewById(R.id.imageView1); img.setImageResource(R.drawable.gasguy);}` but I'm still getting a NULL pointer exemption. – user1923613 Feb 28 '13 at 14:14
  • The if-statement is true if fragmentView is null. Try `!=` instead. – DeeV Feb 28 '13 at 14:16
  • Sorry, I meant `(fragmentView != null)` – user1923613 Feb 28 '13 at 14:18
  • Is it not finding "R.id.imageView1"? – DeeV Feb 28 '13 at 14:29
  • At this point, I don't know what's happening. LOL. But I tried this `View fragmentView = getView().findViewById(R.id.imageView1); if(fragmentView != null) { ImageView img = (ImageView) fragmentView; img.setImageResource(R.drawable.gasguy);}` and it works. Could you enlighten me, please. – user1923613 Feb 28 '13 at 14:29
  • It's not finding `R.id.imageView`, so it skips the if-statement and your image gets updated in `onCreateView()` when the user swipes to the fragment. That's at least the first thing that comes to mind. – DeeV Feb 28 '13 at 14:37
  • If you're calling `getView()` outside your fragment class that has the View, then it could be you're getting the wrong View. – DeeV Feb 28 '13 at 14:37
  • 3
    These fragments are such a pain. Anyway, thank you for your help. – user1923613 Feb 28 '13 at 14:38