![](/img/trans.png)
[英]NullPointerException in AsyncTask onPostExecute on line 1
[英]NullPointerException onPostExecute in Asynctask
我在菜单工具栏中添加了图标,当按下该图标时,它会旋转几秒钟执行代码,该代码检查应用程序是否有任何更新可用,然后动画停止。 它以前运行良好,但我不知道从今天早上开始它会检查更新,但随后不是停止动画,而是应用程序在 logcat 下方崩溃。
Process: com.nepalpolice.yummyfood, PID: 3400
java.lang.NullPointerException
at com.nepalpolice.yummyfood.MainActivity.resetUpdating(MainActivity.java:248)
at com.nepalpolice.yummyfood.update.UpdateTask.onPostExecute(UpdateTask.java:37)
at com.nepalpolice.yummyfood.update.UpdateTask.onPostExecute(UpdateTask.java:12)
下面是来自 Mainactivity 的 resetUpdating 方法
public void resetUpdating() {
// Get our refresh item from the menu
MenuItem m = mymenu.findItem(R.id.action_refresh);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
if (m.getActionView() != null) {
// Remove the animation.
m.getActionView().clearAnimation();
m.setActionView(null);
}
}
}
更新任务类
public class UpdateTask extends AsyncTask<Void, Void, Void> {
private Context mCon;
public UpdateTask(Context con)
{
mCon = con;
}
@Override
protected Void doInBackground(Void... nope) {
try {
// Set a time to simulate a long update process.
Thread.sleep(4000);
return null;
} catch (Exception e) {
return null;
}
}
@Override
protected void onPostExecute(Void nope) {
((MainActivity) mCon).resetUpdating();
}
}
更新服务类
public class UpdateService extends Service {
public UpdateService() {
}
@Override
public int onStartCommand(Intent intent, int flags, int startId){
RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
String url = "http://updatesagar.blogspot.com/2018/06/msc.html";
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if(response != null ) {
boolean resp = response.contains("<div class='post-body entry-content' id='post-body-4386558807662471459' itemprop='description articleBody'>\n" +
"<div dir=\"ltr\" style=\"text-align: left;\" trbidi=\"on\">\n" +
"11</div>\n" +
"<div style='clear: both;'></div>");
if (!resp) {
Intent intent1 = new Intent(UpdateService.this, UpdateDialog.class);
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent1);
} else {
Toast.makeText(getBaseContext(), "No updates Availaible", Toast.LENGTH_SHORT).show();
}
}
}
}, new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError volleyError) {
volleyError.printStackTrace();
}
});
queue.add(stringRequest);
return Service.START_NOT_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
}
如果需要更多信息,请告诉我。 提前致谢。
我对代码行号和与旋转相关的错误的猜测是:这行很可能导致空指针
MenuItem m = mymenu.findItem(R.id.action_refresh);
mymenu 为空
当屏幕旋转时,所有 ui 视图都会重新创建,因此旧视图将消失,如果异步任务需要一些时间来执行并且在处理过程中发生旋转。 正确的做法是在任务完成后,先从 mainactivity 中找到最新的当前视图对象,然后再进行任何设置、操作等
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.