当前位置: 首页 > 面试题库 >

运行时异常无法在未调用looper.prepare错误的线程内创建处理程序

郁鸿博
2023-03-14
问题内容

我正在尝试使用其他EditText上传照片。我从一个在线示例中获取了示例代码,并对其进行了少量编辑,但我收到此错误:

08-29 21:36:46.000: E/AndroidRuntime(4566): FATAL EXCEPTION: AsyncTask #1
08-29 21:36:46.000: E/AndroidRuntime(4566): java.lang.RuntimeException: An error occured while executing doInBackground()
08-29 21:36:46.000: E/AndroidRuntime(4566):     at android.os.AsyncTask$3.done(AsyncTask.java:299)
08-29 21:36:46.000: E/AndroidRuntime(4566):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
08-29 21:36:46.000: E/AndroidRuntime(4566):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
08-29 21:36:46.000: E/AndroidRuntime(4566):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
08-29 21:36:46.000: E/AndroidRuntime(4566):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-29 21:36:46.000: E/AndroidRuntime(4566):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
08-29 21:36:46.000: E/AndroidRuntime(4566):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
08-29 21:36:46.000: E/AndroidRuntime(4566):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
08-29 21:36:46.000: E/AndroidRuntime(4566):     at java.lang.Thread.run(Thread.java:856)
08-29 21:36:46.000: E/AndroidRuntime(4566): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
08-29 21:36:46.000: E/AndroidRuntime(4566):     at android.os.Handler.<init>(Handler.java:121)
08-29 21:36:46.000: E/AndroidRuntime(4566):     at android.widget.Toast$TN.<init>(Toast.java:361)
08-29 21:36:46.000: E/AndroidRuntime(4566):     at android.widget.Toast.<init>(Toast.java:97)
08-29 21:36:46.000: E/AndroidRuntime(4566):     at android.widget.Toast.makeText(Toast.java:254)
08-29 21:36:46.000: E/AndroidRuntime(4566):     at com.example.imageupload.ImageUpload$ImageUploadTask.doInBackground(ImageUpload.java:177)
08-29 21:36:46.000: E/AndroidRuntime(4566):     at com.example.imageupload.ImageUpload$ImageUploadTask.doInBackground(ImageUpload.java:1)
08-29 21:36:46.000: E/AndroidRuntime(4566):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
08-29 21:36:46.000: E/AndroidRuntime(4566):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
08-29 21:36:46.000: E/AndroidRuntime(4566):     ... 5 more

我的Android代码:

package com.example.imageupload;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.ByteArrayBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class ImageUpload extends Activity {
private static final int PICK_IMAGE = 1;
private ImageView imgView;
private Button upload;
private EditText TraineeID, ExamID, Invoiceno;
private Bitmap bitmap;
private ProgressDialog dialog;
private String url = "www.orgaar.com/androidlogin/imageupload.php";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.uploadimage);

    imgView = (ImageView) findViewById(R.id.ImageView);
    upload = (Button) findViewById(R.id.Upload);
    Invoiceno = (EditText) findViewById(R.id.Invoiceno);
    TraineeID = (EditText) findViewById(R.id.TraineeID);
    ExamID = (EditText) findViewById(R.id.ExamID); 
    upload.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            if (bitmap == null) {
                Toast.makeText(getApplicationContext(),
                        "Please select image", Toast.LENGTH_SHORT).show();
            } else {
                dialog = ProgressDialog.show(ImageUpload.this, "Uploading",
                        "Please wait...", true);
                new ImageUploadTask().execute();
            }
        }
    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.imageupload_menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
    case R.id.ic_menu_gallery:
        try {
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(
                    Intent.createChooser(intent, "Select Picture"),
                    PICK_IMAGE);
        } catch (Exception e) {
            Toast.makeText(getApplicationContext(),
                    e.getMessage(),
                    Toast.LENGTH_LONG).show();
            Log.e(e.getClass().getName(), e.getMessage(), e);
        }
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
    case PICK_IMAGE:
        if (resultCode == Activity.RESULT_OK) {
            Uri selectedImageUri = data.getData();
            String filePath = null;

            try {
                // OI FILE Manager
                String filemanagerstring = selectedImageUri.getPath();

                // MEDIA GALLERY
                String selectedImagePath = getPath(selectedImageUri);

                if (selectedImagePath != null) {
                    filePath = selectedImagePath;
                } else if (filemanagerstring != null) {
                    filePath = filemanagerstring;
                } else {
                    Toast.makeText(getApplicationContext(), "Unknown path",
                            Toast.LENGTH_LONG).show();
                    Log.e("Bitmap", "Unknown path");
                }

                if (filePath != null) {
                    decodeFile(filePath);
                } else {
                    bitmap = null;
                }
            } catch (Exception e) {
                Toast.makeText(getApplicationContext(), "Internal error",
                        Toast.LENGTH_LONG).show();
                Log.e(e.getClass().getName(), e.getMessage(), e);
            }
        }
        break;
    default:
    }
}


