【问题标题】:Android Eclipse context cannot be resolved in return typeAndroid Eclipse 上下文无法在返回类型中解析
【发布时间】:2014-09-22 15:10:46
【问题描述】:

我理解有问题上下文我写了一个程序来控制我的灯,但是我需要检查他是否没有连接错误的WiFi然后使用错误的IP。我尝试了多种选择,但没有一个好的解决方案。我看一些关于上下文白化成功的早期问题。如果你需要完整的程序,我可以发给你,但我认为这已经足够了:)

确切的问题是 getCurrentSsid(context) 错误:上下文无法在返回类型中解析

//CLASS SENDCOMMAND

public class SendCommand extends AsyncTask<String, Integer, JSONArray> {


static boolean WifiSSID;


public SendCommand(boolean wifi) {
    this.wifi = wifi;
}

//FUNCTION TO CHECK THE CURREN SSID
public boolean  getCurrentSsid(Context context) {
     ssid = null;
      ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
      NetworkInfo networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
      if (networkInfo.isConnected()) {
        final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        final WifiInfo connectionInfo = wifiManager.getConnectionInfo();
        if (connectionInfo != null && !TextUtils.isEmpty(connectionInfo.getSSID())) {
          ssid = connectionInfo.getSSID();
        }
      }
       if (ssid =="Room#2")
          return true;
      else
          return false; 

    }
//wifiSSID = boolean getCurrentSsid(context);

//send the command
@Override
protected JSONArray doInBackground(String... params) {
    // TODO Auto-generated method stub
    Log.d(tag, "Beginnen do in background");
    Log.d(tag, "Wifi is " + wifi);


    HttpClient client = new DefaultHttpClient();
    HttpGet get;


    if (wifi && **getCurrentSsid(context)**){ //HERE HE GIVES A ERROR getCurrentSsid(context)

【问题讨论】:

  • 对不起,我编辑了它。
  • 上下文变量在哪里设置? O_o

标签: java android eclipse class


【解决方案1】:

doInBackground 范围内没有名称为 context 的符号。

Context 传递给异步任务的常用方法是将其存储在成员变量中,然后在构造函数arg 中传递:

private Context mContext;

public SendCommand(Context context) {
    mContext = context;
}

然后在需要Context 的地方使用mContext

【讨论】:

  • 我以前试过这个! Eclipse 不会出错,但是当我按下按钮打开灯时,我的应用程序会不断停止......
  • 09-22 17:51:45.628: W/dalvikvm(15218): threadid=11: 线程以未捕获的异常退出 (group=0x40ea76d8)
  • 我没有看到错误?
  • 使用堆栈跟踪开始调试崩溃。 stackoverflow.com/questions/23353173/… 。也看看stackoverflow.com/questions/513832/…
猜你喜欢
  • 2017-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-01
  • 1970-01-01
  • 2012-05-27
  • 2019-08-06
相关资源
最近更新 更多