【问题标题】:android the return type is incompatible with Thread.getState()android 返回类型与 Thread.getState() 不兼容
【发布时间】:2013-04-26 14:46:10
【问题描述】:

我正在关注一个关于 Android 中蓝牙的示例。当按照示例并尝试理解该过程时,我收到一个错误:“返回类型与 Thread.getState() 不兼容”。是什么导致了这个错误,我该如何解决?

以下是相关代码:

private int mState;
  /**
   * Set the current state of the chat connection
   * @param state  An integer defining the current connection state
   */
  private synchronized void setState(int state) {
      if (D) Log.d(TAG, "setState() " + mState + " -> " + state);
      mState = state;

      // Give the new state to the Handler so the UI Activity can update
      mHandler.obtainMessage(Rc_auto.MESSAGE_STATE_CHANGE, state, -1).sendToTarget();
  }

  /**
   * Return the current connection state. */
  public synchronized int getState() {
      return mState;
  }

  //How it's called
  public synchronized void start() {
      setState(STATE_LISTEN);
  }

【问题讨论】:

  • 在您当前的代码中,您应该返回一个synchronized int,但您只返回一个int。删除synchronized
  • 什么是同步整数...? synchronized 适用于方法,而不适用于返回类型。
  • 您是否有可能将extends Thread 添加到以前工作的类中?因为那时与Thread类中的method of the same name有冲突
  • 我删除了扩展线程并且错误消失了 thx

标签: android


【解决方案1】:

您可能应该将getState() 重命名为其他名称(例如getConnectionState),因为您的方法会覆盖Thread.getState(),我猜这不是您想要的。

【讨论】:

    【解决方案2】:

    答案已在我的问题下方的 cmets 中给出。

    我只需要在课程开始时删除“扩展线程”。

    你们所有人的帮助都很有帮助,我学到了一些额外的东西。

    谢谢大家

    【讨论】:

      猜你喜欢
      • 2014-08-29
      • 2016-05-28
      • 2019-09-06
      • 2013-11-11
      • 2016-02-14
      • 1970-01-01
      • 2020-08-13
      • 2021-04-15
      • 1970-01-01
      相关资源
      最近更新 更多