在我的应用程序中,我试图通过调出相机来添加照片,以便用户可以添加他们想要的照片。拍摄照片后,会出现一个自定义对话框,以确保这是他们想要的照片。截至照片拍摄并接受后,相机停止。这是我的代码(位图在所有内容之外初始化)和错误......
public void pictureDialog()
{
Button picAdd;
Button picCancel;
ImageView imageView;
final Dialog customDialog = new Dialog(this);
picAdd = (Button) findViewById(R.id.picAdd);
picCancel = (Button) findViewById(R.id.picCancel);
imageView = (ImageView) findViewById(R.id.imageView);
customDialog.setContentView(R.layout.picture);
customDialog.setTitle("Add Picture");
if (cameraImage != null)
{
imageView.setImageBitmap(cameraImage);
picAdd.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
}
});
picCancel.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
}
});
customDialog.show();
}
}
public void camera()
{
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK)
{
if (requestCode == CAMERA_CODE)
{
cameraImage = (Bitmap) data.getExtras().get("data");
pictureDialog();
}
}
}
错误
07-24 12:24:38.393 17706-17706/com.customledsupply.ledaudit E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.customledsupply.ledaudit, PID: 17706
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=10, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.customledsupply.ledaudit/com.customledsupply.ledaudit.FixtureDescription}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)' on a null object reference
at android.app.ActivityThread.deliverResults(ActivityThread.java:3577)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3620)
at android.app.ActivityThread.access$1300(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1352)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)' on a null object reference
at com.customledsupply.ledaudit.FixtureDescription.pictureDialog(FixtureDescription.java:126)
at com.customledsupply.ledaudit.FixtureDescription.onActivityResult(FixtureDescription.java:164)
at android.app.Activity.dispatchActivityResult(Activity.java:6192)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3573)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3620)
at android.app.ActivityThread.access$1300(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1352)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
答案 0 :(得分:1)
问题是,Camera Intent返回NULL。你必须检查这个: onActivityResult returned from a camera, Intent null
Android文档中未对此进行描述。