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;
        }
    });

LogCat:

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 に答える