Stanislav86
Stanislav86

Reputation: 23

NullPointerException on KeyListener

I'm receiving nullPointerException on start Activity and have no idea why. Can any one tell me? Maybe something is missing?

    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)

Line 75 :

btnLeft.setOnKeyListener(new OnKeyListener() {

Upvotes: 0

Views: 356

Answers (2)

Andrei Zhukouski
Andrei Zhukouski

Reputation: 3506

Need

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

or

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

and check is name is correct in setContentView(R.layout.name);

Hope it's help

Upvotes: 0

nano_nano
nano_nano

Reputation: 12523

The problem is that btnLeft

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

is not bound to your layout file. please show us the layout.xml as well as the whole onCreate method.

Upvotes: 1

Related Questions