2014-03-02 49 views
1

我發現一個例子這裏在手機上切換光: 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

+0

您的NullPointerException發生在ActivityMAIN的第40行。這條線上有什麼?請發佈您的整個onCreate()方法。 – Melquiades

+0

好的,我加了 – user3339279

+0

哪一行是40行? – Melquiades

回答

1

你得到第40行NullPointerException異常,那就是:

final Parameters p = camera.getParameters(); 

最有可能的camera對象爲null。

camera = Camera.open(); 

和Camera.open():你與早期初始化它

創建一個新的Camera對象訪問第一個朝後相機的設備上。如果設備沒有後置攝像頭,則返回null。

檢查,如果你的相機是不是null,僅進行隨後,例如:

camera = Camera.open(); 

if (camera != null) { 
    //normal code 
} 
else { 
    //ERROR, camera is null 
} 

你可能會得到這樣的原因是:

  • 設備不具有背置攝像頭
  • 您忘記了添加

    <uses-permission android:name="android.permission.CAMERA" /> 
    

在您的AndroidManifest中。

+0

非常感謝你!真的可以理解 – user3339279

+0

不客氣。 – Melquiades