[英]passing reference of class to another class android error
我最近问了这个问题的先驱,得到了很好的答复。 但是,当我将其用于我的android应用程序时,出现了意外错误,想知道是否每个人都可以看一下我的代码并帮助我看看我在做什么错。
链接到最初的问题: 将类的引用传递给另一个类
我的错误:“构造函数ConnectDevice(new View.OnClickListener(){})未定义”
以上是eclipse检测到的错误。
提前致谢!
以下是我的代码段:
public class SmartApp extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.intro);
final Button connectDeviceButton = (Button) findViewById(R.id.connectDeviceButton);
connectDeviceButton.setOnClickListener(
new View.OnClickListener()
{
@Override
public void onClick(View v) {
Thread cThread = new Thread(new ConnectDevice(this));
cThread.start();
}
});
}
}
public class ConnectDevice implements Runnable {
private boolean connected;
private SmartApp smartAppRef;
private ObjectInputStream ois;
public ConnectDevice(SmartApp smartAppRef) {
this.smartAppRef = smartAppRef;
}
}
那是因为您传递了OnClickListener
对象,但是ConnectDevice
类的构造函数需要一个SmartApp
对象。 为什么? 您正在执行此操作:
new ConnectDevice(this);
在这种情况下, this
引用OnClickListener
。 更改为:
new ConnectDevice(SmartApp.this);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.