gpt4 book ai didi

java - 当它被服务调用时,在 AsycTask 中的 Handler 中的 Toast 上获取 NullPointerException

转载 作者:行者123 更新时间:2023-11-30 08:47:00 25 4
gpt4 key购买 nike

当我使用应用程序时,我开始知道在 AsycTask 中我不能直接使用 Toast,除非它是一个处理程序。所以对于处理程序,到目前为止它工作正常。 Toast 作为处理程序如下

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();
}
}
};

我只是在我的 AsycTask 中调用 threadMsg("Message"); 并且它工作正常。但是在我的新要求中,我碰巧从服务中调用了 AsycTask。当我执行它时,我在 Toast 上遇到空指针异常。
我不明白在这种情况下发生了什么。请帮我解决这个问题。

编辑:我正在执行 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() {

}
}

编辑:堆栈跟踪

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)

最佳答案

问题是服务没有运行 n UI 线程。您可以通过 Activity 显示 Toast:

@Override
public void onCreate() {
super.onCreate();
mHandler = new Handler();
}
@Override
protected void onHandleIntent(Intent intent) {
mHandler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(MyIntentService.this,
"Hello Toast!", Toast.LENGTH_LONG).show();
}
});
}

关于java - 当它被服务调用时,在 AsycTask 中的 Handler 中的 Toast 上获取 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32665088/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com