我感觉这是一个小错误的小错误。我正在尝试设置自定义相机,当我单击捕获按钮时,我得到一个空指针。也许我的相机实例是空的?我不确定为什么我非常仔细地遵循文档。发布下面的代码和错误,谢谢!
主要课程:
public class CameraActivity extends Activity {
private Camera mCamera;
private CameraPreview mPreview;
private String TAG = "CameraActivity";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.camera_preview);
// Create an instance of Camera
mCamera = getCameraInstance();
// Create our Preview view and set it as the content of our activity.
mPreview = new CameraPreview(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mPreview);
Button captureButton = (Button) findViewById(R.id.button_capture);
captureButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
// get an image from the camera
mCamera.takePicture(null, null, mPicture);
}
}
);
}
/** A safe way to get an instance of the Camera object. */
public static Camera getCameraInstance(){
Camera mCamera = null;
try {
mCamera = Camera.open(); // attempt to get a Camera instance
}
catch (Exception e){
// Camera is not available (in use or does not exist)
}
return mCamera; // returns null if camera is unavailable
}
@Override
protected void onPause() {
super.onPause();
if (mCamera != null){
mCamera.release(); // release the camera for other applications
mCamera = null;
}
}
@Override
protected void onResume() {
super.onResume();
mCamera = getCameraInstance();
}
private PictureCallback mPicture = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
Log.d(TAG, "Attempting to store picture...");
final int MEDIA_TYPE_IMAGE = 100;
File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
if (pictureFile == null){
Log.d(TAG, "Error creating media file, check storage permissions");
return;
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
} catch (FileNotFoundException e) {
Log.d(TAG, "File not found: " + e.getMessage());
} catch (IOException e) {
Log.d(TAG, "Error accessing file: " + e.getMessage());
}
}
};
猫咪日志:
03-27 01:13:06.892: E/AndroidRuntime(8008): FATAL EXCEPTION: main
03-27 01:13:06.892: E/AndroidRuntime(8008): java.lang.NullPointerException
03-27 01:13:06.892: E/AndroidRuntime(8008): at com.justbaumdev.tagsense.CameraActivity$2.onClick(CameraActivity.java:47)
03-27 01:13:06.892: E/AndroidRuntime(8008): at android.view.View.performClick(View.java:4204)
03-27 01:13:06.892: E/AndroidRuntime(8008): at android.view.View$PerformClick.run(View.java:17355)
03-27 01:13:06.892: E/AndroidRuntime(8008): at android.os.Handler.handleCallback(Handler.java:725)
03-27 01:13:06.892: E/AndroidRuntime(8008): at android.os.Handler.dispatchMessage(Handler.java:92)
03-27 01:13:06.892: E/AndroidRuntime(8008): at android.os.Looper.loop(Looper.java:137)
03-27 01:13:06.892: E/AndroidRuntime(8008): at android.app.ActivityThread.main(ActivityThread.java:5041)
03-27 01:13:06.892: E/AndroidRuntime(8008): at java.lang.reflect.Method.invokeNative(Native Method)
03-27 01:13:06.892: E/AndroidRuntime(8008): at java.lang.reflect.Method.invoke(Method.java:511)
03-27 01:13:06.892: E/AndroidRuntime(8008): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-27 01:13:06.892: E/AndroidRuntime(8008): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-27 01:13:06.892: E/AndroidRuntime(8008): at dalvik.system.NativeStart.main(Native Method)
预览课程:
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;
private int cameraId = 0;
private Context mContext;
private String TAG = "CameraPreview";
public CameraPreview(Context context, Camera camera) {
super(context);
mContext = context;
mCamera = camera;
//get notified if surface created/destroyed
mHolder = getHolder();
mHolder.addCallback(this);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, now tell the camera where to draw the preview.
//setCameraDisplayOrientation(mContext, mCamera, cameraId);
try {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
} catch (IOException e) {
Log.d(TAG, "Error setting camera preview: " + e.getMessage());
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
//Take care of this in cameraActivity
// mCamera.stopPreview();
// mCamera.release();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
if(mHolder.getSurface() == null)
return;
// stop preview before making changes
try {
mCamera.stopPreview();
} catch (Exception e){
// ignore: tried to stop a non-existent preview
}
//setCameraDisplayOrientation(mContext, mCamera, cameraId);
// start preview with new settings
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (Exception e){
Log.d(TAG, "Error starting camera preview: " + e.getMessage());
}
}
清单:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.justbaumdev.tagsense"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.NFC" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.nfc" />
<uses-feature android:name="android.hardware.wifi.direct" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.Holo" >
<activity
android:name=".TagSense"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.justbaumdev.tagsense.NFC">
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="application/com.justbaumdev.tagsense" />
</intent-filter>
</activity>
<activity android:name="AlbumGallery"></activity>
<activity android:name="CameraActivity"></activity>
</application>
</manifest>
答案 0 :(得分:2)
我发现至少有一个错误。
您在getCameraInstance()
和onCreate()
呼叫onResume()
,并在致电onResume()
后调用onCreate()
,这会导致两个camera.open()
没有camera.release()
。你应该在camera.open()
打电话给onResume()
。
http://developer.android.com/training/camera/cameradirect.html
答案 1 :(得分:1)
内
public static Camera getCameraInstance()
您正在初始化本地(方法)变量
Camera mCamera = null;
不是类实例变量(您打算)
private Camera mCamera;
更改以下行:
Camera mCamera = null;
到
this.mCamera = null;
注意:getCameraInstance()
不应该是静态方法。