0

当我调用 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)
4

4 回答 4

0

我认为相机有 null 因为Camera.open()如果设备没有后置相机,则返回 null。

阅读安卓文档

在设备上对此进行测试并将此代码放入 try catch 块并确保您已添加访问应用程序标签上方的相机所需的所有权限。

于 2013-03-16T19:33:15.603 回答
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();

    }
});
}
于 2013-03-18T21:46:29.293 回答
0

你还没有在你的 onCreate() 中使用 setcontentview() ...我猜那正在生成 nullpointerexception ...把​​你的 logcat 输出放到更多信息

于 2013-03-16T19:31:53.193 回答
0

这是因为您在onStop()中释放相机,并且一旦您返回活动并单击 imageButton,它会调用 flash() 方法。因此,在 flash() 方法中,它使用的相机将为空,因为您在切换到下一个 Activity 时在 inStop() 中释放了它。

作为解决方案,您必须在 Activity 的 onResume() 中重新初始化相机。

于 2016-02-26T18:26:15.477 回答