0

我正在编写 Android 代码来处理操纵杆输入,简单的情况下工作正常,但是当我尝试将 MotionEvent 传递给类方法时,我得到:

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 

有效的代码:

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

}

没有的代码:

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

你不能通过 MotionEvent 吗?我需要复制它还是什么?

4

1 回答 1

0
private float leftJoyX, leftJoyY, rightJoyX, rightJoyY;

leftJoyX, leftJoyY, rightJoyX, rightJoyY被声明为浮点数。

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

您使用的格式 ( %d) 用于十进制。请改用以下内容:

outp.setText(String.format("left: %f, %f right: %f, %f",
                     wikipadController.leftJoyX,
                     wikipadController.leftJoyY,
                     wikipadController.rightJoyX,
                     wikipadController.rightJoyY));
于 2013-09-01T17:46:41.293 回答