简体   繁体   中英

How to stop my app from crashing using AsyncTask in Android Studio?

I have built a user interface in Android Studio to test the PSO algorithm in Java. I took this project on from someone else who did it last year, the person before me used AsyncTask with Boolean[] parameters to execute his application. Below is his version of this class, this is because he used a checkbox in his MainActivity that the user can check, so it can either be one or the other.

  public class runTests extends AsyncTask<Boolean, Void, Void> {
    @Override
    protected Void doInBackground(Boolean... params) {
        boolean one    = params[0];
        boolean custom = params[1];

        if (one)
            results = runTest("TestOne");
        else if (custom) {
            double[] re = runTest("customTest");
            if (re != null) results = re;
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void v) {
        // execution of result of Long time consuming operation
        pd.dismiss();
        if (results[0] != -1 || results != null) {
            loadIntent(results);
        }
    }

    @Override
    protected void onPreExecute() {
        pd = ProgressDialog.show(MainActivity.this, "Busy", "Algorithm is currently executing");
    }
}

Whereas my code doesn't have a checkbox it only needs to implement one test rather than having the option of two. I just want to run "CustomUseCase" and nothing else. I don't want to use a boolean parameter however I still want to have AsyncTask. Please help me!

public class runTests extends AsyncTask<Boolean, Void, Void> {
    @Override
    protected Void doInBackground(Boolean... params) { //sort this out
        boolean one    = params[0];
        boolean custom = params[1];

        if (one)
            results = runTest("CustomUseCase"); //i only want to run this one!!!
        else if (custom) {
            double[] re = runTest("testOne"); //I don't need this, I dont want to run this test
            if (re != null) results = re;
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void v) {
        // execution of result of Long time consuming operation
        if(pd!=null){
            pd.dismiss();
            pd = null;
        }
        if (results[0] != -1 || results != null) {
            loadIntent(results);
        }
    }
  @Override
    protected void onPreExecute() {
        pd = ProgressDialog.show(ParticleActivity.this, "Busy", "Algorithm is currently executing");
    }
}

First, you need to change

if (results[0] != -1 || results != null) {
    loadIntent(results);
}

to

if (results != null && results.length > 0 && results[0] != -1) {
    loadIntent(results);
}

That is an example of a " short circuiting " if statement. The logical AND (&&) operator will cause the entire statement to fail if results is null, otherwise it will evaluate the next logic statement results[0] != -1 with no chance of a NullPointerException.

Try this as well

public class runTests extends AsyncTask<Void, Void, Void> {
    @Override
    protected Void doInBackground(Void... params) { //sort this out

        results = runTest("CustomUseCase"); //i only want to run this one!!!
    }
    ...
}

UPDATE

do I return null when executing the doInBackground?

To clarify how the Template Type List (ie, <Void,Void,Void> ) works, here is an example. Here, we have < URL , Integer , Long >. Notice how doInBackground takes an array of URL (urls) and returns a Long , onProgressUpdate takes an Integer array, and onPostExecute takes a Long .

private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
     protected Long doInBackground(URL... urls) {
         long totalSize = urls.length;
         ...
         return totalSize;
     }

     protected void onProgressUpdate(Integer... progress) {
         setProgressPercent(progress[0]);
     }

     protected void onPostExecute(Long result) {
         showDialog("Downloaded " + result + " bytes");
     }
 }

For <Void, Void, Void> we would have (empty function argument list "()" corresponds to Void):

private class DownloadFilesTask extends AsyncTask<Void, Void, Void> {
     protected Void doInBackground() {             
         ...
         return;
     }

     protected void onProgressUpdate() {
         setProgressPercent("hi");
     }

     protected void onPostExecute() {
         showDialog("bye");
     }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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