Search code examples
javaandroidandroid-contextdeclare

Trying to set context and it's not an enclosing class. Ideas? :)


Problem I'm having:

  • 4th line down I get an error saying "com.example.wifilocator.MainActivity" is not an enclosing class

Code in question(From MainActivity) - 4th line:

class wifi {
    int signalStrength = 0;
    int loopToggle = 0;
    Context context = MainActivity.this;

    @RequiresApi(api = Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    public void loop() throws InterruptedException {
        while (loopToggle == 0) {
            WifiManager signalStrength = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            String wifiInfo = WifiManager.EXTRA_WIFI_INFO;
            TextView textView = (TextView) textView.findViewById(R.id.readOut);
            Thread.sleep(1000);
        }
    }
}

Thanks so much for any help! :)


Solution

  • The error is because you're placing the wifi outside the MainActivity class like this:

    public class MainActivity extends AppCompatActivity {
      ...
    }
    
    class wifi {
      ...
    }
    

    wifi class must be inside the MainActivity class, like this:

    public class MainActivity extends AppCompatActivity {
      ...
    
      class wifi {
         ...
      }
    }