When I was working with an application, I came to know that inside AsycTask I can't use Toast directly unless it's a handler. So With handlers it's working fine so far. The Toast as a handler as follows
private void threadMsg(String msg) {
if (!msg.equals(null) && !msg.equals("")) {
Message msgObj = handler.obtainMessage();
Bundle b = new Bundle();
b.putString("message", msg);
msgObj.setData(b);
handler.sendMessage(msgObj);
}
}
private final Handler handler = new Handler() {
public void handleMessage(Message msg) {
String aResponse = msg.getData().getString("message");
if ((null != aResponse)) {
// ALERT MESSAGE
Toast.makeText(
getBaseContext(),
"Server Response: " + aResponse,
Toast.LENGTH_SHORT).show();
progressBarHolder.setVisibility(View.INVISIBLE);
}
else
{
// ALERT MESSAGE
Toast.makeText(
getBaseContext(),
"Not Got Response From Server.",
Toast.LENGTH_SHORT).show();
}
}
};
I'm just calling threadMsg("Message"); in my AsycTask and it's working fine. But In my new Requirement, I happens to call AsycTask from a Service. And When I execute it, I'm getting a Null pointer exception on Toast.
I'm not understanding what's happening in this scenario. Please help me in solving this.
Edit: Service Code Where I'm executing AsycTask
public class FirstService extends Service {
TayKitDatabaseOpenHelper db = new TayKitDatabaseOpenHelper(this);
private MyApplication app;
/** indicates how to behave if the service is killed */
int mStartMode;
/** interface for clients that bind */
IBinder mBinder;
/** indicates whether onRebind should be used */
boolean mAllowRebind;
/** Called when the service is being created. */
@Override
public void onCreate() {
app = (MyApplication) getApplication();
AsycInvokerActivity.OrderDeliveryAsyTask sm=new
AsycInvokerActivity().new OrderDeliveryAsyTask(orderDelivered);
sm.execute();
}
public void onStart(Intent intent, int startId) {
System.out.println("insuide service omstart");
}
/** The service is starting, due to a call to startService() */
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("inside FirstServece's onStartCommand");
List<OrderDeliveryModel> deliveredOrders =
db.getAllDeliveredOrdersByUserId(app.getPudoType());
System.err.println("Total Number or orders which has to be synced:
"+deliveredOrders.size());
return mStartMode;
}
/** A client is binding to the service with bindService() */
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
/** Called when all clients have unbound with unbindService() */
@Override
public boolean onUnbind(Intent intent) {
return mAllowRebind;
}
/** Called when a client is binding to the service with bindService()*/
@Override
public void onRebind(Intent intent) {
}
/** Called when The service is no longer used and is being destroyed */
@Override
public void onDestroy() {
}
}
Edit: Stack Trace
java.lang.NullPointerException
at android.widget.Toast.<init>(Toast.java:92)
at android.widget.Toast.makeText(Toast.java:233)
at com.example.databaseUtils.offlineDataUtils.AsycInvokerActivity$OrderDeliveryAsyTask$1.run(AsycInvokerActivity.java:174)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4441)
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:823)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
at dalvik.system.NativeStart.main(Native Method)