class ImageUploadTask extends AsyncTask<Void, Void, String> {
    @Override
    protected String doInBackground(Void... unsued) {
        try {
            HttpClient httpClient = new DefaultHttpClient();
            HttpContext localContext = new BasicHttpContext();
            HttpPost httpPost = new HttpPost(url);

            MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            bitmap.compress(CompressFormat.JPEG, 100, bos);
            byte[] data = bos.toByteArray();
            entity.addPart("returnformat", new StringBody("json"));
            entity.addPart("uploaded", new ByteArrayBody(data,"myImage.jpg"));
            entity.addPart("Invoiceno", new StringBody(Invoiceno.getText().toString()));
            entity.addPart("TraineeID", new StringBody(TraineeID.getText().toString()));
            entity.addPart("ExamID", new StringBody(ExamID.getText().toString()));
            httpPost.setEntity(entity);
            HttpResponse response = httpClient.execute(httpPost,
                    localContext);
            BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));

            String sResponse = reader.readLine();
            return sResponse;
            } catch (Exception e) {
            if (dialog.isShowing())
                dialog.dismiss();
            Toast.makeText(getApplicationContext(),
                    e.getMessage(),
                    Toast.LENGTH_LONG).show();
            Log.e(e.getClass().getName(), e.getMessage(), e);
            return null;
        }

        // (null);
    }

    @Override
    protected void onProgressUpdate(Void... unsued) {

    }

    @Override
    protected void onPostExecute(String sResponse) {
        try {
            if (dialog.isShowing())
                dialog.dismiss();

            if (sResponse != null) {
                JSONObject JResponse = new JSONObject(sResponse);
                int success = JResponse.getInt("SUCCESS");
                String message = JResponse.getString("MESSAGE");
                if (success == 0) {
                    Toast.makeText(getApplicationContext(), message,
                            Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(getApplicationContext(),
                            "Photo uploaded successfully",
                            Toast.LENGTH_SHORT).show();
                    Invoiceno.setText("");
                }
            }
        } catch (Exception e) {
            Toast.makeText(getApplicationContext(),
                    e.getMessage(),
                    Toast.LENGTH_LONG).show();
            Log.e(e.getClass().getName(), e.getMessage(), e);
        }
    }
}

public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    if (cursor != null) {
        // HERE YOU WILL GET A NULLPOINTER IF CURSOR IS NULL
        // THIS CAN BE, IF YOU USED OI FILE MANAGER FOR PICKING THE MEDIA
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } else
        return null;
}

public void decodeFile(String filePath) {
    // Decode image size

    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filePath, o);

    // The new size we want to scale to
    final int REQUIRED_SIZE = 1024;

    // Find the correct scale value. It should be the power of 2.
    int width_tmp = o.outWidth, height_tmp = o.outHeight;
    int scale = 1;
    while (true) {
        if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
            break;
        width_tmp /= 2;
        height_tmp /= 2;
        scale *= 2;
    }

    // Decode with inSampleSize
    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = scale;
    bitmap = BitmapFactory.decodeFile(filePath, o2);

    imgView.setImageBitmap(bitmap);

}
}

请告诉我这里是什么问题,我该如何解决呢?预先感谢我知道,常见的答案是我需要从UI线程调用Toast.makeText(…):很抱歉,因为我很新任何人都介意对此进行更详细的解释


问题答案:

可能由于此行而出现错误

 Toast.makeText(getApplicationContext(),
                    e.getMessage(),
                    Toast.LENGTH_LONG).show();

里面doInBackgroundToast必须显示在UIThread上。相反,您使用的是doInBackground线程来显示它。通常,有关UI的所有操作都必须在UI线程上执行。如果要修复,可以使用处理程序在显示Toast的UI线程队列上发布可运行对象



 类似资料:
  • 问题内容: 我收到此错误“无法在未调用Looper.prepare()的线程内创建处理程序” 你能告诉我如何解决吗? StartPayment方法: 问题答案: 您应该知道,当您尝试修改UI时, 唯一 可以执行此操作的线程是。 因此,如果要在另一个线程中修改UI,请尝试使用以下方法: 您的代码应如下所示:

  • 问题内容: 我正在使用AsyncTask调用yahooweather API。以下是代码: } 调试代码后,我发现yahooAPI调用成功,并且可以在函数中看到XML响应。但是,一旦执行完此功能,就会引发异常: 请帮帮我。 问题答案: 从方法中删除所有Toast的from,因为此方法是从of 调用的,并且您无法从后台线程访问Toast等Ui元素(也是Ui元素)。 注意: 如果您想知道后台发生了什么

  • 我在android中点击添加学生时出错了

  • 问题内容: 我不明白为什么会收到此错误。我正在使用AsyncTask在后台运行一些进程。 我有: 当我根据情况进入时: 每当我尝试我都会收到错误消息。 有想法吗? 问题答案: 该方法必须从被调用用户界面(UI)螺纹,而在不同的螺纹,其是主要的原因,运行被设计。 你必须调用无论是在或。 例如:

  • 我正在使用Google Maps V2 API开发一个简单的应用程序,只是为了获得基本知识,我面临着这个错误: 奇怪的是,我可以在我的模拟器(API16)上运行这个应用程序,它运行得很好,但当我试图在物理设备(API15)上运行它时,它得到了这个错误。AsyncTask在单独的类中运行,因此可能会导致此问题。如果有任何建议,我将不胜感激。提前谢了。

  • 问题内容: 我知道已经有很多这样的问题,但是我看不出我做错了什么。该应用程序崩溃,但未显示任何内容。另外,该错误不会在我的设备或仿真器上发生。仅在某些设备上(例如30-40%?)。 无法在未调用Looper.prepare()的线程内创建处理程序 MainActivity.java LogCat 问题答案: 如果您的线程不在监听循环程序,您如何管理发送给处理程序的请求? Doc说:public H