2013-03-16 72 views
0

當我調用flash()時,我得到一個空指針異常,更確切地說,我打開應用程序(它能正常工作,沒有強制關閉),然後我點擊菜單按鈕並選擇設置(啓動ActivitySettings)我回去它給了我空指針異常。Android NullPointerException onClick

P.S. (我知道問題出在閃存(),因爲我在logcat的閱讀)

public class MainActivity extends Activity { 
    private boolean isFlashOn = false; 
    private Camera camera; 
    public ImageButton imagebutton; 
    String r,t; 
    String scelta="btn0"; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     t=CheckPrefsThm(); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     ChooseThm(); 
     r = CheckPrefsImg(); 
     super.onCreate(savedInstanceState); 
     Decision(); 
     camera = Camera.open(); 
    } 

    @Override 
    public void onStart() { 
     super.onStart(); 
     imagebutton.setOnClickListener(new OnClickListener() { 
      public void onClick(View w) { 

       flash(); 

      } 
     }); 
    } 

    public void flash() { 
     final Parameters p = camera.getParameters(); 
     if(isFlashOn) { 
      p.setFlashMode(Parameters.FLASH_MODE_OFF); 
      camera.setParameters(p); 
      isFlashOn = false; 
      if(scelta.equals("btn1")) { 
       imagebutton.setImageResource(R.drawable.powerg); 
      } 
      if(scelta.equals("btn0")) { 
       imagebutton.setImageResource(R.drawable.traoff); 
      } 
      if(scelta.equals("btn2")) { 
       imagebutton.setImageResource(R.drawable.pushg); 
      } 
      if(scelta.equals("btn3")) { 
       imagebutton.setImageResource(R.drawable.playg1); 
      } 
      if(scelta.equals("btn4")) { 
       imagebutton.setImageResource(R.drawable.playg2); 
      } 
     } 
     else { 
      p.setFlashMode(Parameters.FLASH_MODE_TORCH); 
      camera.setParameters(p); 
      isFlashOn = true; 
      if(scelta.equals("btn1")) { 
       imagebutton.setImageResource(R.drawable.powerg); 
      } 
      if(scelta.equals("btn0")) { 
       imagebutton.setImageResource(R.drawable.traon); 
      } 
      if(scelta.equals("btn2")) { 
       imagebutton.setImageResource(R.drawable.pushgon); 
      } 
      if(scelta.equals("btn3")) { 
       imagebutton.setImageResource(R.drawable.playg1); 
      } 
      if(scelta.equals("btn4")) { 
       imagebutton.setImageResource(R.drawable.playg2); 
      } 
     } 
    } 

    @Override 
    protected void onStop() { 
     super.onStop(); 
     if (camera != null) { 
      camera.release(); 
      finish(); 
     } 
    } 

    @Override 
    public void onResume() { 
     super.onResume(); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.preferences, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     Intent startNewActivityOpen = new Intent(this, SettingsActivity.class);  
     switch(item.getItemId()) { 
      case R.id.menu_settings: 
       UpdatePrefs(r ,t); 
       startActivityForResult(startNewActivityOpen,0);     
       return true; 
      default: 
       return super.onOptionsItemSelected(item); 
     } 
    } 

    public String CheckPrefsImg() { 
     SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); 
     String result = prefs.getString("img","uno"); 

     return result; 
    } 

    public String CheckPrefsThm() { 
     SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); 
     String result = prefs.getString("thm","dark"); 

     return result; 
    } 

    public void UpdatePrefs(String img,String thm) { 
     SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); 
     SharedPreferences.Editor editor = prefs.edit(); 
     editor.putString("img",img); 
     editor.putString("thm",thm); 
     editor.commit(); 
    } 

    public void ChooseThm() { 
     if(t.equals("radiolight")) { 
      this.setTheme(R.style.Light); 
     } 
     else{ 
      if(t.equals("radiodark")) { 
       this.setTheme(R.style.Dark); 
      } 
     } 
    } 

    public void Decision() { 
     if(r.equals("uno")) { 
      setContentView(R.layout.activity_main); 
      imagebutton = (ImageButton)findViewById(R.id.torcia); 
      scelta="btn0"; 
     } 
     else if(r.equals("due")) { 
      setContentView(R.layout.activity_main2); 
      imagebutton = (ImageButton)findViewById(R.id.btn1); 
      scelta="btn1"; 
     } 
     else if(r.equals("tre")) { 
      setContentView(R.layout.activity_main3); 
      imagebutton = (ImageButton)findViewById(R.id.btn2); 
      scelta="btn2"; 
     } 
     else if(r.equals("quattro")) { 
      setContentView(R.layout.activity_main4); 
      imagebutton = (ImageButton)findViewById(R.id.btn3); 
      scelta="btn3"; 
     } 
     else if(r.equals("cinque")) { 
      setContentView(R.layout.activity_main5); 
      imagebutton = (ImageButton)findViewById(R.id.btn4); 
      scelta="btn4"; 
     } 
    } 
} 

