在 onPostExecute() 之后没有开始新的意图
Posted
技术标签:
【中文标题】在 onPostExecute() 之后没有开始新的意图【英文标题】:new intent not getting started after onPostExecute() 【发布时间】:2017-08-09 11:25:33 【问题描述】:这是一个用于执行我的后台活动的 java 文件,在 onPostExecute() 中我添加了一个启动意图,但它不起作用,这个后台类只是一个 java 文件,它不是任何活动的 java 文件
public class background extends AsyncTask<String,Void,String>
private ProgressDialog dialog;
private ConnectivityManager cm;
String jsonurl, jsonstring;
mobile_form mform;
private Context context;
background (Context ctx)
this.context = context.getApplicationContext();
this.cm = (ConnectivityManager)ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
this.dialog = new ProgressDialog(ctx);
mform = new mobile_form();
@Override
protected void onProgressUpdate(Void... values)
super.onProgressUpdate(values);
@Override
protected void onPostExecute(String result)
context.startActivity(new Intent(context, mobile_form.class));
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
if (isConnected)
if (dialog.isShowing())
dialog.dismiss();
【问题讨论】:
【参考方案1】:public class background extends AsyncTask<String,Void,String>
private ProgressDialog dialog;
private ConnectivityManager cm;
String jsonurl, jsonstring;
mobile_form mform;
private Context context;
public background (Context ctx)
this.context = ctx;
this.cm = (ConnectivityManager)ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
this.dialog = new ProgressDialog(ctx);
mform = new mobile_form();
//...
// doInBackground()
//...
@Override
protected void onProgressUpdate(Void... values)
super.onProgressUpdate(values);
@Override
protected void onPostExecute(String result)
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
if (isConnected)
if (dialog.isShowing())
dialog.dismiss();
context.startActivity(new Intent(context, mobile_form.class));
要使用这个 AsynkTask:
new background(getApplicationContext()).execute();
【讨论】:
没有改变它仍然显示不幸的是应用程序已停止工作【参考方案2】:您可以按照以下方式解决您的问题 公共类背景扩展 AsyncTask
private ProgressDialog dialog;
private ConnectivityManager cm;
String jsonurl, jsonstring;
mobile_form mform;
private mContext context;
public background (Context ctx)
this.mContext = ctx;
this.cm = (ConnectivityManager)ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
this.dialog = new ProgressDialog(ctx);
mform = new mobile_form();
//...
// doInBackground()
//...
@Override
protected void onProgressUpdate(Void... values)
super.onProgressUpdate(values);
@Override
protected void onPostExecute(String result)
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
if (isConnected)
if (dialog.isShowing())
dialog.dismiss();
mContext.startActivity(new Intent(mContext, mobile_form.class));
将此后台任务称为
new background(this).execute();
【讨论】:
以上是关于在 onPostExecute() 之后没有开始新的意图的主要内容,如果未能解决你的问题,请参考以下文章
从 onPostExecute(String result) 开始活动不设置 ui 标志
在 AsyncTask 的 onPostExecute 之后,无法按意图更新 RecyclerView