如何阻止我的应用在Android Studio中使用AsyncTask崩溃?

时间:2017-03-30 15:29:33

标签: java android android-asynctask

我在Android Studio中构建了一个用户界面来测试Java中的PSO算法。我从去年做过的其他人那里开始这个项目,我之前的人使用AsyncTask和Boolean []参数来执行他的应用程序。下面是他这个类的版本,这是因为他在他的MainActivity中使用了一个用户可以检查的复选框,因此它可以是一个或另一个。

  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");
    }
}

虽然我的代码没有复选框,但它只需要实现一个测试,而不是选择两个。我只想运行“CustomUseCase”而没有别的。我不想使用布尔参数,但我仍然想要AsyncTask。请帮帮我!

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");
    }
}

1 个答案:

答案 0 :(得分:0)

首先,您需要更改

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

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

这是&#34; 短路&#34;如果声明。如果结果为null,则逻辑AND(&amp;&amp;)运算符将导致整个语句失败,否则它将评估下一个逻辑语句 results[0] != -1 ,而不会出现NullPointerException。 / p>

试试这个

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!!!
    }
    ...
}

<强>更新

  

执行doInBackground时会返回null吗?

为了阐明模板类型列表(即<Void,Void,Void>)的工作原理,这里有一个例子。在这里,我们有&lt; URL Integer Long &gt;。请注意doInBackground如何获取 URL (网址)数组并返回 Long ,onProgressUpdate采用 Integer < / strong>数组,onPostExecute采用 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");
     }
 }

对于<Void, Void, Void>我们会有(空函数参数列表&#34;()&#34;对应于Void):

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

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

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