-1
public String getMacInfo(){    
    String mac = "";    
       WifiManager wifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);    
       WifiInfo wifiInfo = wifiManager.getConnectionInfo();    
       if(wifiInfo.getMacAddress()!=null){    
        mac = wifiInfo.getMacAddress();    
    } else {    
        mac = "Fail";    
    }    

       return mac;    
}   

I add these code in my test case ,there is a error

showing in "mContext" the line 3.

Who can help me???

Thank you !

Lavekush Agrawal
  • 6,040
  • 7
  • 52
  • 85

1 Answers1

1

The "mContext cannot be resolved" error means that variable "mContext" is not in scope (or is not declared anywhere).

The fix is simple, you just need to declare the object as a member variable, and initialize it in your setUp() method.

public class MyTest extends InstrumentationTestCase {
    private Context mContext;

    public void setUp() {
        mContext = getInstrumentation().getContext();
    }

    // ...
}

See Java, "Variable name" cannot be resolved to a variable for a similar question regarding this particular error.

Community
  • 1
  • 1
Allen Hair
  • 1,021
  • 2
  • 10
  • 21
  • thanks !i am green hand ,add the code you given can't run correctly ,why?the error is
    The method getInstrumentation() is undefined for the type baidumap,the "baidumap" is my test case extends UiAutomatorTestCase! do you know how to modify  ! Thank you!
    – Andy GeGe Jul 31 '15 at 03:16
  • Are you using a recent version of UiAutomator (v2.0 or higher)? – Allen Hair Jul 31 '15 at 04:41
  • i think not,the uiautomator.jar file is in android-19 sdk which my colleague shared. I must use the recent version? – Andy GeGe Aug 01 '15 at 05:57
  • Shell-based UiAutomator (the old version) cannot access the Context object. This is possible with UiAutomator v2.0 or later. – Allen Hair Aug 01 '15 at 15:54