setOnClickListener崩溃我的磨损应用程序

时间:2014-10-27 21:51:42

标签: java android onclicklistener wear-os

我为我的Android手机制作了一个应用程序,通过Raspberry Pi控制机器人车。该代码有效,但现在我尝试将应用移植到Android Wear应用。但是,当我尝试在Wear设备上运行应用程序时,它会在启动时崩溃。从错误日志中,它来自SetOnClickListeners。我该如何解决这些错误?提前谢谢!

编辑:logcat给出的错误是RuntimeExceptions和NullPointerException http://puu.sh/ctfF4/49f0abe2ef.png

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    buttonConnect = (Button) findViewById(R.id.buttonConnect);
    buttonConnect.setOnClickListener(this);

    buttonDisconnect = (Button) findViewById(R.id.buttonDisconnect);
    buttonDisconnect.setOnTouchListener(this);

    buttonReverse = (Button) findViewById(R.id.buttonReverse);
    buttonReverse.setOnTouchListener(this);

    buttonLeft = (Button) findViewById(R.id.buttonLeft);
    buttonLeft.setOnTouchListener(this);

    buttonForward = (Button) findViewById(R.id.buttonForward);
    buttonForward.setOnTouchListener(this);

    buttonRight = (Button) findViewById(R.id.buttonRight);
    buttonRight.setOnTouchListener(this);

}

不确定是否有用:

@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouch(View v, MotionEvent event) {

    if ((R.id.buttonForward == v.getId())) {
        switch (event.getAction()) {

            case MotionEvent.ACTION_DOWN:
                socketCommand("forward");
                break;
            case MotionEvent.ACTION_UP:
                socketCommand("stop");
                break;
        }
    }

    else if ((R.id.buttonReverse == v.getId())) {
        switch (event.getAction()) {

            case MotionEvent.ACTION_DOWN:
                socketCommand("backwards");
                break;
            case MotionEvent.ACTION_UP:
                socketCommand("stop");
                break;
        }
    }

    else if ((R.id.buttonRight == v.getId())) {
        switch (event.getAction()) {

            case MotionEvent.ACTION_DOWN:
                socketCommand("right");
                break;
            case MotionEvent.ACTION_UP:
                socketCommand("stop");
                break;
        }
    }

    else if ((R.id.buttonLeft == v.getId())) {
        switch (event.getAction()) {

            case MotionEvent.ACTION_DOWN:
                socketCommand("left");
                break;
            case MotionEvent.ACTION_UP:
                socketCommand("stop");
                break;
        }
    }

    return false;
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.buttonConnect: {

            System.out.println("ONCLICK CONNECT");
            SocketConnect.connect();
            break;
        }
        case R.id.buttonDisconnect: {
            SocketConnect.closeConnection();
            break;
        }

    }
}

提前致谢!

1 个答案:

答案 0 :(得分:0)

您可能正在创建现有布局的某个版本以使用Android耗材设备的dpi要求,不是吗?

我打赌你在activity_main.xml文件中没有这些指定id的一个或多个以下视图:

buttonConnect
buttonDisconnect
buttonReverse
buttonLeft
buttonForward
buttonRight

这将导致NullPointerException。