[英]Method Undefined
Edit: My original question is below the line. 编辑:我的原始问题是在下面。 I decided to go with a much simpler approach to setting up a button and assigning a click function.
我决定采用一种更简单的方法来设置按钮和分配点击功能。 I found it at the following link.
我在以下链接中找到了它。 He does a good job of explaining the difference between the 2 approaches...
他很好地解释了这两种方法之间的区别。
Android User Interface Design: Basic Buttons Android用户界面设计:基本按钮
I realize this is a popular question, but in all of the examples I've looked at the problem seems to be a simple detail that's been overlooked, and the detail is never the same. 我意识到这是一个很普遍的问题,但是在我看过的所有示例中,这个问题似乎都是一个被忽略的简单细节,而且细节从来都不相同。 I'm sure this is basic.
我确定这是基本的。 I'm just starting out with programming for Android and this is a modification of existing code.
我刚开始使用Android编程,这是对现有代码的修改。
The app has one button on a blank page, and I want the button click to send an int to my Arduino via the Amarino API. 该应用程序在空白页上有一个按钮,我希望单击按钮可以通过Amarino API将整数发送到我的Arduino。 Here is my MainActivity code
这是我的MainActivity代码
package com.example.buttontest1;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import at.abraxas.amarino.Amarino;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
public class MainActivity extends Activity{
private Button button;
private static final String DEVICE_ADDRESS = "00:06:66:4B:E4:23";
public Context foo1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Amarino.connect(this, DEVICE_ADDRESS);
setContentView(R.layout.main);
addListenerOnButton();
}
public void addListenerOnButton() {
//Select a specific button to bundle it with the action you want
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Amarino.sendDataToArduino(this, DEVICE_ADDRESS, 'j', 1);
}
});
}
protected void onStop() {
super.onStop();
// stop Amarino's background service, we don't need it any more
Amarino.disconnect(this, DEVICE_ADDRESS);
}
}
The error I see is this, referring to line 38: 我看到的错误是这样,请参考第38行:
The method sendDataToArduino(Context, String, char, int) in the type Amarino is not applicable for the arguments (new View.OnClickListener(){}, String, char, int)
Amarino类型的sendDataToArduino(Context,String,char,int)方法不适用于参数(新的View.OnClickListener(){},String,char,int)
So there's a problem with the context and the method? 那么上下文和方法有问题吗?
Teh api expects the object of Context
but you pass this
to: 德API预计的目标
Context
,但你通过this
来:
Amarino.sendDataToArduino(this, DEVICE_ADDRESS, 'j', 1);
this is not an object of Context instead it is OnClickListener
object. 这不是Context的对象,而是
OnClickListener
对象。
Amarino.sendDataToArduino(this, DEVICE_ADDRESS, 'j', 1);
this
here refers to View.OnClickListener's
current instance. this
在这里指的是View.OnClickListener's
当前实例。 The compilation error basically says, sendDataToArduino()
expects the first argument as Context
but you are passing a OnClickListener
编译错误基本上说,
sendDataToArduino()
期望第一个参数为Context
但是您正在传递OnClickListener
sendDataToArduino
expects its first argument to be of type Context
. sendDataToArduino
期望其第一个参数为Context
类型。 You are passing it a View.onClickListener
. 您正在传递给它一个
View.onClickListener
。 Instead of passing this
as the first argument, try setting up a context as mentioned here and pass that as the first argument. 不要将
this
作为第一个参数传递,而是尝试按此处所述设置上下文并将其作为第一个参数传递。
Try adding the following in your onCreate method after the super call: 尝试在超级调用之后将以下内容添加到onCreate方法中:
MainActivity.context = getApplicationContext();
Also add the following method after onCreate: 还要在onCreate之后添加以下方法:
public static Context getAppContext() {
return MainActivity.context;
}
Now call the method with: 现在使用以下方法调用该方法:
Amarino.sendDataToArduino(getApppContext(), DEVICE_ADDRESS, 'j', 1);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.