Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android Asynctask和runOnUiThread_Java_Android_Android Asynctask - Fatal编程技术网

Java Android Asynctask和runOnUiThread

Java Android Asynctask和runOnUiThread,java,android,android-asynctask,Java,Android,Android Asynctask,我最近尝试实现这个项目openalpr,它在其原始代码中运行良好。但是当我试图修改它并添加一些函数时,我遇到了一些问题。 这是它的部分代码: AsyncTask.execute(new Runnable() { @Override public void run() { String result = OpenALPR.Factory.create(MainActivity.this, ANDROID_DATA_DI

我最近尝试实现这个项目openalpr,它在其原始代码中运行良好。但是当我试图修改它并添加一些函数时,我遇到了一些问题。 这是它的部分代码:

AsyncTask.execute(new Runnable() {
            @Override
            public void run() {
                String result = OpenALPR.Factory.create(MainActivity.this, ANDROID_DATA_DIR).recognizeWithCountryRegionNConfig("us", "", destination.getAbsolutePath(), openAlprConfFile, 10);

                Log.d("OPEN ALPR", result);

                try {
                    final Results results = new Gson().fromJson(result, Results.class);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (results == null || results.getResults() == null || results.getResults().size() == 0) {
                                Toast.makeText(MainActivity.this, "It was not possible to detect the licence plate.", Toast.LENGTH_LONG).show();
                                resultTextView.setText("It was not possible to detect the licence plate.");
                            } else {
                                resultTextView.setText("Plate: " + results.getResults().get(0).getPlate()
                                        // Trim confidence to two decimal places
                                        + " Confidence: " + String.format("%.2f", results.getResults().get(0).getConfidence()) + "%"
                                        // Convert processing time to seconds and trim to two decimal places
                                        + " Processing time: " + String.format("%.2f", ((results.getProcessingTimeMs() / 1000.0) % 60)) + " seconds");
                            }
                        }
这很有效。但当我试图在RunNuithRead之前获得“结果”时,它无法再正确检测。 下面是我修改过的代码:

AsyncTask.execute(new Runnable() {
            @Override
            public void run() {
                String result = OpenALPR.Factory.create(MainActivity.this, ANDROID_DATA_DIR).recognizeWithCountryRegionNConfig("us", "", destination.getAbsolutePath(), openAlprConfFile, 10);

                Log.d("OPEN ALPR", result);

                try {
                    final Results results = new Gson().fromJson(result, Results.class);
                    if(results!=null) Log.d("ShowTheResults",results.getResults().get(0).getPlate());
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (results == null || results.getResults() == null || results.getResults().size() == 0) {
                                Toast.makeText(MainActivity.this, "It was not possible to detect the licence plate.", Toast.LENGTH_LONG).show();
                                resultTextView.setText("It was not possible to detect the licence plate.");
                            } else {
                                resultTextView.setText("Plate: " + results.getResults().get(0).getPlate()
                                        // Trim confidence to two decimal places
                                        + " Confidence: " + String.format("%.2f", results.getResults().get(0).getConfidence()) + "%"
                                        // Convert processing time to seconds and trim to two decimal places
                                        + " Processing time: " + String.format("%.2f", ((results.getProcessingTimeMs() / 1000.0) % 60)) + " seconds");
                            }
                        }

我就是不明白为什么输出会与UI线程不同。在我将“结果”显示在UI上之前,有人能告诉我如何在后台正确使用它吗?

首先,我不确定您的异步任务实现,但我知道问题出在哪里。请检查下面的代码

AsyncTask.execute(new Runnable() {
            @Override
            public void run() {
                String result = OpenALPR.Factory.create(MainActivity.this, ANDROID_DATA_DIR).recognizeWithCountryRegionNConfig("us", "", destination.getAbsolutePath(), openAlprConfFile, 10);

                Log.d("OPEN ALPR", result);

                try {
                    final Results results = new Gson().fromJson(result, Results.class);
                    if(results!=null || results.getResults() != null || results.getResults().size() > 0) Log.d("ShowTheResults",results.getResults().get(0).getPlate());
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (results == null || results.getResults() == null || results.getResults().size() == 0) {
                                Toast.makeText(MainActivity.this, "It was not possible to detect the licence plate.", Toast.LENGTH_LONG).show();
                                resultTextView.setText("It was not possible to detect the licence plate.");
                            } else {
                                resultTextView.setText("Plate: " + results.getResults().get(0).getPlate()
                                        // Trim confidence to two decimal places
                                        + " Confidence: " + String.format("%.2f", results.getResults().get(0).getConfidence()) + "%"
                                        // Convert processing time to seconds and trim to two decimal places
                                        + " Processing time: " + String.format("%.2f", ((results.getProcessingTimeMs() / 1000.0) % 60)) + " seconds");
                            }
                        }

首先,我不确定您的异步任务实现,但我知道问题出在哪里。请检查下面的代码

AsyncTask.execute(new Runnable() {
            @Override
            public void run() {
                String result = OpenALPR.Factory.create(MainActivity.this, ANDROID_DATA_DIR).recognizeWithCountryRegionNConfig("us", "", destination.getAbsolutePath(), openAlprConfFile, 10);

                Log.d("OPEN ALPR", result);

                try {
                    final Results results = new Gson().fromJson(result, Results.class);
                    if(results!=null || results.getResults() != null || results.getResults().size() > 0) Log.d("ShowTheResults",results.getResults().get(0).getPlate());
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (results == null || results.getResults() == null || results.getResults().size() == 0) {
                                Toast.makeText(MainActivity.this, "It was not possible to detect the licence plate.", Toast.LENGTH_LONG).show();
                                resultTextView.setText("It was not possible to detect the licence plate.");
                            } else {
                                resultTextView.setText("Plate: " + results.getResults().get(0).getPlate()
                                        // Trim confidence to two decimal places
                                        + " Confidence: " + String.format("%.2f", results.getResults().get(0).getConfidence()) + "%"
                                        // Convert processing time to seconds and trim to two decimal places
                                        + " Processing time: " + String.format("%.2f", ((results.getProcessingTimeMs() / 1000.0) % 60)) + " seconds");
                            }
                        }

我认为您使用
AsyncTask
s的方式非常糟糕。您希望在异步线程上执行
Runnable
,然后在其中调用
runOnUiThread
,以处理UI线程上的结果。为什么不按预期的方式使用
AsyncTask
s

创建新任务并使用其常用方法:
doInBackground
onProgressUpdate
onPostExecute
。如果在主线程上执行AsyncTask,那么在UI线程上已经调用了最后两个

new AsyncTask<Void, Void, Results>() {
    @Override
    protected Results doInBackground(Void... params) {
        String result = OpenALPR.Factory.create(MainActivity.this, ANDROID_DATA_DIR).recognizeWithCountryRegionNConfig("us", "", destination.getAbsolutePath(), openAlprConfFile, 10);

        Log.d("OPEN ALPR", result);

       Results results = new Gson().fromJson(result, Results.class);
        if (results != null || results.getResults() != null || results.getResults().size() > 0)
            Log.d("ShowTheResults", results.getResults().get(0).getPlate());

        return results;
    }

    @Override
    protected void onPostExecute(Results result) {
        if (results == null || results.getResults() == null || results.getResults().size() == 0) {
            Toast.makeText(MainActivity.this, "It was not possible to detect the licence plate.", Toast.LENGTH_LONG).show();
            resultTextView.setText("It was not possible to detect the licence plate.");
        } else {
            resultTextView.setText("Plate: " + results.getResults().get(0).getPlate()
                    // Trim confidence to two decimal places
                    + " Confidence: " + String.format("%.2f", results.getResults().get(0).getConfidence()) + "%"
                    // Convert processing time to seconds and trim to two decimal places
                    + " Processing time: " + String.format("%.2f", ((results.getProcessingTimeMs() / 1000.0) % 60)) + " seconds");
        }
    }
}.execute();
newasynctask(){
@凌驾
受保护的结果在后台(无效…参数){
String result=OpenALPR.Factory.create(main activity.this,ANDROID_DATA_DIR)。识别国家区域配置(“us”,destination.getAbsolutePath(),openAlprConfFile,10);
Log.d(“打开ALPR”,结果);
Results=new Gson().fromJson(result,Results.class);
if(results!=null | | results.getResults()!=null | | results.getResults().size()>0)
Log.d(“ShowTheResults”,results.getResults().get(0.getPlate());
返回结果;
}
@凌驾
受保护的void onPostExecute(结果){
if(results==null | | results.getResults()==null | | results.getResults().size()==0){
Toast.makeText(MainActivity.this,“无法检测到车牌。”,Toast.LENGTH_LONG.show();
resultTextView.setText(“无法检测到车牌”);
}否则{
resultTextView.setText(“板块:+results.getResults().get(0.getPlate())
//将置信度减至小数点后两位
+置信度:“+String.format”(%.2f),results.getResults().get(0.getConfidence())+”%
//将处理时间转换为秒,并修剪为小数点后两位
+“处理时间:”+String.format(“%.2f”,((results.getProcessingTimes()/1000.0)%60))+“秒”);
}
}
}.execute();

我认为您使用异步任务的方式非常糟糕。您希望在异步线程上执行
Runnable
,然后在其中调用
runOnUiThread
,以处理UI线程上的结果。为什么不按预期的方式使用
AsyncTask
s

创建新任务并使用其常用方法:
doInBackground
onProgressUpdate
onPostExecute
。如果在主线程上执行AsyncTask,那么在UI线程上已经调用了最后两个

new AsyncTask<Void, Void, Results>() {
    @Override
    protected Results doInBackground(Void... params) {
        String result = OpenALPR.Factory.create(MainActivity.this, ANDROID_DATA_DIR).recognizeWithCountryRegionNConfig("us", "", destination.getAbsolutePath(), openAlprConfFile, 10);

        Log.d("OPEN ALPR", result);

       Results results = new Gson().fromJson(result, Results.class);
        if (results != null || results.getResults() != null || results.getResults().size() > 0)
            Log.d("ShowTheResults", results.getResults().get(0).getPlate());

        return results;
    }

    @Override
    protected void onPostExecute(Results result) {
        if (results == null || results.getResults() == null || results.getResults().size() == 0) {
            Toast.makeText(MainActivity.this, "It was not possible to detect the licence plate.", Toast.LENGTH_LONG).show();
            resultTextView.setText("It was not possible to detect the licence plate.");
        } else {
            resultTextView.setText("Plate: " + results.getResults().get(0).getPlate()
                    // Trim confidence to two decimal places
                    + " Confidence: " + String.format("%.2f", results.getResults().get(0).getConfidence()) + "%"
                    // Convert processing time to seconds and trim to two decimal places
                    + " Processing time: " + String.format("%.2f", ((results.getProcessingTimeMs() / 1000.0) % 60)) + " seconds");
        }
    }
}.execute();
newasynctask(){
@凌驾
受保护的结果在后台(无效…参数){
String result=OpenALPR.Factory.create(main activity.this,ANDROID_DATA_DIR)。识别国家区域配置(“us”,destination.getAbsolutePath(),openAlprConfFile,10);
Log.d(“打开ALPR”,结果);
Results=new Gson().fromJson(result,Results.class);
if(results!=null | | results.getResults()!=null | | results.getResults().size()>0)
Log.d(“ShowTheResults”,results.getResults().get(0.getPlate());
返回结果;
}
@凌驾
受保护的void onPostExecute(结果){
if(results==null | | results.getResults()==null | | results.getResults().size()==0){
Toast.makeText(MainActivity.this,“无法检测到车牌。”,Toast.LENGTH_LONG.show();
resultTextView.setText(“无法检测到车牌”);
}否则{
resultTextView.setText(“板块:+results.getResults().get(0.getPlate())
//将置信度减至小数点后两位
+置信度:“+String.format”(%.2f),results.getResults().get(0.getConfidence())+”%
//将处理时间转换为秒,并修剪为小数点后两位
+“处理时间:”+String.format(“%.2f”,((results.getProcessingTimes()/1000.0)%60))+“秒”);
}
}
}.execute();

AsyncTask.execute(新的Runnable(){
???从未见过这样的构造。谁发明了它?请解释你在做什么。你从哪里得到的?@greenapps我从中获取了这些。我也搜索了一段时间,最后得到了一点。我不会再关注这个链接。你不能在看到它的java文件上发布链接吗?你认为我们应该吗做这些工作和努力吗?
AsyncTask.execute(newrunnable()){
???从未见过这样的构造。谁发明了它?请解释你在做什么。你从哪里得到的?@greenapps我从中获取了这些。我也搜索了一段时间,最后得到了一点。我不会再关注这个链接。你不能在看到它的java文件上发布链接吗?你认为我们应该吗做的工作和努力?谢谢你的帮助。我只是一个android的初学者,所以我不知道什么是好方法。我会用普通的方法来尝试。好的,一旦尝试过,记得接受答案,如果它解决了你的问题。我试过了。它可以正常工作