doInBackground()中的致命异常AsyncTask#1

时间:2013-05-15 21:35:22

标签: android json android-asynctask httpclient futuretask

我是Android上的新手,我正在开发一个应用程序,从QR代码获取一些信息,然后连接一个php页面。然而,应用程序在读取QR代码后崩溃,而logCat在运行时会产生致命异常AsyncTask#1。

以下是doInBackground()代码:

class CreateNewProduct extends AsyncTask<String, String, String>
    { 
        protected String doInBackground(String... args)
        {
            String tc= new String();
            tc= "3123";

            String no = new String();
            no = "3";

            /*String tcKimlik = "3123";
            String hatNo = "3";*/

            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("tc", tc));
            params.add(new BasicNameValuePair("no", no ));

            JSONObject json = jsonParser.makeHttpRequest(url_create_product, "POST", params);

            try
            {
                int success = json.getInt(TAG_SUCCESS);

                if (success == 1)
                {

                    //qrCodeText.setText("DONE!");
                }
                else
                {
                    //qrCodeText.setText("Nope...");
                }
            }
            catch (JSONException e)
            {
                e.printStackTrace();
            }

            return null;
        }

    } // End of CreateNewProduct

CreateNewProduct在另一个读取QR码的类中,我在读取Qr代码后将其称为new CreateNewProduct().execute();

这是LogCat:

更新:

我添加http://并将setTexT()作为评论后,JSON错误被添加到LogCat中:

05-16 01:00:08.900: E/JSON Parser(11835): Error parsing data org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject

05-16 01:00:08.900: W/dalvikvm(11835): threadid=14: thread exiting with uncaught exception (group=0x410572a0)
05-16 01:00:08.910: E/AndroidRuntime(11835): FATAL EXCEPTION: AsyncTask #1
05-16 01:00:08.910: E/AndroidRuntime(11835): java.lang.RuntimeException: An error occured while executing doInBackground()
05-16 01:00:08.910: E/AndroidRuntime(11835):    at android.os.AsyncTask$3.done(AsyncTask.java:299)
05-16 01:00:08.910: E/AndroidRuntime(11835):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
05-16 01:00:08.910: E/AndroidRuntime(11835):    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
05-16 01:00:08.910: E/AndroidRuntime(11835):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
05-16 01:00:08.910: E/AndroidRuntime(11835):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
05-16 01:00:08.910: E/AndroidRuntime(11835):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
05-16 01:00:08.910: E/AndroidRuntime(11835):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
05-16 01:00:08.910: E/AndroidRuntime(11835):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
05-16 01:00:08.910: E/AndroidRuntime(11835):    at java.lang.Thread.run(Thread.java:856)
05-16 01:00:08.910: E/AndroidRuntime(11835): Caused by: java.lang.NullPointerException
05-16 01:00:08.910: E/AndroidRuntime(11835):    at com.example.yoritreader.MainActivity$CreateNewProduct.doInBackground(MainActivity.java:442)
05-16 01:00:08.910: E/AndroidRuntime(11835):    at com.example.yoritreader.MainActivity$CreateNewProduct.doInBackground(MainActivity.java:1)
05-16 01:00:08.910: E/AndroidRuntime(11835):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
05-16 01:00:08.910: E/AndroidRuntime(11835):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
05-16 01:00:08.910: E/AndroidRuntime(11835):    ... 5 more

感谢您的帮助!

3 个答案:

答案 0 :(得分:1)

Target host must not be null, or set in parameters. scheme=null, host=null, path=www.halilcosgun.com/qrcode.php

您可能忘记将协议放在网站的网址中。

尝试使用http://www.halilcosgun.com/qrcode.php

https://stackoverflow.com/a/8768930/1296658

答案 1 :(得分:1)

解决了从php文件中删除<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">的错误。

答案 2 :(得分:0)

我认为您当前的问题是,当您发送请求时,需要向"http://"添加url等。这应该照顾你当前的问题IllegalStateException。您的下一个问题是尝试从后台UI访问Thread个元素。您需要将调用setText()的行移至onPostExecute()Activity方法

由于您不允许从此行的背景中访问UI元素

qrCodeText.setText("DONE!");

以及您尝试更新UI的任何其他人都会给您一个错误。您可以return successonPostExecute()执行此操作,因为所有其他AsyncTask方法都在UI上运行。这假设他们可以访问这些元素,如同Activity的内部类或在构造函数中传递对它们的引用