【发布时间】:2014-06-28 17:26:57
【问题描述】:
我从 main_activity 打这个电话
Intent createAccount = new Intent(mainact_Context,Register_activity.class);
startActivity(createAccount);
这是调用AsyncTask的活动:
public class Register_activity extends Activity {
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
context = this;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register_activity);
Button RegButton = (Button) this.findViewById(R.id.Regbutton);
RegButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
RegisterWithServer regser = new RegisterWithServer(context);
regser.execute();
}
});
setupActionBar();
}
}
在我的异步活动中,我有这个:
public class RegisterWithServer extends AsyncTask <String,Void,String> {
Context con;
public RegisterWithServer(Context context)
{
con = context;
}
protected String doInBackground(String... params) {
//this code gets executed perfectly
}
@Override
protected void onPostExecute(String result)
{
/*I tried doing something with the context and the result string here and it worked too.*/
Toast.makeText(con, result , Toast.LENGTH_SHORT).show();
}
}
doInBackground() 部分得到完美执行。 onPostExecute() 部分被调用,但 Toast 未显示。有人能发现什么吗?
【问题讨论】:
-
Toast.makeText(this, result, Toast.LENGTH_SHORT).show(); or Toast.makeText(classname.this, result , Toast.LENGTH_SHORT).show();or Context con=this;//先初始化this
-
你确定任务结束时activity还活着吗?
-
代替 v.getContext() 尝试 Register_activity.this
-
@greenapps:不能这样做,它说“Register_activity 类型的封闭实例在范围内是不可访问的”
-
@Mario Lenci:我确定该活动还活着,因为我尝试使用相同的 onpostexecute 方法更改 Register_activity 内的按钮文本,并且它起作用了。
标签: android android-asynctask android-toast