我發現一個例子這裏在手機上切換光: http://www.mkyong.com/android/how-to-turn-onoff-camera-ledflashlight-in-android/手電筒ON/OFF應用程序崩潰
所以我剪掉了一些代碼爲我的應用程序,並得到了一個錯誤。
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:
最終參數P = camera.getParameters(); //線40
您的NullPointerException發生在ActivityMAIN的第40行。這條線上有什麼?請發佈您的整個onCreate()方法。 – Melquiades
好的,我加了 – user3339279
哪一行是40行? – Melquiades