2013-04-15 77 views
0

我在啓動Activity時收到nullPointerException,不知道爲什麼。任何人都可以告訴我嗎? 也許丟失了一些東西?KeyListener上的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() {

+0

在按下按鈕之後還是之前? – Bigflow

+0

請發佈您的佈局xml –

+2

btnLeft爲空,您是否先調用setContentView? – Nermeen

回答

1

的問題是,btnLeft

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

未綁定到你的佈局文件。請向我們展示layout.xml以及整個onCreate方法。

+0

非常感謝!這是我的注意力不集中...... – Stanislav86

0

需要

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

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

和檢查name是正確的setContentView(R.layout.name);

希望這有助於