編輯:這裏是我的logcat

03-16 13:47:21.174: D/dalvikvm(29612): Late-enabling CheckJNI 
03-16 13:47:21.204: E/jdwp(29612): Failed sending reply to debugger: Broken pipe 
03-16 13:47:21.204: D/dalvikvm(29612): Debugger has detached; object registry had 1 entries 
03-16 13:47:21.404: D/dalvikvm(29612): GC_FOR_ALLOC freed 111K, 6% free 12166K/  12931K, paused 33ms, total 33ms 
03-16 13:47:21.404: I/dalvikvm-heap(29612): Grow heap (frag case) to 12.899MB for 484456-byte allocation 
03-16 13:47:21.449: D/dalvikvm(29612): GC_CONCURRENT freed 1K, 7% free 12638K/  13447K, paused 19ms+1ms, total 45ms 
03-16 13:47:22.429: D/libEGL(29612): loaded /system/lib/egl/libEGL_mali.so 
03-16 13:47:22.434: D/libEGL(29612): loaded /system/lib/egl/libGLESv1_CM_mali.so 
03-16 13:47:22.439: D/libEGL(29612): loaded /system/lib/egl/libGLESv2_mali.so 
03-16 13:47:22.444: D/(29612): Device driver API match 
03-16 13:47:22.444: D/(29612): Device driver API version: 10 
03-16 13:47:22.444: D/(29612): User space API version: 10 
03-16 13:47:22.444: D/(29612): mali: REVISION=Linux-r2p4-02rel0 BUILD_DATE=Tue Oct 16 15:37:13 KST 2012 
03-16 13:47:22.579: D/OpenGLRenderer(29612): Enabling debug mode 0 
03-16 13:47:29.734: W/Camera(29612): Camera server died! 
03-16 13:47:44.749: D/AbsListView(29612): Get MotionRecognitionManager 
03-16 13:47:45.444: D/dalvikvm(29612): GC_CONCURRENT freed 108K, 6% free 12965K/ 13703K, paused 12ms+2ms, total 25ms 
03-16 13:47:45.474: E/SensorManager(29612): thread start 
03-16 13:47:45.474: D/SensorManager(29612): registerListener::handle=0name=LSM330DLC 3-axis Accelerometer delay= 200000Listener=android.view. 
[email protected] 
03-16 13:47:45.554: E/SpannableStringBuilder(29612): SPAN_EXCLUSIVE_EXCLUSIVE  spans cannot have a zero length 
03-16 13:47:45.554: E/SpannableStringBuilder(29612): SPAN_EXCLUSIVE_EXCLUSIVE  spans cannot have a zero length 
03-16 13:47:51.909: D/SensorManager(29612): unregisterListener:: Listener=android.view.OrientationEventListener 

