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

有目标就不怕路远。年轻人.无论你现在身在何方.重要的是你将要向何处去。只有明确的目标才能助你成功。没有目标的航船.任何方向的风对他来说都是逆风。因此,再遥远的旅程,只要有目标.就不怕路远。没有目标,哪来的劲头?一车尔尼雷夫斯基

导读:本篇文章讲解 关于安卓6.0系统出现Camera$Parameters android.hardware.Camera.getParameters()’ on a null object reference,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

具体问题如下:

  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" />

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/134138.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!