我找到了一个在手机上切换灯光的示例: http://www.mkyong.com/android/how-to-turn-onoff-camera-ledflashlight-in-android/
所以我为我的应用程序剪了一些代码并得到了一个错误。
03-02 21:31:28.066: E/AndroidRuntime(1591): FATAL EXCEPTION: main
03-02 21:31:28.066: E/AndroidRuntime(1591): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kaltech.led/com.kaltech.led.ActivityMAIN}: java.lang.NullPointerException
03-02 21:31:28.066: E/AndroidRuntime(1591): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
03-02 21:31:28.066: E/AndroidRuntime(1591): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
03-02 21:31:28.066: E/AndroidRuntime(1591): at android.app.ActivityThread.access$600(ActivityThread.java:130)
03-02 21:31:28.066: E/AndroidRuntime(1591): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
03-02 21:31:28.066: E/AndroidRuntime(1591): at android.os.Handler.dispatchMessage(Handler.java:99)
03-02 21:31:28.066: E/AndroidRuntime(1591): at android.os.Looper.loop(Looper.java:137)
03-02 21:31:28.066: E/AndroidRuntime(1591): at android.app.ActivityThread.main(ActivityThread.java:4745)
03-02 21:31:28.066: E/AndroidRuntime(1591): at java.lang.reflect.Method.invokeNative(Native Method)
03-02 21:31:28.066: E/AndroidRuntime(1591): at java.lang.reflect.Method.invoke(Method.java:511)
03-02 21:31:28.066: E/AndroidRuntime(1591): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-02 21:31:28.066: E/AndroidRuntime(1591): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-02 21:31:28.066: E/AndroidRuntime(1591): at dalvik.system.NativeStart.main(Native Method)
03-02 21:31:28.066: E/AndroidRuntime(1591): Caused by: java.lang.NullPointerException
03-02 21:31:28.066: E/AndroidRuntime(1591): at com.kaltech.led.ActivityMAIN.onCreate(ActivityMAIN.java:40)
03-02 21:31:28.066: E/AndroidRuntime(1591): at android.app.Activity.performCreate(Activity.java:5008)
03-02 21:31:28.066: E/AndroidRuntime(1591): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
03-02 21:31:28.066: E/AndroidRuntime(1591): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
03-02 21:31:28.066: E/AndroidRuntime(1591): ... 11 more
编辑:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bat_status = (ImageView)findViewById(R.id.bat_stat);
mySwitch = (Switch) findViewById(R.id.switch_signal);
Context context = this;
PackageManager pm = context.getPackageManager();
if (!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
Log.e("err", "Device has no camera!");
return;
}
camera = Camera.open();
final Parameters p = camera.getParameters();
mySwitch.setChecked(false);
mySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if(isChecked){ //EIN
bat_status.setImageResource(R.drawable.bat_signal_1);
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
camera.startPreview();
isLighOn = true;
}
else{ //AUS
bat_status.setImageResource(R.drawable.bat_signal_2);
p.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
camera.stopPreview();
isLighOn = false;
}
}
});
}
编辑2:
final参数p = camera.getParameters(); //第40行
答案 0 :(得分:1)
你在第40行得到 NullPointerException ,这是:
final Parameters p = camera.getParameters();
camera
对象很可能是null。您之前使用以下命令初始化它:
camera = Camera.open();
和Camera.open():
创建一个新的Camera对象以访问设备上的第一个后置摄像头。如果设备没有后置摄像头,则返回null。
检查您的相机是否为空并且仅在此时继续,例如:
camera = Camera.open();
if (camera != null) {
//normal code
}
else {
//ERROR, camera is null
}
你可能会这样做是因为:
你忘了添加
<uses-permission android:name="android.permission.CAMERA" />
在AndroidManifest中。