简体   繁体   English

Android AsyncTask [无法在未调用Looper.prepare()的线程内创建处理程序

[英]Android AsyncTask [Can't create handler inside thread that has not called Looper.prepare()]

I've created an image upload AsyncTask based on a function. 我已经基于一个函数创建了一个图像上传AsyncTask And after uploading, I get this error on onPostExecute() . 上传后,我在onPostExecute()上收到此错误。 I read up some StackOverflow answers on Runnable yet I kept getting the error over and over again despite implementing a different solution. 我在Runnable上读了一些StackOverflow的答案但是我不断地反复得到错误,尽管实现了不同的解决方案。

My code : 我的代码:

class uploadFile extends AsyncTask<String, String, String> {
    private ProgressDialog pDialog;

    /**
     * --------------------------------------------------------------------
     * --------------------------------- Before starting background thread
     * Show Progress Dialog
     */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(MainActivity.this);
        pDialog.setMessage("Uploading file");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    /**
     * --------------------------------------------------------------------
     * --------------------------------- getting all recent articles and
     * showing them in listview
     */
    @Override
    protected String doInBackground(String... args) {
        HttpURLConnection conn = null;
        DataOutputStream dos = null;
        DataInputStream inStream = null;
        String existingFileName = Environment.getExternalStorageDirectory()
                .getAbsolutePath() + "/mypic.png";
        String lineEnd = "\r\n";
        String twoHyphens = "--";
        String boundary = "*****";
        int bytesRead, bytesAvailable, bufferSize;
        byte[] buffer;
        int maxBufferSize = 1 * 1024 * 1024;
        String serverResponseMessage = "";
        String urlString = "http://google.info/imgupl/index.php";
        try {
            // ------------------ CLIENT REQUEST
            FileInputStream fileInputStream = new FileInputStream(new File(
                    existingFileName));
            // open a URL connection to the Servlet
            URL url = new URL(urlString);
            // Open a HTTP connection to the URL
            conn = (HttpURLConnection) url.openConnection();
            // Allow Inputs
            conn.setDoInput(true);
            // Allow Outputs
            conn.setDoOutput(true);
            // Don't use a cached copy.
            conn.setUseCaches(false);
            // Use a post method.
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setRequestProperty("Content-Type",
                    "multipart/form-data;boundary=" + boundary);
            dos = new DataOutputStream(conn.getOutputStream());
            dos.writeBytes(twoHyphens + boundary + lineEnd);
            dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\""
                    + existingFileName + "\"" + lineEnd);
            dos.writeBytes(lineEnd);
            // create a buffer of maximum size
            bytesAvailable = fileInputStream.available();
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            buffer = new byte[bufferSize];
            // read file and write it into form...
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);
            while (bytesRead > 0) {
                dos.write(buffer, 0, bufferSize);
                bytesAvailable = fileInputStream.available();
                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                bytesRead = fileInputStream.read(buffer, 0, bufferSize);
            }
            // send multipart form data necesssary after file data...
            dos.writeBytes(lineEnd);
            dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
            // close streams
            Integer serverResponseCode = conn.getResponseCode();
            serverResponseMessage = conn.getResponseMessage();
            Toast.makeText(getApplicationContext(), serverResponseMessage,
                    Toast.LENGTH_SHORT).show();
            Toast.makeText(getApplicationContext(),
                    serverResponseCode.toString(), Toast.LENGTH_SHORT)
                    .show();
            Log.e("Debug", "File is written");
            fileInputStream.close();
            dos.flush();
            dos.close();
        } catch (MalformedURLException ex) {
            Log.e("Debug", "error: " + ex.getMessage(), ex);
        } catch (IOException ioe) {
            Log.e("Debug", "error: " + ioe.getMessage(), ioe);
        }
        // ------------------ read the SERVER RESPONSE
        try {
            inStream = new DataInputStream(conn.getInputStream());

            while ((str = inStream.readLine()) != null) {
                Log.e("Debug", "Server Response " + str);
            }
            inStream.close();

        } catch (IOException ioex) {
            Log.e("Debug", "error: " + ioex.getMessage(), ioex);
        }
        return null;
    }

    /**
     * --------------------------------------------------------------------
     * --------------------------------- After completing background task
     * Dismiss the progress dialog
     **/
    protected void onPostExecute(String args) {
        // dismiss the dialog after getting all products
        pDialog.dismiss();
        MainActivity.this.runOnUiThread(new Runnable() {
            public void run() {
                Toast.makeText(MainActivity.this, "Hello", Toast.LENGTH_SHORT).show();
            }
        });

    }
}