$Sen[email protected] 
03-16 13:47:51.909: D/Sensors(29612): Remain listener = Sending .. normal delay  200ms 
03-16 13:47:51.909: I/Sensors(29612): sendDelay --- 200000000 
03-16 13:47:51.909: D/SensorManager(29612): JNI - sendDelay 
03-16 13:47:51.909: I/SensorManager(29612): Set normal delay = true 
03-16 13:47:53.319: W/Camera(29612): Camera server died! 
03-16 13:47:53.329: I/Choreographer(29612): Skipped 84 frames! The application may be doing too much work on its main thread. 
03-16 13:47:53.409: E/SpannableStringBuilder(29612): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 
03-16 13:47:53.409: E/SpannableStringBuilder(29612): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 
03-16 13:48:01.899: D/AndroidRuntime(29612): Shutting down VM 
03-16 13:48:01.899: W/dalvikvm(29612): threadid=1: thread exiting with uncaught exception (group=0x411542a0) 
03-16 13:48:01.904: E/AndroidRuntime(29612): FATAL EXCEPTION: main 
03-16 13:48:01.904: E/AndroidRuntime(29612): java.lang.NullPointerException 
03-16 13:48:01.904: E/AndroidRuntime(29612): at com.burattiluca.latorcia.MainActivity.flash(MainActivity.java:47) 
03-16 13:48:01.904: E/AndroidRuntime(29612): at com.burattiluca.latorcia.MainActivity$1.onClick(MainActivity.java:41) 
03-16 13:48:01.904: E/AndroidRuntime(29612): at android.view.View.performClick(View.java:4211) 
03-16 13:48:01.904: E/AndroidRuntime(29612): at android.view.View$PerformClick.run(View.java:17267) 
03-16 13:48:01.904: E/AndroidRuntime(29612): at android.os.Handler.handleCallback(Handler.java:615) 
03-16 13:48:01.904: E/AndroidRuntime(29612): at android.os.Handler.dispatchMessage(Handler.java:92) 
03-16 13:48:01.904: E/AndroidRuntime(29612): at android.os.Looper.loop(Looper.java:137) 
03-16 13:48:01.904: E/AndroidRuntime(29612): at android.app.ActivityThread.main(ActivityThread.java:4898) 
03-16 13:48:01.904: E/AndroidRuntime(29612): at java.lang.reflect.Method.invokeNative(Native Method) 
03-16 13:48:01.904: E/AndroidRuntime(29612): at java.lang.reflect.Method.invoke(Method.java:511) 
03-16 13:48:01.904: E/AndroidRuntime(29612): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773) 
03-16 13:48:01.904: E/AndroidRuntime(29612): at dalvik.system.NativeStart.main(Native Method) 
+3

Stacktrace請。 – 2013-03-16 19:26:29

+0

'flash()'中的哪行是MainActivity.java:47'?那是你的問題。 – Geobits 2013-03-16 20:48:36

+0

你重寫onActivityResult方法嗎? – 2013-03-16 21:35:46

回答

0

你還沒有使用的setContentView()在你的onCreate().. .i猜測是生成nullpointerexception ...把​​你的logcat輸出瞭解更多信息

+0

我使用setcontentview(),是在裏面決定()方法,事實上,當我打開應用程序我沒有問題,我得到空指針,當我從設置實體返回到這個活動 – user2177749 2013-03-16 20:08:47

0

我認爲相機是空的,因爲Camera.open()返回null如果設備沒有後置攝像頭。

閱讀android docs

在設備上測試此代碼,並將此代碼嘗試catch塊並確保已添加訪問應用程序標籤上方攝像頭所需的所有權限。

+0

我正在測試一個銀河s3 – user2177749 2013-03-16 20:04:32

+0

調試應用程序我想你會在相機中得到零。 – 2013-03-16 20:28:53

+0

我已經把onclicklistener與/ * * /之間的flash()方法,但應用程序仍然非常緩慢,當我回到主要活動,我得到fc ...所以我認爲問題不是閃光(),但是我發佈logcat。 – user2177749 2013-03-16 22:05:50

0

嘗試改用下面的代碼,看看它是否工作

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
// remove onClickListener  
imagebutton.setOnClickListener(null); 
Intent startNewActivityOpen = new Intent(this, SettingsActivity.class);  
switch(item.getItemId()){ 
    case R.id.menu_settings: 
      UpdatePrefs(r ,t); 
      startActivityForResult(startNewActivityOpen,0);     
      return true; 
    default: 
     return super.onOptionsItemSelected(item); 
} 

}

然後onActivityResult

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent intent) 
{ 
    super.onActivityResult(requestCode, resultCode, intent); 
    imagebutton.setOnClickListener(new OnClickListener(){ 
    public void onClick(View w){ 

    flash(); 

    } 
}); 
} 
+0

我試過但不起作用,我很確定問題是camera.release()需要太多才能釋放相機,我想我會從頭開始重建項目 – user2177749 2013-03-19 10:31:17

0

這是因爲你的onStop中釋放攝像頭()一旦你回來活動,並點擊imageButton,使cal升到flash()方法。因此,在flash()方法中,它將使用相機,因爲您在切換到下一個Activity時將其釋放到inStop()中,因此它將爲null。

作爲此解決方案,您必須在您的活動的onResume()中重新初始化相機。

相關問題