简体   繁体   English

无法将值从doInBackground赋值给主类

[英]Can't assign value to variable from doInBackground to main class

I have a android class where I initialise my variables: 我有一个android类,我初始化我的变量:

public class myClass extends Activity {
String itemIdString;
...
}

then I invoke AssyncTask in my onCreate method 然后我在onCreate方法中调用AssyncTask

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.next_item_caller);

            Log.d("Before " +itemName);
            new getItemNumber().execute();
            Log.d("After" +itemName);
            ...
     return "success";
    }

In AssyncTask I parse JSON object and assign values to variables: AssyncTask我解析JSON对象并为变量赋值:

 class getItemNumber extends AsyncTask<String, String, String> {
    protected void onPreExecute() {
        super.onPreExecute();
        Log.d("getItemNumber", "On pre-execute");
    }

protected String doInBackground(String... args) {
//parse here
itemIdString = c.getString(TAG_ITEM_ID);
Log.d(TAG, itemIdString);
}
 protected void onPostExecute(String result) {
        super.onPostExecute(result);
        Log.d("getItemNumber", "on Post-Execute");
    }

My Log in AssyncTask prints values as supposed to, but in onCreate before and after values are null? 我的登录AssyncTask打印的值应该是,但是onCreate 值为null 之前之后 How should I assign those strings so that I can access them from other methods as well? 我应该如何分配这些字符串,以便我也可以从其他方法访问它们? Thanks 谢谢

AsyncTask 's doInBackground runs on another thread. AsyncTaskdoInBackground在另一个线程上运行。

onCreate runs on the UI thread. onCreate在UI线程上运行。

Therefore when you call AsyncTask.execute() you don't wait for it to finish and move on with your next line which is 因此,当您调用AsyncTask.execute()您不会等待它完成并继续使用您的下一行

Log.d("After" +itemName);

To detect when doInBackground has finished, you have a convenience callback method onPostExecute : 为了检测 doInBackground完成后,你有一个方便的回调方法onPostExecute

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

    @Override
    protected Void doInBackground(Void... params) {
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        // doInBackground finished, use your variable now
    }

}

Return the string value from the doInBackgroind method and then assign it to itemIdString in the onPostExecute method which runs on the main thread 从doInBackgroind方法返回字符串值,然后将其分配给onPostExecute方法中的itemIdString,该方法在主线程上运行

class getItemNumber extends AsyncTask<String, String, String> {
    protected void onPreExecute() {
        super.onPreExecute();
        Log.d("getItemNumber", "On pre-execute");
    }

protected String doInBackground(String... args) {
    //parse here
    return c.getString(TAG_ITEM_ID);
}

 protected void onPostExecute(String result) 
   super.onPostExecute(result);
    //assign itemIdString here
   itemIdString = result;
}

There is also another way to do this is that you can check status of your AsyncTask as follows: 还有另一种方法可以检查AsyncTask状态,如下所示:

in onCreate() , you can check the status of AysncTask like this one: onCreate() ,您可以像这样检查AysncTask的状态:

MyAsyncTask myAsyncTask = new MyAsyncTask();
myAsyncTask.execute();

if(myAsyncTask.getStatus() == AsyncTask.Status.FINISHED){
        // MyAsyncTask completed. assign your variable now    
}

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

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