My logcat : 我的logcat:

08-13 22:13:32.627: E/AndroidRuntime(9554): FATAL EXCEPTION: AsyncTask #1
08-13 22:13:32.627: E/AndroidRuntime(9554): java.lang.RuntimeException: An error occured while executing doInBackground()
08-13 22:13:32.627: E/AndroidRuntime(9554):     at android.os.AsyncTask$3.done(AsyncTask.java:200)
08-13 22:13:32.627: E/AndroidRuntime(9554):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
08-13 22:13:32.627: E/AndroidRuntime(9554):     at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
08-13 22:13:32.627: E/AndroidRuntime(9554):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
08-13 22:13:32.627: E/AndroidRuntime(9554):     at java.util.concurrent.FutureTask.run(FutureTask.java:138)
08-13 22:13:32.627: E/AndroidRuntime(9554):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
08-13 22:13:32.627: E/AndroidRuntime(9554):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
08-13 22:13:32.627: E/AndroidRuntime(9554):     at java.lang.Thread.run(Thread.java:1019)
08-13 22:13:32.627: E/AndroidRuntime(9554): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
08-13 22:13:32.627: E/AndroidRuntime(9554):     at android.os.Handler.<init>(Handler.java:121)
08-13 22:13:32.627: E/AndroidRuntime(9554):     at android.widget.Toast.<init>(Toast.java:68)
08-13 22:13:32.627: E/AndroidRuntime(9554):     at android.widget.Toast.makeText(Toast.java:231)
08-13 22:13:32.627: E/AndroidRuntime(9554):     at dev.google.imageupload.MainActivity$uploadFile.doInBackground(MainActivity.java:128)
08-13 22:13:32.627: E/AndroidRuntime(9554):     at dev.google.imageupload.MainActivity$uploadFile.doInBackground(MainActivity.java:1)
08-13 22:13:32.627: E/AndroidRuntime(9554):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
08-13 22:13:32.627: E/AndroidRuntime(9554):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
08-13 22:13:32.627: E/AndroidRuntime(9554):     ... 4 more

EDITED after zapl's suggestion : 在zapl的建议之后编辑:

08-13 22:38:06.297: E/AndroidRuntime(11511): FATAL EXCEPTION: AsyncTask #1
08-13 22:38:06.297: E/AndroidRuntime(11511): java.lang.RuntimeException: An error occured while executing doInBackground()
08-13 22:38:06.297: E/AndroidRuntime(11511):    at android.os.AsyncTask$3.done(AsyncTask.java:200)
08-13 22:38:06.297: E/AndroidRuntime(11511):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
08-13 22:38:06.297: E/AndroidRuntime(11511):    at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
08-13 22:38:06.297: E/AndroidRuntime(11511):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
08-13 22:38:06.297: E/AndroidRuntime(11511):    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
08-13 22:38:06.297: E/AndroidRuntime(11511):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
08-13 22:38:06.297: E/AndroidRuntime(11511):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
08-13 22:38:06.297: E/AndroidRuntime(11511):    at java.lang.Thread.run(Thread.java:1019)
08-13 22:38:06.297: E/AndroidRuntime(11511): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
08-13 22:38:06.297: E/AndroidRuntime(11511):    at android.os.Handler.<init>(Handler.java:121)
08-13 22:38:06.297: E/AndroidRuntime(11511):    at android.widget.Toast.<init>(Toast.java:68)
08-13 22:38:06.297: E/AndroidRuntime(11511):    at android.widget.Toast.makeText(Toast.java:231)
08-13 22:38:06.297: E/AndroidRuntime(11511):    at dev.google.imageupload.MainActivity$uploadFile.doInBackground(MainActivity.java:128)
08-13 22:38:06.297: E/AndroidRuntime(11511):    at dev.google.imageupload.MainActivity$uploadFile.doInBackground(MainActivity.java:1)
08-13 22:38:06.297: E/AndroidRuntime(11511):    at android.os.AsyncTask$2.call(AsyncTask.java:185)
08-13 22:38:06.297: E/AndroidRuntime(11511):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
08-13 22:38:06.297: E/AndroidRuntime(11511):    ... 4 more

