Exception when using application object?
<p>I'm trying to create a socket as an application object. Here's the activity's code:</p>

<pre><code>@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    context = getApplicationContext();
    setContentView(R.layout.activity_connect);
    app = (MyApplication) getApplication();
    connectButton = (ImageButton) findViewById(R.id.connect_button);
    connectButton.setOnClickListener(connectListener);
    //app.stablishConnection();

}

private OnClickListener connectListener = new OnClickListener() {

    @Override
    public void onClick(View v) {

         app.stablishConnection();  
         Intent intent = new Intent(context, ShowDetails.class);
         startActivity(intent);
    }
};
</code></pre>

<p>And here is the Application class' code</p>

<pre><code>private Socket socket;
Context context;

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
}

@Override
public void onCreate() {
    super.onCreate();
    context = getApplicationContext();
}

@Override
public void onLowMemory() {
    super.onLowMemory();
}

@Override
public void onTerminate() {
    super.onTerminate();
}

public void stablishConnection(){
    try {   InetAddress serverAddr;
        serverAddr = InetAddress.getByName("10.10.10.10");
        socket = new Socket(serverAddr, 5000);
    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}
</code></pre>

<p>I want the socket not to be destroyed until the application is closed and I want to create the socket once the user click the button.</p>

<p>The system shows me a androidblockguardpolicy exception when the activity calls <code>app.stablishConnection()</code>. I've also tried to call this method in onCreate (you can see it commented) and in that case it shows and Unable to start componentInfo exception when the app tries to create the socket <code>socket = new Socket(serverAddr, 5000);</code> in the application class.</p>

<p>How can I fix this?</p>
