sagar
sagar

Reputation: 745

exception on making toast in android

I am implementing a code in android that sends json data to server.I am referring to the tutorials of the following link :- sending json to server.

Almost all the code works fine, i.e. i am getting response from server.But when my code reaches onPostExecute() method I get the following exception :

01-18 02:20:40.485      435-435/com.example.sagar.postjson W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x4001d800)
01-18 02:20:40.505      435-435/com.example.sagar.postjson E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at android.widget.Toast.<init>(Toast.java:89)
            at android.widget.Toast.makeText(Toast.java:231)
            at com.example.sagar.postjson.HttpAsyncTask.onPostExecute(HttpAsyncTask.java:119)
            at com.example.sagar.postjson.HttpAsyncTask.onPostExecute(HttpAsyncTask.java:26)
            at android.os.AsyncTask.finish(AsyncTask.java:417)
            at android.os.AsyncTask.access$300(AsyncTask.java:127)
            at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:123)
            at android.app.ActivityThread.main(ActivityThread.java:4627)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:521)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
            at dalvik.system.NativeStart.main(Native Method)

The code for this method is as follows :

protected void onPostExecute(String result) {
        Toast.makeText(new MainActivity().getBaseContext(), "come", Toast.LENGTH_LONG).show();
    }

I have also tried in this way :-

protected void onPostExecute(String result) {
     new MainActivity().print();
}

print() method in MainActivity as :

public void print(String result) {
        Toast.makeText(getBaseContext(), "come", Toast.LENGTH_LONG).show();
    }

But it also doesn't works.

Please tell me where i am wrong,, Thanks.

Upvotes: 1

Views: 2507

Answers (3)

Hemant Shori
Hemant Shori

Reputation: 2483

Define a global variable in the Class Context mcontext;

Then , in oncreate() initialize the variable mcontext= getActivity().getApplicationContext() .

Then, use the context in that class .

Toast.makeText(mcontext, "come", Toast.LENGTH_LONG).show();

Upvotes: 1

Simon Dorociak
Simon Dorociak

Reputation: 33515

I think that problem is this piece of code:

new MainActivity().getBaseContext()

This is not a way how to obtain Context. You need to change it to MainActivity.this and then it will work (it depends on current scenario as well and i don't know yours).

No , i have defined in the same package as MainActivity but didn't made it inner class.

Since your AsyncTask is separated class it knows nothing about your Activity class. You need to pass Context through constructor:

public MyTask(final Context context) {
   this.mContext = context;
}

Usually I'm showing toast messages by implementing util method that will do it for me:

public static void showMessage(final String msg) {
   final Context context = App.getContext();
   Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
}

where App is custom class that extends from Application and it could like like:

public class App extends Application {

   private static Context sContext;

   public void onCreate() {
      sContext = getApplicationContext();
   }

   public static Context getContext() {
      return sContext;
   }
}

I think it's better way how to do it and also it's more clean solution since you can call it everywhere in your application without redundant calls.

Upvotes: 1

Willis
Willis

Reputation: 5336

Change your Toast to be...

Toast.makeText(MainActivity().getActivity(), "come", Toast.LENGTH_LONG).show();

instead of...

Toast.makeText(new MainActivity().getBaseContext(), "come", Toast.LENGTH_LONG).show();

You do not need to create a new Activity, only reference the Context of the current Activity.

Upvotes: 2

Related Questions