2015-02-10 78 views
-2

我一直在嘗試使用圖像按鈕(初學者)創建基本的手電筒應用程序。在代碼中沒有語法錯誤的地方,在運行時確實有一些NULL指針異常。手電筒應用程序中的空指針異常

這是我的主要活動類: -

public class FlashLight extends Activity{ 

     Camera camera = null; 
     Parameters params = null; 
     boolean isFlashOn = false; 
     boolean hasFlash; 
     ImageButton btnSwitch = (ImageButton) findViewById(R.id.imageButton1); 

     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      hasFlash = getApplicationContext().getPackageManager() 
        .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH); 
      if(!hasFlash) 
      { 
       AlertDialog alert = new AlertDialog.Builder(FlashLight.this).create(); 
       alert.setTitle("Error"); 
       alert.setMessage("Application Not Supported"); 
       alert.setButton("OK", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         // closing the application 
         finish(); 
        } 
       }); 
       return; 
      } 
     // get the camera 
      getCamera(); 

      // displaying button image 
      toggleButtonImage(); 


      // Switch button click event to toggle flash on/off 
      btnSwitch.setOnClickListener(new View.OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        if (isFlashOn) { 
         // turn off flash 
         turnOffFlash(); 
        } else { 
         // turn on flash 
         turnOnFlash(); 
        } 
       } 
      }); 
     } 


     private void getCamera() { 
      if(camera == null) 
      { 
       try{ 
        camera = Camera.open(); 
        params = camera.getParameters(); 
       } 
       catch (RuntimeException e) { 
        Log.e("Camera Error. Failed to Open. Error: ", e.getMessage()); 
       } 
      } 
     } 

     private void toggleButtonImage() { 
      try{ 
      if(isFlashOn){ 
       btnSwitch.setImageResource(R.drawable.switchon); 
      }else{ 
       btnSwitch.setImageResource(R.drawable.switchoff); 
      } 
      } 
      catch(RuntimeException e){ 
       Log.e("Could not toggle Button image ", e.getMessage()); 
      } 
     } 

     private void turnOnFlash() { 
      try{ 
      if (!isFlashOn) { 
       if (camera == null || params == null) { 
        return; 
       } 

       params = camera.getParameters(); 
       params.setFlashMode(Parameters.FLASH_MODE_TORCH); 
       camera.setParameters(params); 
       camera.startPreview(); 
       isFlashOn = true; 

       // changing button/switch image 
       toggleButtonImage(); 
      } 
      } 
      catch(RuntimeException e){ 
       Log.e("Could not turn on Flash ", e.getMessage());   
      } 
     } 

     private void turnOffFlash() { 
      try{ 
      if (isFlashOn) { 
       if (camera == null || params == null) { 
        return; 
       } 

       params = camera.getParameters(); 
       params.setFlashMode(Parameters.FLASH_MODE_OFF); 
       camera.setParameters(params); 
       camera.stopPreview(); 
       isFlashOn = false; 

       // changing button/switch image 
       toggleButtonImage(); 
      } 
      } 
      catch(RuntimeException e){ 
       Log.e("Could not turn off flash ", e.getMessage());   
      } 
     } 
     @Override 
     public boolean onCreateOptionsMenu(Menu menu) { 
      // Inflate the menu; this adds items to the action bar if it is present. 
      getMenuInflater().inflate(R.menu.flash_light, menu); 
      return true; 
     } 

     @Override 
     public boolean onOptionsItemSelected(MenuItem item) { 
      // Handle action bar item clicks here. The action bar will 
      // automatically handle clicks on the Home/Up button, so long 
      // as you specify a parent activity in AndroidManifest.xml. 
      int id = item.getItemId(); 
      if (id == R.id.action_settings) { 
       return true; 
      } 
      return super.onOptionsItemSelected(item); 
     } 

     @Override 
     protected void onDestroy() { 
      super.onDestroy(); 
     } 

     @Override 
     protected void onPause() { 
      super.onPause(); 

      // on pause turn off the flash 
      turnOffFlash(); 
     } 

     @Override 
     protected void onRestart() { 
      super.onRestart(); 
     } 

     @Override 
     protected void onResume() { 
      super.onResume(); 
      // on resume turn on the flash 
      if(hasFlash) 
       turnOnFlash(); 
     } 

     @Override 
     protected void onStart() { 
      super.onStart(); 

      // on starting the app get the camera params 
      getCamera(); 
     } 

     @Override 
     protected void onStop() { 
      super.onStop(); 

      // on stop release the camera 
      if (camera != null) { 
       camera.release(); 
       camera = null; 
      } 
     } 
    } 

,而這裏是我的清單文件: -

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.flashlight" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="14" /> 

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

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="FlashLight" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

請在logcat的菜單下面的錯誤的鑑定幫助: -

02-10 23:18:23.409:E/AndroidRuntime(13237):了java.lang.RuntimeException:無法實例活動ComponentInfo {com.example.flashlight /共m.example.flashlight.FlashLight}:顯示java.lang.NullPointerException

+1

請提供異常回溯以及 – StenSoft 2015-02-10 18:20:31

+1

堆棧跟蹤,而不是「回溯」。 – 2015-02-10 18:45:17

回答

0

的ImageButton btnSwitch給你空指針,你沒有 的setContentView(R.layout.name_of_your_activity);

改變這一點,它的工作:

Camera camera = null; 
Camera.Parameters params = null; 
boolean isFlashOn = false; 
boolean hasFlash; 
ImageButton btnSwitch; //add this 



protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.name_of_your_xml_layout_for_activity);//add this 
    btnSwitch=(ImageButton)findViewById(R.id.imageButton1);//add this 

    hasFlash = getApplicationContext().getPackageManager() 
      .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);... 
+0

感謝噸哥們.....這工程....這提供了我更多的動機在Android開發進一步工作..... – 2015-02-11 18:40:00