Pass MotionEvent from callback function to a class method causing "Exception dispatching input event"

StackOverflow https://stackoverflow.com/questions/18560882

  •  27-06-2022
  •  | 
  •  

Question

I'm writing Android code to handle joystick input and the simple case works fine, but when I try to pass the MotionEvent to a class method, I get:

09-01 10:36:48.358: E/InputEventReceiver(8079): Exception dispatching input event. 
09-01 10:36:48.358: D/AndroidRuntime(8079): Shutting down VM  
09-01 10:36:48.358: W/dalvikvm(8079): threadid=1: thread exiting with uncaught exception (group=0x418f32a0) 
09-01 10:36:48.368: E/AndroidRuntime(8079): FATAL EXCEPTION: main  
09-01 10:36:48.368: E/AndroidRuntime(8079): java.lang.NullPointerException 
09-01 10:36:48.368: E/AndroidRuntime(8079):     at com.example.inputtest.MainActivity.onGenericMotionEvent(MainActivity.java:60) 
09-01 10:36:48.368: E/AndroidRuntime(8079):     at android.app.Activity.dispatchGenericMotionEvent(Activity.java:2435) 
09-01 10:36:48.368: E/AndroidRuntime(8079):     at 

The code that works:

package com.example.inputtest;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MotionEvent;
import android.widget.TextView;

public class MainActivity extends Activity {
    private TextView outp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        outp = (TextView) findViewById(R.id.outp);
        outp.setText((CharSequence) "No input yet");
    }

    @Override
    public boolean onGenericMotionEvent(MotionEvent mevt){
        outp.setText((CharSequence) "MotionEvent");
             if (mevt.getAction() == MotionEvent.ACTION_MOVE) {
                 outp.setText((CharSequence) String.valueOf(mevt.getAxisValue(MotionEvent.AXIS_X)));
                 return true;
             }
        return false;   
    }

}

The code that doesn't:

package com.example.inputtest;

import android.os.Bundle;
import android.app.Activity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MotionEvent;
import android.widget.TextView;

public class MainActivity extends Activity {
    private TextView outp;
    private wikipadController wikipadController = new wikipadController(); 

    private class wikipadController {
         private float leftJoyX, leftJoyY, rightJoyX, rightJoyY;

         public wikipadController(){
             leftJoyX =0;
             leftJoyY = 0;
             rightJoyX = 0;
             rightJoyY = 0;
         }

         public void update(MotionEvent mevt){
             leftJoyX=mevt.getAxisValue(MotionEvent.AXIS_X);
             leftJoyY=mevt.getAxisValue(MotionEvent.AXIS_Y);
             rightJoyX=mevt.getAxisValue(MotionEvent.AXIS_Z);
             rightJoyY=mevt.getAxisValue(MotionEvent.AXIS_RZ);
         }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        outp = (TextView) findViewById(R.id.outp);
        outp.setText((CharSequence) "No input yet");
    }

    @Override
    public boolean onGenericMotionEvent(MotionEvent mevt){
        outp.setText((CharSequence) "MotionEvent");
             if (mevt.getAction() == MotionEvent.ACTION_MOVE) {
                 wikipadController.update(mevt);
                 outp.setText(String.format("left: %d, %d right: %d, %d",
                         wikipadController.leftJoyX,
                         wikipadController.leftJoyY,
                         wikipadController.rightJoyX,
                         wikipadController.rightJoyY));
                 return true;
             }
        return false;
    }       
}

Can you not pass a MotionEvent? Do I need to copy it or something?

Was it helpful?

Solution

private float leftJoyX, leftJoyY, rightJoyX, rightJoyY;

leftJoyX, leftJoyY, rightJoyX, rightJoyY are declared as floats.

outp.setText(String.format("left: %d, %d right: %d, %d",
                     wikipadController.leftJoyX,
                     wikipadController.leftJoyY,
                     wikipadController.rightJoyX,
                     wikipadController.rightJoyY));

The format you use (%d) is for decimal. Use the following instead:

outp.setText(String.format("left: %f, %f right: %f, %f",
                     wikipadController.leftJoyX,
                     wikipadController.leftJoyY,
                     wikipadController.rightJoyX,
                     wikipadController.rightJoyY));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top