How to check if thread finished execution Android Studio

安稳与你 提交于 2020-01-24 22:12:31

问题


I am trying to create an app that contains 2 spinners with data from a data base. When a button is pressed, a new intent is created, showing the 2 spinners. The problem is that I have to create the DB queries in a new thread, and when I run the app, I get a null pointer exception (as far as my understanding goes, it is because the array in which I store the DB data is not yet populated). My question is, how can I delay the creation of the spinners until the queries from the DB are made? Below is a sample code of my implementation: The intent where I create the spinners and make a call to a class that makes the DB queries:

String [][] dbData; //the array where i store data from DB
getDBdata myData = new getDBdata(); // create a new instance of the class that queries the DB
dbData = myData.getData(); // I get the data
Log.e("My log: ", String.valueOf(dbData.length)); //and here it crashes, giving me a null pointer exception

And the class where I create a new thread to make a DB query:

public getDBdata()
{
    Thread thread = new Thread(new Runnable(){
        @Override
        public void run() {
            DBconn(); //make a DB query 
        }
    });
    thread.start();
}
public String[][] getData()
{
    return data;
}

回答1:


The easiest way is a using of AsyncTask. The basic idea of AsyncTask is splitting execution of your task into three steps which go one after another. Each step is running in a separate thread. The last one runs in the Main(UI) where you can create your spinners. Sample:

public class DBQueryTask extends AsyncTask<Void, Void, String[][]> {

    @Override
    protected String[][] doInBackground(Void... params) {
        DBconn();
        String[][] a;
        //...populating array
        return a;//returning populated array
    }

    @Override
    protected void onPostExecute(String[][] strings) {
        //strings - array already populated
        //this method runs on the Main thread. Therefore you can create your spinner
    }
}



回答2:


You don't need a new thread if you want to wait it to finish. But if you do, then you don't need to wait it to finish, but use a callback method instead

private static final int MESSAGE_DATA_RETRIEVED = 1;

private String [][] dbData; //the array where i store data from DB

private Handler mHandler;

// in onCreate or constructor depending what are you in

mHandler = new DataHandler(this);

// end of onCreate or constructor

private void getData() {
    final getDBdata myData = new getDBdata(mHandler);
    myData.getData();
}

private void onDataRetrieved() {
    Log.e("My log: ", String.valueOf(dbData.length));
}

private static final class DataHandler extends Handler {

    private final WeakReference<YourEnclosingClass> mClassReference;

    DataHandler(final YourEnclosingClass instance) {
        mClassReference = new WeakReference<>(instance);
    }

    @Override
    public void handleMessage(Message msg) {
        if (msg.what == MESSAGE_DATA_RETRIEVED) {
            final YourEnclosingClass instance = mClassReference.get();
            if (instance != null) {
                instance.onDataRetrieved();
            }
        }
    }

}

The getting data class

private final Handler mHandler;

// calling a class a verb is bad. Should be a noun, but I haven't took a time to rename it here
public getDBdata(final final Handler handler) {
    mHandler = handler;
    // starting a Thread in constructor is a bad idea. I moved it to a method
}

public void getData()
{
    final Thread thread = new Thread(new Runnable(){
        @Override
        public void run() {
            DBconn(); //make a DB query 
            nHandler.sendEmptyMessage(MESSAGE_DATA_RETRIEVED);
        }
    });
    thread.start();
}

That's how multithreading done.

But anyway that's a bad idea. You have to use AsyncTask or CursorLoader for your task instead.



来源:https://stackoverflow.com/questions/24228611/how-to-check-if-thread-finished-execution-android-studio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!