将捕获图像数据插入片段中的图像视图时出错



我想将照片从设备相机显示到图像视图 我之前在活动中做过,但是当我在片段上尝试时,我发现了这个问题:

java.lang.RuntimeException: Failure delivering result
ResultInfo{who=null, request=1888, result=-1, data=Intent {
act=inline-data (has extras) }} to activity
{com.example.uhf/com.example.uhf.activity.UHFMainActivity}:
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:4295)

我的代码太简单了:

ImageView person_photo;
private static final int CAMERA_REQUEST = 1888;


Button open_camera = (Button)getView().findViewById(R.id.take_photo);
open_camera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i,CAMERA_REQUEST);
}
});


@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode != RESULT_CANCELED){
if (requestCode == CAMERA_REQUEST) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
person_photo.setImageBitmap(photo);
}
}
}

获取person_image视图的引用。

person_photo= (ImageView)findViewById(R.id.person_photo_id);

我希望有效!

最新更新