尝试从异步方法启动新活动,但由于某种原因,活动无法启动
protected void onPostExecute(Integer result) {
try {
if (qResult.equals(userinfo[1])) {
File file = new File("info");
FileWriter fw = new FileWriter(file);
fw.write(userinfo[0] + "\n" + userinfo[1]);
context.startActivity(new Intent(context,OtherActivity.class));
我通过将其添加为构造函数中的一个参数来传递当前活动的上下文:
class CreateFileConnectToDatabase extends AsyncTask<String, Void, Integer> {
Activity activity;
Context context; //ELIMINA COGLIONE
String qResult = "";
public CreateFileConnectToDatabase(Activity activity){
this.activity = activity;
this.context = activity.getApplicationContext();
}
但是,尽管达到了onPostExecute方法,它仍然无法启动新活动。我是否无法从嵌套的异步类调用新活动?
答案 0 :(得分:0)
您必须在AsyncTask构造函数中传递上下文才能获得正确的活动上下文:
public CreateFileConnectToDatabase(Context context){
this.context = context.getApplicationContext();
}
并将其称为传递您的活动或片段上下文:
new CreateFileConnectToDatabase(yourcontext).execute();
O.T。评论//ELIMINA COGLIONE
让我离开了主席。