简体   繁体   English

Android异步任务不起作用

[英]Android Async task not working

I am calling this from a function by creating an object.The progress box appears and then disappears but the toast is not displayed.(I have removed the code that follows the toast but even that code which queries the web page is not executed in the doinBackground) 我通过创建对象从函数中调用此函数。进度框出现然后消失,但不显示烤面包。(我删除了烤面包后面的代码,但即使查询网页的代码也没有在代码中执行doinBackground)

private class MyTask extends AsyncTask<Void, Void, Void> {

    MyTask(String p) {
        enter=p;
    }

    protected void onPreExecute() {
        progressDialog = ProgressDialog.show(CheckitoutActivity.this,"", "Loading. Please wait...", true);
    }

    protected Void doInBackground(Void... params) {
        try {
            Toast.makeText(getApplicationContext(), "GHJGFHGJL",1000).show();
         }
    }


     @Override
     protected void onPostExecute(Void result) {
         progressDialog.dismiss();
     }

}

The doInBackground does not apppear to be called. doInBackground似乎没有被调用。 Even the toast is not displayed.Why is that. 连烤面包都没有显示,这是为什么。

doInBackground method is non UI thread so it can't dispaly a toast doInBackground方法不是UI线程,因此无法显示吐司

So use runOnUIthread(this) or onProgressUpdate and are sure .execute() with object, like objMytask.execute(); 因此,请使用runOnUIthread(this)onProgressUpdate并确保将.execute()与对象(例如objMytask.execute();

Toast (You can't touch UI Thread from doInBack() that's why) is not works in doInBackground() so just put Log Toast (您不能从doInBack()触摸UI线程,这就是为什么)在doInBackground()doInBackground()因此只需将Log

protected Void doInBackground(Void... params) {
 try 
 {
  Log.e("AsyncTask","Inside the doInBackground()");
 }
}

Now check your logcat ... If ProgressDailog is showing then your AsyncTask is works well. 现在检查您的logcat ...如果显示ProgressDailog ,则您的AsyncTask运行良好。

you can not display the UI like toast and dialog alert in doInBackground method. 您无法在doInBackground方法中显示UI,如Toast和对话框警报。 instead you can show these UI in postExecute method of asynkTask 相反,您可以在asynkTask的postExecute方法中显示这些UI

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM