【问题标题】:passing reference of class to another class android error将类的引用传递给另一个类android错误
【发布时间】:2011-01-18 02:45:15
【问题描述】:

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

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

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

以上是eclipse检测到的错误。

提前致谢!

下面是我的代码sn-ps:

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

【问题讨论】:

    标签: java android class reference


    【解决方案1】:

    那是因为你传递了一个OnClickListener 对象,但是ConnectDevice 类的构造函数需要一个SmartApp 对象。为什么?你正在这样做:

    new ConnectDevice(this);
    

    在这种情况下,this 引用 OnClickListener。将其更改为:

    new ConnectDevice(SmartApp.this);
    

    【讨论】:

    • 如果您阅读我的第一个问题,您会更好地理解我想要做什么。我在这里的问题中将其链接起来。请指教。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-13
    • 1970-01-01
    • 1970-01-01
    • 2014-02-24
    • 2013-12-10
    • 2013-07-24
    相关资源
    最近更新 更多