2014-03-29 92 views
0

我的應用程序使用相機閃光燈,但我不明白爲什麼如果我打開另一個應用程序,然後恢復我的應用程序應用程序崩潰。我想因爲我沒有在應用中插入暫停和恢復。Flash Camera Crash

這是MainActivity:

public class MainActivity extends ActionBarActivity { 

//flag to detect flash is on or off 
    private boolean isLighOn = false; 

    private Camera camera; 

    private Button button; 

    private LinearLayout rl; 

    int i=0; 

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

     if (camera != null) { 
      camera.release(); 
     } 
    } 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     button = (Button) findViewById(R.id.buttonFlashlight); 
     rl=(LinearLayout) findViewById(R.id.container); 
     rl.setBackgroundColor(getResources().getColor(R.color.Black)); 


     Context context = this; 
     PackageManager pm = context.getPackageManager(); 

     // if device support camera? 
     if (!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) { 
      Log.e("err", "Device has no camera!"); 
      return; 
     } 

     camera = Camera.open(); 
     final Parameters p = camera.getParameters(); 

     button.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 

       if (isLighOn) { 

        Log.i("info", "torch is turn off!"); 

        p.setFlashMode(Parameters.FLASH_MODE_OFF); 
        camera.setParameters(p); 
        camera.stopPreview(); 
        isLighOn = false; 
        button.setBackgroundResource(R.drawable.off); 

        rl.setBackgroundColor(getResources().getColor(R.color.Black)); 

       } else { 

        Log.i("info", "torch is turn on!"); 

        p.setFlashMode(Parameters.FLASH_MODE_TORCH); 

        camera.setParameters(p); 
        camera.startPreview(); 
        isLighOn = true; 
        button.setBackgroundResource(R.drawable.on); 

        rl.setBackgroundResource(R.drawable.check); 

       } 

      } 
     }); 

    } 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 

    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, 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); 
} 
} 

的logcat:

03-29 21:24:51.048: D/AndroidRuntime(27113): Shutting down VM 
03-29 21:24:51.048: W/dalvikvm(27113): threadid=1: thread exiting with uncaught exception (group=0x418eeda0) 
03-29 21:24:51.058: E/AndroidRuntime(27113): FATAL EXCEPTION: main 
03-29 21:24:51.058: E/AndroidRuntime(27113): Process: com.example.torcia, PID: 27113 
03-29 21:24:51.058: E/AndroidRuntime(27113): java.lang.RuntimeException: Method called after release() 
+0

崩潰的堆棧跟蹤將很有用。 – Philio

+0

我加了logcat。 – ddd

回答

0

當你讓你的應用程序進入到了後臺,停止()被調用。然後你釋放你的相機。在此之後,您不能再使用它 - 您需要調用camera.open()並獲取新的相機對象。我建議不要在onCreate中創建相機對象,而只是在onStart(或onResume)中執行相機對象。

+0

你能舉個例子嗎? – ddd

0

問題是,當應用程序停止時釋放相機,但應用程序恢復時不會再次初始化相機。您的相機初始化代碼位於onCreate中,不會再被調用。

這可能是最好的初始化相機在onResume並釋放它onPause。

E.g.

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

    camera = Camera.open(); 
} 

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

    if (camera != null) { 
     camera.release(); 
    } 
} 
+0

你能舉個例子嗎? – ddd

+0

使用示例編輯答案 – Philio

+0

只要啓動應用程序,就會崩潰 – ddd