图像视图上的空指针,不知道如何将其传递给视图

时间:2016-10-26 10:06:13

标签: android nullpointerexception

我有一个拍照的应用程序然后我想在拍摄时看到那张照片。这工作正常,我得到一张照片并将其保存到存储。所以我为它创建了一个名为“activity_image.xml。”但我得到了这个nullpointer:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)' on a null object reference

在这里,我尝试将其从主要活动传递给imageview,我无法弄清楚如何正确地做到这一点:

static final int REQUEST_TAKE_PHOTO = 1;
static final int REQUEST_IMAGE_CAPTURE = 1;
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
private Bitmap mImageBitmap;
private String mCurrentPhotoPath;
private ImageView mImageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_fullscreen);

    mVisible = true;
    mControlsView = findViewById(R.id.fullscreen_content_controls);
    mContentView = findViewById(R.id.fullscreen_content);
    sButton = findViewById(R.id.sButton);
    //final Intent cam = new Intent("android.media.action.IMAGE_CAPTURE");

    // Set up the user interaction to manually show or hide the system UI.
    mContentView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            toggle();
        }
    });

    sButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view){
            dispatchTakePictureIntent();
        }
    });

    // Upon interacting with UI controls, delay any scheduled hide()
    // operations to prevent the jarring behavior of controls going away
    // while interacting with the UI.
    findViewById(R.id.sButton).setOnTouchListener(mDelayHideTouchListener);
}

    private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    // Ensure that there's a camera activity to handle the intent
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        // Create the File where the photo should go
        File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
            // Error occurred while creating the File
            System.out.println("ERR CANNOT CREATE FILE");
        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            Uri photoURI = FileProvider.getUriForFile(this,
                    "com.example.android.fileprovider",
                    photoFile);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
        }
    }
}
private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);

    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );

    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = "file:" + image.getAbsolutePath();
    return image;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        try {
            mImageBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(mCurrentPhotoPath));

            mImageView = (ImageView)findViewById(R.id.imageView2);
            mImageView.setImageBitmap(mImageBitmap);


        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Activity_image.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:srcCompat="@mipmap/ic_launcher"
        android:id="@+id/imageView2"
        android:layout_weight="0.95" />
</LinearLayout>

4 个答案:

答案 0 :(得分:1)

更改此

mImageView.setImageBitmap(mImageBitmap);
mImageView = (ImageView)findViewById(R.id.imageView2);

 mImageView = (ImageView)findViewById(R.id.imageView2); mImageView.setImageBitmap(mImageBitmap);

您的imageview位于activity_image .xml中,但是您使用了activity_fullscreen.xml的oncreate方法,因此您必须在此处使用layout inflater并进行查看。 例如:view v; Imageview image=(Imageview)v.findViewById(R.id.imageView2);

答案 1 :(得分:1)

mImageView = (ImageView)findViewById(R.id.imageView2);
mImageBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(mCurrentPhotoPath));
                mImageView.setImageBitmap(mImageBitmap);

只需像这样重新编写代码 改变这个:

setContentView(R.layout.activity_image);

答案 2 :(得分:0)

替换

 mImageView.setImageBitmap(mImageBitmap);
 mImageView = (ImageView)findViewById(R.id.imageView2);

mImageView = (ImageView)findViewById(R.id.imageView2);
mImageView.setImageBitmap(mImageBitmap);

修改

setContentView(R.layout.activity_fullscreen);

因此,您的imageView2 ImageView应位于activity_fullscreen。但是什么是Activity_image.xml

答案 3 :(得分:0)

你的问题在这里:

mImageView = (ImageView)findViewById(R.id.imageView2); // first set the var
mImageView.setImageBitmap(mImageBitmap); // then set the image view

还要检查一下:

setContentView(R.layout.activity_fullscreen); // this should link to the right XML file. At the moment it looks like it doesn't.