title: Singleton class for socket connection not working
body: <p>I created a singleton class to enable socket connection, which returns a socket object, which can be used by any number of activities in the application.
  the singleton class is as follows</p>
  
  <pre><code>public class Singleton 
  {
  private static Socket socket;
  private DataInputStream input;
  private DataOutputStream output;
  private boolean logged;
  private static Singleton instance;
  private String information;
  private static final int SERVERPORT = 8020;
  private static final String SERVER_IP = "192.168.1.33";
  static PrintWriter out;
  
  private Singleton()
  {
  }
  
  public static Singleton getInstance()
  {
          return instance;
  }
  
  public static void initSingleton()
  {
      if(instance == null)
      {
          instance = new Singleton();
          InetAddress serverAddr = null;
          try {
              serverAddr = InetAddress.getByName(SERVER_IP);
          } catch (UnknownHostException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
  
          try {
              socket = new Socket(serverAddr, SERVERPORT);
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }  
          try {
              out = new PrintWriter(new BufferedWriter(
                      new OutputStreamWriter(socket.getOutputStream())),
                      true);
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      }
  }
  
  
  
  public Socket getSocket()
  {
      return socket;
  }
  
  
  public PrintWriter getOutput()
  {
      return out;
  }
  
  }
  </code></pre>
  
  <p>In the MainActivity, I call the singleton class.</p>
  
  <pre><code>public class MainActivity extends Activity implements OnItemSelectedListener {
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);     
      initSingleton(); }
  protected void initSingleton()
  {
      Singleton.initSingleton();
  }
  public void onClickon(View view) {
      Socket socket = Singleton.getInstance().getSocket();
  
      try{
  
          PrintWriter out=Singleton.getInstance().getOutput();    
      out.println("0"); 
      }
      catch (Exception e) {
          e.printStackTrace();
      }
  
  }
    }
  </code></pre>
  
  <p>on button click , the onClickon function is called which uses the the getSocket() and getOutput() functions of the Singleton class to return the object types.</p>
  
  <p>But the application crashes.</p>
  
  <p><strong>The log is as follows.</strong></p>
  
  <pre><code>05-08 11:33:41.236: E/AndroidRuntime(19813): FATAL EXCEPTION: main
  05-08 11:33:41.236: E/AndroidRuntime(19813): Process: com.example.clientmobile2, PID: 19813
  05-08 11:33:41.236: E/AndroidRuntime(19813): java.lang.RuntimeException: Unable to start activity     ComponentInfo{com.example.clientmobile2/com.example.clientmobile2.MainActivity}: android.os.NetworkOnMainThreadException
  05-08 11:33:41.236: E/AndroidRuntime(19813):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
  05-08 11:33:41.236: E/AndroidRuntime(19813):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)
  05-08 11:33:41.236: E/AndroidRuntime(19813):    at android.app.ActivityThread.access$800(ActivityThread.java:139)
  05-08 11:33:41.236: E/AndroidRuntime(19813):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
  05-08 11:33:41.236: E/AndroidRuntime(19813):    at android.os.Handler.dispatchMessage(Handler.java:102)
  05-08 11:33:41.236: E/AndroidRuntime(19813):    at android.os.Looper.loop(Looper.java:136)
  05-08 11:33:41.236: E/AndroidRuntime(19813):    at android.app.ActivityThread.main(ActivityThread.java:5102)
  05-08 11:33:41.236: E/AndroidRuntime(19813):    at java.lang.reflect.Method.invokeNative(Native Method)
  05-08 11:33:41.236: E/AndroidRuntime(19813):    at java.lang.reflect.Method.invoke(Method.java:515)
  05-08 11:33:41.236: E/AndroidRuntime(19813):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
  05-08 11:33:41.236: E/AndroidRuntime(19813):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
  05-08 11:33:41.236: E/AndroidRuntime(19813):    at dalvik.system.NativeStart.main(Native Method)
  05-08 11:33:41.236: E/AndroidRuntime(19813): Caused by: android.os.NetworkOnMainThreadException
  05-08 11:33:41.236: E/AndroidRuntime(19813):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
  05-08 11:33:41.236: E/AndroidRuntime(19813):    at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
  05-08 11:33:41.236: E/AndroidRuntime(19813):    at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
  05-08 11:33:41.236: E/AndroidRuntime(19813):    at libcore.io.IoBridge.connect(IoBridge.java:112)
  05-08 11:33:41.236: E/AndroidRuntime(19813):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
  05-08 11:33:41.236: E/AndroidRuntime(19813):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
  05-08 11:33:41.236: E/AndroidRuntime(19813):    at java.net.Socket.startupSocket(Socket.java:566)
  05-08 11:33:41.236: E/AndroidRuntime(19813):    at java.net.Socket.&lt;init&gt;(Socket.java:226)
  05-08 11:33:41.236: E/AndroidRuntime(19813):    at com.example.clientmobile2.Singleton.initSingleton(Singleton.java:48)
  05-08 11:33:41.236: E/AndroidRuntime(19813):    at com.example.clientmobile2.MainActivity.initSingleton(MainActivity.java:148)
  05-08 11:33:41.236: E/AndroidRuntime(19813):    at com.example.clientmobile2.MainActivity.onCreate(MainActivity.java:101)
  05-08 11:33:41.236: E/AndroidRuntime(19813):    at android.app.Activity.performCreate(Activity.java:5248)
  05-08 11:33:41.236: E/AndroidRuntime(19813):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
  05-08 11:33:41.236: E/AndroidRuntime(19813):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2173)
  </code></pre>