关于安卓6.0系统出现Camera$Parameters android.hardware.Camera.getParameters()' on a null object reference

具体问题如下:

  Process: com.beta.myapplication, PID: 32404
                                                                        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.beta.myapplication/com.beta.myapplication.ui.CameraActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.hardware.Camera$Parameters android.hardware.Camera.getParameters()' on a null object reference
                                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2612)
                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2672)
                                                                            at android.app.ActivityThread.access$900(ActivityThread.java:169)
                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1432)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                            at android.os.Looper.loop(Looper.java:171)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5751)
                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
                                                                         Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.hardware.Camera$Parameters android.hardware.Camera.getParameters()' on a null object reference
                                                                            at com.beta.myapplication.widget.Fragment.SettingsFragment.passCamera(SettingsFragment.java:48)
                                                                            at com.beta.myapplication.ui.CameraActivity.initCamera(CameraActivity.java:77)
                                                                            at com.beta.myapplication.ui.CameraActivity.onCreate(CameraActivity.java:26)
                                                                            at android.app.Activity.performCreate(Activity.java:6320)
                                                                            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1121)
                                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2565)
                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2672) 
                                                                            at android.app.ActivityThread.access$900(ActivityThread.java:169) 
                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1432) 
                                                                            at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                            at android.os.Looper.loop(Looper.java:171) 
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5751) 
                                                                            at java.lang.reflect.Method.invoke(Native Method) 
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732) 
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622) 

解决方法动态添加相机权限即可。
dependencies{

    implementation 'me.weyye.hipermission:library:1.0.7'

}
写入方法在你点击相机那个按钮处添加即可,如果不会用请参考一行代码搞定漂亮的Android6.0权限申请界面

//动态获取手机状态和发送短信权限
	private void Permissionx() {
		List<PermissionItem> permissions = new ArrayList<PermissionItem>();
		permissions.add(new PermissionItem(Manifest.permission.SEND_SMS, getString(R.string.permission_send_sms_phone), R.drawable.permission_ic_sms));
		permissions.add(new PermissionItem(Manifest.permission.READ_PHONE_STATE, getString(R.string.permission_read_phone_state), R.drawable.permission_ic_phone));
		permissions.add(new PermissionItem(Manifest.permission.CAMERA,"允许打开相机",R.drawable.permission_ic_camera));
		HiPermission.create(mContext)
				.title(getString(R.string.permission_cus_title))
				.permissions(permissions)
				.msg(getString(R.string.permission_cus_msg))
				.animStyle(R.style.PermissionAnimModal)
				.style(R.style.PermissionDefaultGreenStyle)
				.checkMutiPermission(new PermissionCallback() {
					@Override
					public void onClose() {
						Log.i(TAG, "onClose");
						showToast(getString(R.string.permission_on_close));
					}

					@Override
					public void onFinish() {
						showToast(getString(R.string.permission_completed));
					}

					@Override
					public void onDeny(String permission, int position) {
						Log.i(TAG, "onDeny");
					}

					@Override
					public void onGuarantee(String permission, int position) {
						Log.i(TAG, "onGuarantee");
					}
				});


	}
	private void showToast(String text) {//简化Toast
		Toast.makeText(mContext, text, Toast.LENGTH_SHORT).show();
	}
	public static final String TAG = "RegisterActivity";

别忘了在manifests中也添加权限

//开启相机的权限
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-feature android:name="android.hardware.camera" />

你可能感兴趣的:(安卓开发)