Android视图:未捕获的处理程序:线程主因未捕获的异常而退出
Posted
技术标签:
【中文标题】Android视图:未捕获的处理程序:线程主因未捕获的异常而退出【英文标题】:Android View : Uncaught handler: thread main exiting due to uncaught exception 【发布时间】:2013-11-29 10:47:06 【问题描述】:我的应用在 androidVirtualDevice 上启动时出现问题 用户
包 com.test.imageviewproject;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import android.widget.ImageView;
public class MainActivity extends Activity
private static int RESULT_LOAD_IMAGE;
Bitmap monBitmapAMoi;
String imagePath;
// assignation des ids du menu
private static final int GALERIE_MENU_ID = Menu.FIRST;
private static final int LARGEUR_MENU_ID = Menu.FIRST + 1;
private static final int HAUTEUR_MENU_ID = Menu.FIRST + 2;
private static final int COULEURPXL_MENU_ID = Menu.FIRST + 3;
// private static final int BOUTONRETOUR_MENU_ID = Menu.FIRST + 4;
// assignation des éléments du menu
@Override
public boolean onCreateOptionsMenu(Menu parambmp)
super.onCreateOptionsMenu(parambmp);
parambmp.add(0, GALERIE_MENU_ID, 0, "Galerie").setShortcut('3', 'c');
parambmp.add(0, LARGEUR_MENU_ID, 0, "Largeur").setShortcut('4', 's');
parambmp.add(0, HAUTEUR_MENU_ID, 0, "Hauteur").setShortcut('5', 'z');
parambmp.add(0, COULEURPXL_MENU_ID, 0, "Couleur pxL").setShortcut('5', 'z');
// parambmp.add(0, BOUTONRETOUR_MENU_ID, 0, "Couleur pxL").setShortcut('6','z');
return true;
@Override
public boolean onPrepareOptionsMenu(Menu menu)
super.onPrepareOptionsMenu(menu);
return true;
// événements lancés selon les éléments du menu appelés
@Override
public boolean onOptionsItemSelected(MenuItem item)
switch (item.getItemId())
case GALERIE_MENU_ID:
Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i,RESULT_LOAD_IMAGE);
return true;
case LARGEUR_MENU_ID:
int w = monBitmapAMoi.getWidth();
Toast.makeText(getApplicationContext(),"Largeur bmp : " +w,Toast.LENGTH_SHORT).show();
return true;
case HAUTEUR_MENU_ID:
int h = monBitmapAMoi.getHeight();
Toast.makeText(getApplicationContext(),"Hauteur bmp : "+h,Toast.LENGTH_SHORT).show();
return true;
case COULEURPXL_MENU_ID:
int c = monBitmapAMoi.getPixel(10, 10);
Toast.makeText(getApplicationContext(),"Couleur du pixel en 10x10 : "+c,Toast.LENGTH_SHORT).show();
return true;
/* case BOUTONRETOUR_MENU_ID:
Toast.makeText(getApplicationContext(),"Hello Seicom",Toast.LENGTH_SHORT).show();
return true;*/
return super.onOptionsItemSelected(item);
public static class MyView extends View
/*private static final float MINP = 0.25f;
private static final float MAXP = 0.75f;*/
/*
* les 4 éléments de bases pour permettre le dessin
*/
private static Bitmap mBitmap;
private Canvas mCanvas;
private static Path mPath;
private static Paint mBitmapPaint;
private static Paint mPaint;
public MyView(Context c)
super(c);
// instanciation du path
mPath = new Path();
// instanciation du paint avec tramage à on.
// mBitmapPaint = new Paint(Paint.DITHER_FLAG);
protected void onSizeChanged(int w, int h, int oldw, int oldh)
super.onSizeChanged(w, h, oldw, oldh);
// creation du bitmap ou objet du trait
// mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
// instanciation du canvas
mCanvas = new Canvas(mBitmap);
/*
* methode appelée dynamiquement quand on dessine
*/
@Override
protected void onDraw(Canvas canvas)
// assignation de la couleur au canvas
//canvas.drawColor(0xFFAAAAAA);
// assignation du bitmap au canvas
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
// assignation du chemin au canvas
//canvas.drawPath(imagePath, mPaint);
public static void setBitmap (Bitmap bitmap)
mBitmap = bitmap;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
//ImageView monBitmapAMoi = new ImageView(this);
//monBitmapAMoi.setImageBitmap(BitmapFactory.decodeFile(imagePath));
setContentView(new MyView(this));
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data)
Uri selectedImage = data.getData();//recupère l'URI de l'image sélectionnée dans le onCreate(bundle)
String[] filePathColumn = MediaStore.Images.Media.DATA ;
Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
cursor.moveToFirst();
/*
* Gestion du path de l'image
*/
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String imagePath = cursor.getString(columnIndex);
cursor.close();
Bitmap test = Bitmap.createBitmap(BitmapFactory.decodeFile(imagePath));
MyView.setBitmap(test);
View img = new MyView (this);
setContentView(img);
// ImageView monBitmapAMoi = (ImageView) findViewById(R.id.imgView);
// monBitmapAMoi.setImageBitmap(BitmapFactory.decodeFile(imagePath)); //conversion de l'image affichée en bitmap
但是当应用程序启动时它每次都会崩溃,我没有在代码中找到我的错误。 Ecplise 也找不到错误:-/ 这是我的日志:
11-28 14:29:09.416: E/AndroidRuntime(1011): Uncaught handler: thread main exiting due to uncaught exception
11-28 14:29:09.436: E/AndroidRuntime(1011): java.lang.NullPointerException
11-28 14:29:09.436: E/AndroidRuntime(1011): at android.graphics.Canvas.<init>(Canvas.java:82)
11-28 14:29:09.436: E/AndroidRuntime(1011): at com.test.imageviewproject.MainActivity$MyView.onSizeChanged(MainActivity.java:122)
11-28 14:29:09.436: E/AndroidRuntime(1011): at android.view.View.setFrame(View.java:6897)
11-28 14:29:09.436: E/AndroidRuntime(1011): at android.view.View.layout(View.java:6824)
11-28 14:29:09.436: E/AndroidRuntime(1011): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
11-28 14:29:09.436: E/AndroidRuntime(1011): at android.view.View.layout(View.java:6830)
11-28 14:29:09.436: E/AndroidRuntime(1011): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)
11-28 14:29:09.436: E/AndroidRuntime(1011): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998)
11-28 14:29:09.436: E/AndroidRuntime(1011): at android.widget.LinearLayout.onLayout(LinearLayout.java:918)
11-28 14:29:09.436: E/AndroidRuntime(1011): at android.view.View.layout(View.java:6830)
11-28 14:29:09.436: E/AndroidRuntime(1011): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
11-28 14:29:09.436: E/AndroidRuntime(1011): at android.view.View.layout(View.java:6830)
11-28 14:29:09.436: E/AndroidRuntime(1011): at android.view.ViewRoot.performTraversals(ViewRoot.java:996)
11-28 14:29:09.436: E/AndroidRuntime(1011): at android.view.ViewRoot.handleMessage(ViewRoot.java:1633)
11-28 14:29:09.436: E/AndroidRuntime(1011): at android.os.Handler.dispatchMessage(Handler.java:99)
11-28 14:29:09.436: E/AndroidRuntime(1011): at android.os.Looper.loop(Looper.java:123)
11-28 14:29:09.436: E/AndroidRuntime(1011): at android.app.ActivityThread.main(ActivityThread.java:4363)
11-28 14:29:09.436: E/AndroidRuntime(1011): at java.lang.reflect.Method.invokeNative(Native Method)
11-28 14:29:09.436: E/AndroidRuntime(1011): at java.lang.reflect.Method.invoke(Method.java:521)
11-28 14:29:09.436: E/AndroidRuntime(1011): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
11-28 14:29:09.436: E/AndroidRuntime(1011): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
11-28 14:29:09.436: E/AndroidRuntime(1011): at dalvik.system.NativeStart.main(Native Method)
即使在看 logcat 我也不明白我的错误
我找不到我的错误,你能帮我吗?
【问题讨论】:
请贴出logcat错误及相关代码。谢谢。 【参考方案1】:正如你的 logcat 所指出的,你得到了由第 122 行 MainActivity.java 中的代码引起的空指针异常
11-28 14:29:09.436: E/AndroidRuntime(1011): java.lang.NullPointerException
11-28 14:29:09.436: E/AndroidRuntime(1011): at
com.test.imageviewproject.MainActivity$MyView.onSizeChanged(MainActivity.java:122)
【讨论】:
好的,谢谢。但是如何解决我的错误?请以上是关于Android视图:未捕获的处理程序:线程主因未捕获的异常而退出的主要内容,如果未能解决你的问题,请参考以下文章