gpt4 book ai didi

java - 将类的引用传递给另一个类android错误

转载 作者:行者123 更新时间:2023-12-02 00:46:57 26 4
gpt4 key购买 nike

我最近问过这个问题的前辈,得到了很好的答复。然而,当我在我的 Android 应用程序中使用它时,我遇到了一个意外的错误,并且想知道是否每个人都可以看一下我的代码并帮助我看看我做错了什么。

链接到最初的问题:passing reference of class to another class

我的错误:“构造函数 ConnectDevice(new View.OnClickListener(){}) 未定义”

以上是eclipse检测到的错误。

提前致谢!

以下是我的代码 fragment :

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

关于java - 将类的引用传递给另一个类android错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4720040/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com