简体   繁体   English

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

[英]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. 我已经在Android Studio中构建了一个用户界面来测试Java中的PSO算法。 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. 我是去年与其他人一起完成这个项目的,而我之前的那个人使用带有Boolean []参数的AsyncTask来执行他的应用程序。 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. 下面是该类的版本,这是因为他在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");
    }
}

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. 我只想运行“ CustomUseCase”而已。 I don't want to use a boolean parameter however I still want to have AsyncTask. 我不想使用布尔参数,但是我仍然想要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. 那是if语句“ 短路 ”的一个例子。 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. 如果结果为null,则逻辑AND(&&)运算符将导致整个语句失败,否则它将评估下一个逻辑语句results[0] != -1 ,而没有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? 执行doInBackground时如何返回null?

To clarify how the Template Type List (ie, <Void,Void,Void> ) works, here is an example. 为了阐明模板类型列表(即<Void,Void,Void> )的工作方式,下面是一个示例。 Here, we have < URL , Integer , Long >. 在这里,我们有< URLIntegerLong >。 Notice how doInBackground takes an array of URL (urls) and returns a Long , onProgressUpdate takes an Integer array, and onPostExecute takes a Long . 请注意,doInBackground如何采用URL数组并返回Long ,onProgressUpdate采用Integer数组,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");
     }
 }

For <Void, Void, Void> we would have (empty function argument list "()" corresponds to Void): 对于<Void, Void, Void>我们将拥有(空函数参数列表“()”对应于Void):

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

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

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

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

相关问题 尝试使用ASyncTask发出GET请求时,如何防止我的应用程序崩溃? - How can I stop my app from crashing when trying to make a GET request with an ASyncTask? 使用后台方法从我的片段中读取 json 后,我的 android studio 应用程序不断崩溃 - My android studio app keeps crashing after using a background method to read a json from my fragment 从android studio安装的应用不断在我的手机上崩溃 - App installed from android studio keeps crashing on my phone android studio中的应用程序不断在我的手机上崩溃 - app from android studio keeps crashing on my phone 使用AsyncTask执行方法时Android App崩溃 - Android App is crashing when using AsyncTask execute method 如何停止asyncTask android - how to stop asyncTask android 在Android Studio上使用JavaScript时应用崩溃 - App crashing while using JavaScript on Android Studio 如何在从另一个应用程序切换回来后阻止我的应用程序崩溃? - How to stop my App from crashing after switching back from another app? 由于某种随机原因,我的应用程序在 android 工作室中不断崩溃 - my app keeps crashing in android studio for some random reason 我的 android studio 代码构建成功但输出应用程序崩溃 - My android studio code built successful but output app crashing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM