2014-10-27 46 views
3

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

编辑:

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

    } 
} 

在此先感谢:由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); 

} 

不知道是否有用给出的错误!

+0

请将完整的错误添加到您的问题 – Paul 2014-10-27 21:54:04

+0

完成。 NullPointerException – Riekelt 2014-10-27 22:03:18

回答

0

您可能正在为您的Android Wear设备的dpi要求创建一个现有布局的版本,不是吗?

我敢打赌,你不必与你activity_main.xml中文件中的这些指定的IDS以下观点的一种或多种:

buttonConnect 
buttonDisconnect 
buttonReverse 
buttonLeft 
buttonForward 
buttonRight 

这将导致空指针异常。

+0

我在rect_activity_main.xml中创建了我的布局,因为那将是我所设想的屏幕。 WatchViewStub就位后,我保持activity_main不变。我会在那里试试,谢谢! – Riekelt 2014-10-27 22:02:55

+0

谢谢,它为我做了!不能触摸功能,因为我的树莓编译了一些东西! – Riekelt 2014-10-27 22:06:01

+0

酷!如果有帮助,请接受我的回答。问候,迈克 – 2014-10-27 22:08:08