0

my question may seem similar to his, Replacing an ActionBar menu item icon with an indeterminate ProgressBar but the issue is different.

I am already able to replace refresh button from action bar with the progress bar but when I try it from onPreExecute method of AsyncTask class, I always get NullPointerException error.

Here is my code,

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.desc_xml, menu);


        refreshMenuItem = menu.findItem(R.id.action_refresh);

        return true;
    }

@Override

public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {


    case R.id.action_refresh:
        new GetDesc().execute();
        return true;


    default:
        return super.onOptionsItemSelected(item);
    }
}

and this is my preExecute method in AsyncTask,

@Override
        protected void onPreExecute() {
            super.onPreExecute();
            refreshMenuItem.setActionView(R.layout.actionbar_indeterminate_progress);

        }

the above code always give error, and after searching on StackOverflow I came to know that I have to provide right context to the class when setting a layout from AsyncTask,

so I tried this in onPreExecute,

myclass.this.refreshMenuItem.setActionView(R.layout.actionbar_indeterminate_progress);

but that give the same error. So i tried this,

    refreshMenuItem.setActionView(com.abc.myproject.R.layout.actionbar_indeterminate_progress);

but I am still getting the same error.

Why am I still getting the same error even though i am providing the context ?

Here is the logcat output,

): FATAL EXCEPTION: main
): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myproject.abc java.lang.NullPointerException
):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
):  at android.os.Handler.dispatchMessage(Handler.java:99)
):  at android.os.Looper.loop(Looper.java:137)
):  at android.app.ActivityThread.main(ActivityThread.java:5041)
):  at java.lang.reflect.Method.invokeNative(Native Method)
):  at java.lang.reflect.Method.invoke(Method.java:511)
):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
):  at dalvik.system.NativeStart.main(Native Method)
): Caused by: java.lang.NullPointerException
):  at $GetDesc.onPreExecute(NewsDetails.java:105)
):  at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
):  at android.os.AsyncTask.execute(AsyncTask.java:534)
):  at .onCreate(NewsDetails.java:40)
):  at android.app.Activity.performCreate(Activity.java:5104)
):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
):  ... 11 more
Community
  • 1
  • 1
Joe
  • 1,488
  • 3
  • 16
  • 27
  • Seems like `refreshMenuItem` is null at this stage. Check it with the debugger. – aga Dec 26 '14 at 14:04
  • that I know, I know that it is null and that is why I am getting error. But I don't know why it is null ? – Joe Dec 26 '14 at 14:06
  • 2
    `onCreate` method is called before `onCreateOptionsMenu` and your `refreshMenuItem` variable has assigned a value in `onCreateOptionsMenu`. This is why it is `null`. – Sufiyan Ghori Dec 26 '14 at 16:59

0 Answers0