TheLettuceMaster
TheLettuceMaster

Reputation: 15734

Problematic Custom Adapter ocasionally Null in ListFragment

So this problem happens only some times. There is no rhyme or reason that I can see. I suspect, the faster the user moves around the app, the more likely it happens.

Here is LogCat:

02-20 12:09:58.526: E/AndroidRuntime(18777): FATAL EXCEPTION: main
02-20 12:09:58.526: E/AndroidRuntime(18777): java.lang.NullPointerException
02-20 12:09:58.526: E/AndroidRuntime(18777):    at android.widget.ArrayAdapter.init(ArrayAdapter.java:310)
02-20 12:09:58.526: E/AndroidRuntime(18777):    at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:128)
02-20 12:09:58.526: E/AndroidRuntime(18777):    at com.---.---.ReviewAdapter.<init>(ReviewAdapter.java:42)
02-20 12:09:58.526: E/AndroidRuntime(18777):    at com.---.---.RateFragmentActivity$ReviewFragment$ReviewTask.onPostExecute(RateFragmentActivity.java:1203)
02-20 12:09:58.526: E/AndroidRuntime(18777):    at com.---.---.RateFragmentActivity$ReviewFragment$ReviewTask.onPostExecute(RateFragmentActivity.java:1)
02-20 12:09:58.526: E/AndroidRuntime(18777):    at android.os.AsyncTask.finish(AsyncTask.java:631)
02-20 12:09:58.526: E/AndroidRuntime(18777):    at android.os.AsyncTask.access$600(AsyncTask.java:177)
02-20 12:09:58.526: E/AndroidRuntime(18777):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
02-20 12:09:58.526: E/AndroidRuntime(18777):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-20 12:09:58.526: E/AndroidRuntime(18777):    at android.os.Looper.loop(Looper.java:137)
02-20 12:09:58.526: E/AndroidRuntime(18777):    at android.app.ActivityThread.main(ActivityThread.java:5193)
02-20 12:09:58.526: E/AndroidRuntime(18777):    at java.lang.reflect.Method.invokeNative(Native Method)
02-20 12:09:58.526: E/AndroidRuntime(18777):    at java.lang.reflect.Method.invoke(Method.java:511)
02-20 12:09:58.526: E/AndroidRuntime(18777):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
02-20 12:09:58.526: E/AndroidRuntime(18777):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
02-20 12:09:58.526: E/AndroidRuntime(18777):    at dalvik.system.NativeStart.main(Native Method)

The crash happens here:

adapter = new ReviewAdapter(getActivity(), r, tf);

r (an array) is not null, neither is tf, a TrueType font).

On the ReviewAdapter, here is other side of crash line 42:

public ReviewAdapter(Context context, Review[] objects, Typeface tf) {
        super(context, 0, objects);  // line 42

I believe context is null.

Here is the big picture:

I have a FragmentActivity with a Fragment and ListFragment. The adapter is inside the ListFragment of course. Neither of those fragments interact with each other. they are statically displayed in the xml.

So ReviewAdapter is called inside:

protected void onPostExecute(Void v) {

inside of:

public class ReviewTask extends AsyncTask<String, String, Void> {

inside of:

public static class ReviewFragment extends ListFragment {

inside of:

public class RateFragmentActivity extends SherlockFragmentActivity implements
        ActionBar.TabListener {

That is the context of things. I must not be handling my fragment lifecycle correctly, but this error is so sporadic.

Last note: I do have this in my manifest:

   android:configChanges="orientation|screenSize|keyboardHidden"

So config changes are not causing this.

Someone please help me on this: this problem has plagued me for months. If you need more code to view, by all means, let me know were and I will provide!

Upvotes: 1

Views: 508

Answers (2)

TheLettuceMaster
TheLettuceMaster

Reputation: 15734

I have solved this problem by using Luksprog's advice: checking for null on context before setting adapter.

simply:

if (getActivity() != null) { \\ set adapter }

Thanks for everyone's help.

Upvotes: 2

Eluvatar
Eluvatar

Reputation: 2295

it sounds like you may be creating the adapter OnCreate of the fragment and depending on how it gets executed the fragment may have not been attached to an activity yet. more code would help to see where your ReviewTask gets executed and if you can I would recommend moving it to onCreateView as I suspect it's currently in onCreate and it's running through and trying to create the adapter before your Fragment has been attached to it's activity.

Upvotes: 1

Related Questions