【发布时间】:2018-03-12 04:18:05
【问题描述】:
这是我的代码:
我已经添加了一个 github 权限代码但它仍然崩溃
我做了所有的事情,但每次都崩溃
我还在清单中添加了相机权限
parameter = camera.getParameters();
}
@Override public void onPermissionDenied(PermissionDeniedResponse response) {
Toast.makeText(getApplicationContext(), "Permission Denied", Toast.LENGTH_SHORT).show();
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setMessage("App needs permission to access camera")
.setPositiveButton("Granted", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent myAppSettings = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.parse("package:" + getPackageName()));
myAppSettings.addCategory(Intent.CATEGORY_DEFAULT);
myAppSettings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(myAppSettings);
}
}).setNegativeButton("Denied", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
}).create().show();
}
@Override public void onPermissionRationaleShouldBeShown(PermissionRequest permission, PermissionToken token)
{[enter image description here][1]
token.continuePermissionRequest();
}
}).check();
//getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
textview = (TextView) findViewById(R.id.textView);
flashLight = (ImageButton) findViewById(R.id.flash_light);
// setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//askPermission(CAMERA,camera1);
flashLight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!isFlashLightOn) {
turnOnTheFlash();
} else {
turnOffTheFlash();
}
}
});
日志猫:
09-30 18:59:31.698 11339-11339/inducesmile.com.androidflashlightapp E/AndroidRuntime: 致命异常: main 进程:inductionsmile.com.androidflashlightapp,PID:11339 java.lang.RuntimeException:无法恢复活动 {inducesmile.com.androidflashlightapp/inducesmile.com.androidflashlightapp.MainActivity}:java.lang.RuntimeException:无法连接到相机服务 在 android.app.ActivityThread.performResumeActivity(ActivityThread.java:3506) 在 android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3546) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2795) 在 android.app.ActivityThread.-wrap12(ActivityThread.java) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1527) 在 android.os.Handler.dispatchMessage(Handler.java:110) 在 android.os.Looper.loop(Looper.java:203) 在 android.app.ActivityThread.main(ActivityThread.java:6251) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1073) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:934) 原因:java.lang.RuntimeException:无法连接到相机服务 在 android.hardware.Camera.(Camera.java:647) 在 android.hardware.Camera.open(Camera.java:510) 在inductionsmile.com.androidflashlightapp.MainActivity.turnOffTheFlash(MainActivity.java:105) 在inductionsmile.com.androidflashlightapp.MainActivity.onResume(MainActivity.java:165) 在 android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1269) 在 android.app.Activity.performResume(Activity.java:6791) 在 android.app.ActivityThread.performResumeActivity(ActivityThread.java:3477) 在 android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3546) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2795) 在 android.app.ActivityThread.-wrap12(ActivityThread.java) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1527) 在 android.os.Handler.dispatchMessage(Handler.java:110) 在 android.os.Looper.loop(Looper.java:203) 在 android.app.ActivityThread.main(ActivityThread.java:6251) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1073) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:934) 09-30 18:59:31.804 11339-11347/inducesmile.com.androidflashlightapp I/art: 进入while循环。
【问题讨论】:
-
请附加Logcat
-
在编辑问题时可以使用格式化工具。如果没有正确的格式,就很难阅读 logcat。
-
在许多潜在的重复中:stackoverflow.com/q/26305107/1531971所有这些相似的问题对您来说有何不同?
-
这是其余的代码:
-
private void turnOffTheFlash() { parameter.setFlashMode(Parameters.FLASH_MODE_OFF); this.camera.setParameters(参数); this.camera.stopPreview(); isFlashLightOn = 假; private void turnOnTheFlash() { if (camera != null) { parameter = this.camera.getParameters();参数.setFlashMode(Parameters.FLASH_MODE_TORCH); this.camera.setParameters(参数); this.camera.startPreview(); isFlashLightOn = 真; \
标签: android