1

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!

3
  • The Context is null if the exception is at that line. You should test getActivity() for returning null. Do you save that task across a configuration change? Commented Feb 21, 2013 at 17:32
  • @Luksprog I actually fixed this in a hackery way (I think?) by setting adapter if getActivity is NOT null. (so kind of like what you said). It seems I just put a band aid in the real issue though. I also removed this: android:configChanges="orientation|screenSize|keyboardHidden" As I want layouts to be able to change based on port/land. So no to your second questions? Commented Feb 21, 2013 at 19:12
  • 1
    It's not a hack(that was what I suggested with the null check). The task will continue to do its stuff and if the activity is not available at the moment of completion you shouldn't try to interact with it(by creating/setting the adapter). The second question was about you taking care of the task you started(and hopefully not just starting it and praying that the user doesn't do something until it finishes). There are a lot of resources on how to handle the task(using getLastNonConfiguration(deprecated), a retained fragment(the new way) etc). Commented Feb 23, 2013 at 9:19

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

4 Comments

Well ReviewTask is executing in public class MyOnItemSelectedListener implements OnItemSelectedListener { There is a spinner in the fragment and the list is sorted by one of two ways. The spinner is initialized and set to a default position in the onCreateView. Does that help?
I seemed to replicate this bug by going into the activity but exiting before it has finished loading
Sorry one more edit, MyOnItemSelectedListener is actually in onActivityCreated`
I would recommend not setting up an adapter in OnItemSelected, if you need to adjust the sorting just change the list that the adapter is referencing and call adapter.NotifyDataSetChanged() and the adapter will redraw your views. I'd recomend setting up your adapter in the fragment that the spinner is stored in (or the fragment you're using it in not sure if those are the same you said you have 2 fragments)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.