ERROR LINE:java.lang.NullPointerException: Attempt to invoke virtual
method 'void android.widget.ImageView.setOnClickListener(android.view.View$OnClickListener)'
on a null object reference
.JAVA FILE
package co.hangyr.Hangyr.Activity.Camera;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import butterknife.ButterKnife;
import butterknife.InjectView;
import co.hangyr.Hangyr.Activity.Home.ActivityHome;
import co.hangyr.Hangyr.Activity.Others.Footer;
import co.hangyr.Hangyr.R;
public class ActivityCamera extends Footer implements View.OnClickListener {
static final int RESULT_LOAD_IMAGE = 1;
private Camera mCamera;
private CameraPreview mCameraPreview;
ImageView imageView;
/* static final int SECOND_PIC = 2;
static final int THIRD_PIC = 3;
static final int FORTH_PIC = 4;
static final int FIFTH_PIC = 5;
*/
@InjectView(R.id.ivFirstPic)
ImageView firstPic;
@InjectView(R.id.ivCapture)
ImageView ivCapture;
@InjectView(R.id.ivFlipCamera)
ImageView flipCamera;
@InjectView(R.id.ivFlash)
ImageView flash;
@InjectView(R.id.tvUploadFromGallery)
TextView tvUploadFromGallery;
@InjectView(R.id.ivSecondPic)
ImageView secondPic;
@InjectView(R.id.ivThirdPic)
ImageView thirdPic;
@InjectView(R.id.ivForthPic)
ImageView forthPic;
@InjectView(R.id.ivFifthPic)
ImageView fifthPic;
@InjectView(R.id.ivJustClicked)
FrameLayout justClicked;
// @InjectView(R.id.tvNextEditPic)
TextView tvNext;
@InjectView(R.id.bDeleteFirstPic)
Button deleteFirstPic;
@InjectView(R.id.bDeleteSecondPic)
Button deleteSecondPic;
@InjectView(R.id.bDeleteThirdPic)
Button deleteThirdPic;
@InjectView(R.id.bDeleteForthPic)
Button deleteForthPic;
@InjectView(R.id.bDeleteFifthPic)
Button deleteFifthPic;
@InjectView(R.id.tvCancel)
TextView tvCancel;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.camera);
ButterKnife.inject(this);
tvNext = (TextView) findViewById(R.id.tvNextEditPic);
tvNext.setOnClickListener(this);
mCamera = getCameraInstance();
mCamera.setDisplayOrientation(90);
mCameraPreview = new CameraPreview(this, mCamera);
justClicked.addView(mCameraPreview);
ivCapture.setOnClickListener(this);
tvUploadFromGallery.setOnClickListener(this);
firstPic.setOnClickListener(this);
deleteFirstPic.setOnClickListener(this);
deleteSecondPic.setOnClickListener(this);
deleteThirdPic.setOnClickListener(this);
deleteForthPic.setOnClickListener(this);
deleteFifthPic.setOnClickListener(this);
tvCancel.setOnClickListener(this);
}
private Camera getCameraInstance() {
Camera camera = null;
try {
camera = Camera.open();
flipCamera.setVisibility(View.VISIBLE);
flash.setVisibility(View.VISIBLE);
} catch (Exception e) {
// cannot get camera or does not exist
}
return camera;
}
Camera.PictureCallback mPicture = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
File pictureFile = getOutputMediaFile();
if (pictureFile == null) {
return;
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
firstPic.setImageBitmap(bmp);
fos.write(data);
fos.close();
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
}
};
private static File getOutputMediaFile() {
File mediaStorageDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
"MyCameraApp");
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d("MyCameraApp", "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
.format(new Date());
File mediaFile;
mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "IMG_" + timeStamp + ".jpg");
return mediaFile;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.ivCapture:
mCamera.takePicture(null, null, mPicture);
break;
case R.id.ivFirstPic:
mCameraPreview.getHolder().removeCallback(mCameraPreview);
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
deleteFirstPic.setVisibility(View.VISIBLE);
break;
case R.id.tvCancel:
startActivity(new Intent(getApplicationContext(), ActivityHome.class));
break;
case R.id.tvNextEditPic:
startActivity(new Intent(getApplicationContext(), UploadPic.class));
break;
case R.id.bDeleteFirstPic:
firstPic.setImageResource(R.drawable.pic_item_cam);
deleteFirstPic.setVisibility(View.GONE);
break;
case R.id.bDeleteSecondPic:
secondPic.setImageResource(R.drawable.pic_item_cam);
deleteSecondPic.setVisibility(View.GONE);
break;
case R.id.bDeleteThirdPic:
thirdPic.setImageResource(R.drawable.pic_item_cam);
deleteThirdPic.setVisibility(View.GONE);
break;
case R.id.bDeleteForthPic:
forthPic.setImageResource(R.drawable.pic_item_cam);
deleteForthPic.setVisibility(View.GONE);
break;
case R.id.bDeleteFifthPic:
fifthPic.setImageResource(R.drawable.pic_item_cam);
deleteFifthPic.setVisibility(View.GONE);
}
}
@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();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
firstPic.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
}
.XML FILE
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/backgroundApp"
android:orientation="vertical"
android:weightSum="2">
<LinearLayout
android:id="@+id/llNameBack"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginBottom="1dp"
android:background="@color/white"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:id="@+id/tvCancel"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_gravity="left|center"
android:layout_marginBottom="15dp"
android:paddingLeft="15dp"
android:layout_marginTop="14dp"
android:layout_weight="1"
android:gravity="left|center_vertical"
android:text="@string/cancelS"
android:textColor="@color/black"
android:textSize="13dp"
android:textStyle="bold" />
<TextView
android:id="@+id/tvNextEditPic"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_gravity="end|center_vertical"
android:layout_marginBottom="15dp"
android:paddingRight="15dp"
android:layout_marginTop="14dp"
android:layout_weight="1"
android:background="@color/white"
android:gravity="right|center_vertical"
android:text="@string/nextS"
android:textColor="#fd676a"
android:textSize="13dp"
android:textStyle="bold" />
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="375dp">
<FrameLayout
android:id="@+id/ivJustClicked"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:layout_margin="5dp"
android:layout_weight="1">
<ImageView
android:id="@+id/ivFlipCamera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|bottom"
android:padding="8dp"
android:src="@drawable/flip_camera" />
<ImageView
android:id="@+id/ivFlash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|bottom"
android:padding="8dp"
android:src="@drawable/flash_icon" />
</FrameLayout>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:orientation="vertical">
<TextView
android:id="@+id/tvUploadFromGallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/white"
android:padding="18dp"
android:text="@string/galleryS"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#4a4a4a"
android:textSize="13dp"
android:textStyle="bold" />
<ImageView
android:id="@+id/ivCapture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="16dp"
android:layout_marginTop="-30dp"
android:src="@drawable/capture" />
<include
layout="@layout/camera_pic_added"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginTop="1dp"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
答案 0 :(得分:0)
您应该使用@Bind
代替@InjectView
@InjectView于2015年6月更换。
答案 1 :(得分:0)
您错过了初始化此变量ivFirstPic
:
ivFirstPic.setOnClickListener(this);