当我调用 flash() 时,我得到一个空指针异常,更准确地说,我打开了应用程序(它工作正常,没有强制关闭),然后我点击菜单按钮并选择设置(启动 ActivitySettings),当我返回时它给了我空指针异常。
PS(我知道问题出在 flash() 因为我是在 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.
OrientationEventListener$SensorEventListenerImpl@41e66718
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
$SensorEventListenerImpl@41e66718
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)