And so, I do a VPN client and get an incomprehensible error ...

A reference to an enclosing class is requiered

public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // Запрос прав на подключение VPN... Intent intent = VpnService.prepare(MainActivity.this); startActivityForResult(intent, BIND_AUTO_CREATE); } // Права полученны... @Override protected void onActivityResult(int requestCode, int resultCode) { if (requestCode == BIND_AUTO_CREATE && resultCode == RESULT_OK) { VpnService.Builder bb = new VpnService.Builder(); } } } 

new VpnService.Builder () This piece of code is underlined in red..not compile!

Maybe someone knows what needs to be done? What is missing? And what am I doing wrong.

  • in which place (line) does an error occur? add code VpnService - Mikhail Vaysman
  • new VpnService.Builder (); This piece of code is underlined in red..not compile! - Vadim

1 answer 1

Apparently, the Builder class is the inner class of the VpnService class. An instance of an inner class can only be created in the context of an outer class.

There are two solutions: either declare the Builder class as a static nested class (add the static modifier) ​​- such classes can exist separately from the external class, or create an instance of the Builder class in the context of the VpnService class (having its instance).

UPD :

 public class VpnService { public static class Builder { } } 

Now somewhere outside you can create an instance builder:

 VpnService.Builder builder = new VpnService.Builder(); 
  • And how to declare a class Builder static nested? - Vadim
  • Give an example with my code .. - Vadim
  • @ Vadim, Replace class Builder with static class Builder . - post_zeew
  • Sveravno doesn’t work .. - Vadim
  • Vseravno does not work, discard a ready-made solution if it is not difficult ... It does not work at all - Vadim