0

我正在接收nullPointerException启动活动,但不知道为什么。谁能告诉我?也许缺少了什么?

    btnLeft = (ImageButton)findViewById(R.id.btnLeft);
    btnLeft.setOnKeyListener(new OnKeyListener() {

        public boolean onKey(View v, int keyCode, KeyEvent event) {
            // TODO Auto-generated method stub
            if(event.getAction() == KeyEvent.ACTION_DOWN){
                JSONObject sendJSon = new JSONObject();
                try {
                    sendJSon.put("side", "1");
                    sendJSon.put("pushed", "1");
                    mConnection.sendTextMessage(sendJSon.toString());
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            } else if (event.getAction() == KeyEvent.ACTION_UP){
                JSONObject sendJSon = new JSONObject();
                try {
                    sendJSon.put("side", "1");
                    sendJSon.put("pushed", "0");
                    mConnection.sendTextMessage(sendJSon.toString());
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            return true;
        }
    });

日志猫:

04-15 15:02:13.113: E/AndroidRuntime(18722): FATAL EXCEPTION: main
04-15 15:02:13.113: E/AndroidRuntime(18722): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.autobahn/com.example.stas.ArcanoidActivity}: java.lang.NullPointerException
04-15 15:02:13.113: E/AndroidRuntime(18722):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
04-15 15:02:13.113: E/AndroidRuntime(18722):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
04-15 15:02:13.113: E/AndroidRuntime(18722):    at android.app.ActivityThread.access$600(ActivityThread.java:123)
04-15 15:02:13.113: E/AndroidRuntime(18722):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
04-15 15:02:13.113: E/AndroidRuntime(18722):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-15 15:02:13.113: E/AndroidRuntime(18722):    at android.os.Looper.loop(Looper.java:137)
04-15 15:02:13.113: E/AndroidRuntime(18722):    at android.app.ActivityThread.main(ActivityThread.java:4424)
04-15 15:02:13.113: E/AndroidRuntime(18722):    at java.lang.reflect.Method.invokeNative(Native Method)
04-15 15:02:13.113: E/AndroidRuntime(18722):    at java.lang.reflect.Method.invoke(Method.java:511)
04-15 15:02:13.113: E/AndroidRuntime(18722):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
04-15 15:02:13.113: E/AndroidRuntime(18722):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
04-15 15:02:13.113: E/AndroidRuntime(18722):    at dalvik.system.NativeStart.main(Native Method)
04-15 15:02:13.113: E/AndroidRuntime(18722): Caused by: java.lang.NullPointerException
04-15 15:02:13.113: E/AndroidRuntime(18722):    at com.example.stas.ArcanoidActivity.onCreate(ArcanoidActivity.java:75)
04-15 15:02:13.113: E/AndroidRuntime(18722):    at android.app.Activity.performCreate(Activity.java:4465)
04-15 15:02:13.113: E/AndroidRuntime(18722):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
04-15 15:02:13.113: E/AndroidRuntime(18722):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)

第 75 行:

btnLeft.setOnKeyListener(new OnKeyListener() {

4

2 回答 2

1

问题是btnLeft

btnLeft = (ImageButton)findViewById(R.id.btnLeft);

未绑定到您的布局文件。请向我们展示 layout.xml 以及整个onCreate方法。

于 2013-04-15T12:22:39.140 回答
0

需要

ImageButton btnLeft ;
btnLeft = (ImageButton)findViewById(R.id.btnLeft);

或者

ImageButton btnLeft= (ImageButton)findViewById(R.id.btnLeft);

并且检查name是正确的setContentView(R.layout.name);

希望有帮助

于 2013-04-15T12:21:52.170 回答