You are attempting to update the UI from a background thread. 您正在尝试从后台线程更新UI。 Either move the toast to onPostExecute , which executes on the UI thread (recommended), or call runOnUiThread . 将toast移动到onPostExecute ,它在UI线程上执行(推荐),或者调用runOnUiThread

runOnUiThread(new Runnable() {
    public void run() {
        // runs on UI thread
    }
});

at dev.shaunidiot.imageupload.MainActivity$uploadFile.doInBackground(MainActivity.java:128) at dev.shaunidiot.imageupload.MainActivity $ uploadFile.doInBackground(MainActivity.java:128)

You could use the progress mechanism of AsyncTask s to update UI from inside doInBackground while the task is running: 您可以使用AsyncTask的进度机制在任务运行时从doInBackground内部更新UI:

replace 更换

Toast.makeText(getApplicationContext(), serverResponseMessage,
        Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(),
        serverResponseCode.toString(), Toast.LENGTH_SHORT)
        .show();

in doInBackground with doInBackground

publishProgress(serverResponseMessage, serverResponseCode.toString());

and add the following to your AsyncTask implementation 并将以下内容添加到AsyncTask实现中

@Override
protected void onProgressUpdate(String... values) {
    if (values != null) {
        for (String value : values) {
            // shows a toast for every value we get
            Toast.makeText(MainActivity.this, value, Toast.LENGTH_SHORT).show();
        }
    }
}

You already have set String as progress type in AsyncTask<Params, Progress, Result> so in case you want to use progress for something different you can try to use runOnUiThread but I don't know if that will work. 您已经在AsyncTask<Params, Progress, Result>中将String设置为进度类型,因此如果您想使用不同的进度,可以尝试使用runOnUiThread但我不知道这是否有效。

暂无
暂无

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

相关问题 Android AsyncTask无法在未调用Looper.prepare()的线程内创建处理程序? - Android AsyncTask Can't create handler inside thread that has not called Looper.prepare()? Android 2.3无法在未调用Looper.prepare()(AsyncTask)的线程内创建处理程序 - Android 2.3 Can't create handler inside thread that has not called Looper.prepare() (AsyncTask) 无法在 AsyncTask 内未调用 Looper.prepare() 的线程内创建处理程序 - Can't create handler inside thread that has not called Looper.prepare() inside AsyncTask 无法在未调用Looper.prepare()的线程内创建处理程序 - 对话框中的AsyncTask - Can't create handler inside thread that has not called Looper.prepare() - AsyncTask inside a dialog 无法在未在AsyncTask for ProgressDialog中调用Looper.prepare()的线程内创建处理程序 - Can't create handler inside thread that has not called Looper.prepare() inside AsyncTask for ProgressDialog 使用AsyncTask时无法在未调用Looper.prepare()的线程内创建处理程序 - Can't create handler inside thread that has not called Looper.prepare() when using AsyncTask asynctask中的CursorLoader,无法在未调用Looper.prepare的线程内创建处理程序 - CursorLoader in asynctask, can't create handler inside thread that has not called Looper.prepare 无法在 AsyncTask 中未调用 Looper.prepare() 的线程内创建处理程序 - Can't create handler inside thread that has not called Looper.prepare() in AsyncTask Asynctask导致异常“无法在未调用Looper.prepare()的线程内创建处理程序” - Asynctask causes exception 'Can't create handler inside thread that has not called Looper.prepare()' 无法在未调用looper.prepare()asynctask的线程内创建处理程序 - Can't create handler inside thread that has not called looper.prepare() asynctask
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM