2012-08-22 43 views
1

我試圖連接到服務器我通過手機運行我的計算機。每次我點擊連接按鈕,它都會給我java.lang.IllegalStateException。當連接按鈕被點擊時,它會創建一個應該連接到我的計算機上的服務器的套接字。 這裏的錯誤Android致命錯誤:創建套接字時按鈕上的java.lang.IllegalStateException異常

08-21 23:45:26.427: E/AndroidRuntime(4986): FATAL EXCEPTION: main 
08-21 23:45:26.427: E/AndroidRuntime(4986): java.lang.IllegalStateException: Could not execute method of the activity 
08-21 23:45:26.427: E/AndroidRuntime(4986):  at android.view.View$1.onClick(View.java:3044) 
08-21 23:45:26.427: E/AndroidRuntime(4986):  at android.view.View.performClick(View.java:3511) 
08-21 23:45:26.427: E/AndroidRuntime(4986):  at android.view.View$PerformClick.run(View.java:14105) 
08-21 23:45:26.427: E/AndroidRuntime(4986):  at android.os.Handler.handleCallback(Handler.java:605) 
08-21 23:45:26.427: E/AndroidRuntime(4986):  at android.os.Handler.dispatchMessage(Handler.java:92) 
08-21 23:45:26.427: E/AndroidRuntime(4986):  at android.os.Looper.loop(Looper.java:137) 
08-21 23:45:26.427: E/AndroidRuntime(4986):  at android.app.ActivityThread.main(ActivityThread.java:4424) 
08-21 23:45:26.427: E/AndroidRuntime(4986):  at java.lang.reflect.Method.invokeNative(Native Method) 
08-21 23:45:26.427: E/AndroidRuntime(4986):  at java.lang.reflect.Method.invoke(Method.java:511) 
08-21 23:45:26.427: E/AndroidRuntime(4986):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787) 
08-21 23:45:26.427: E/AndroidRuntime(4986):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554) 
08-21 23:45:26.427: E/AndroidRuntime(4986):  at dalvik.system.NativeStart.main(Native Method) 
08-21 23:45:26.427: E/AndroidRuntime(4986): Caused by: java.lang.reflect.InvocationTargetException 
08-21 23:45:26.427: E/AndroidRuntime(4986):  at java.lang.reflect.Method.invokeNative(Native Method) 
08-21 23:45:26.427: E/AndroidRuntime(4986):  at java.lang.reflect.Method.invoke(Method.java:511) 
08-21 23:45:26.427: E/AndroidRuntime(4986):  at android.view.View$1.onClick(View.java:3039) 
08-21 23:45:26.427: E/AndroidRuntime(4986):  ... 11 more 
08-21 23:45:26.427: E/AndroidRuntime(4986): Caused by: android.os.NetworkOnMainThreadException 
08-21 23:45:26.427: E/AndroidRuntime(4986):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099) 
08-21 23:45:26.427: E/AndroidRuntime(4986):  at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84) 
08-21 23:45:26.427: E/AndroidRuntime(4986):  at libcore.io.IoBridge.connectErrno(IoBridge.java:127) 
08-21 23:45:26.427: E/AndroidRuntime(4986):  at libcore.io.IoBridge.connect(IoBridge.java:112) 
08-21 23:45:26.427: E/AndroidRuntime(4986):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192) 
08-21 23:45:26.427: E/AndroidRuntime(4986):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) 
08-21 23:45:26.427: E/AndroidRuntime(4986):  at java.net.Socket.startupSocket(Socket.java:566) 
08-21 23:45:26.427: E/AndroidRuntime(4986):  at java.net.Socket.<init>(Socket.java:225) 
08-21 23:45:26.427: E/AndroidRuntime(4986):  at com.example.socketconnection.Singleton.connect(Singleton.java:44) 
08-21 23:45:26.427: E/AndroidRuntime(4986):  at com.example.socketconnection.MainActivity.connect(MainActivity.java:47) 
08-21 23:45:26.427: E/AndroidRuntime(4986):  ... 14 more 

下面是MainActivity.java

代碼
package com.example.socketconnection; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.View; 
import android.widget.EditText; 
import android.widget.TextView; 

public class MainActivity extends Activity 
{ 
    private TextView errorMessageField; 
    public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE"; 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     errorMessageField = (TextView)findViewById(R.id.errorMessageText); 
     errorMessageField.setKeyListener(null); 
     initSingleton(); 
    } 

    protected void initSingleton() 
    { 
     Singleton.initSingleton(); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) 
    { 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 
    } 

    public void connect(View view) 
    { 
     EditText userNameField = (EditText)findViewById(R.id.userNameText); 
     EditText passwordField = (EditText)findViewById(R.id.passwordText); 
     String userName = userNameField.getText().toString(); 
     String password = passwordField.getText().toString(); 
     userNameField.setText(""); 
     passwordField.setText(""); 
     errorMessageField.setText(""); 
     String member = Singleton.getInstance().connect(userName, password); 
     if(member.equalsIgnoreCase("true")) 
     { 
      errorMessageField.setText(""); 
      Intent intent = new Intent(this, UserList.class); 
      intent.putExtra(EXTRA_MESSAGE, userName); 
      startActivity(intent); 
     } 
     else if(member.equalsIgnoreCase("false")) 
     { 
      errorMessageField.setText("Invalid User Name/Password"); 
     } 
     else 
     { 
      errorMessageField.setText("Invalid User Name/Password"); 
     } 
    } 

} 

代碼Singleton.java

package com.example.socketconnection; 

import java.io.DataInputStream; 
import java.io.DataOutputStream; 
import java.io.IOException; 
import java.net.InetAddress; 
import java.net.Socket; 

public class Singleton 
{ 
    private Socket socket; 
    private DataInputStream input; 
    private DataOutputStream output; 
    private boolean logged; 
    private static Singleton instance; 
    private String information; 
    private String userName; 
    private String password; 

    private Singleton() 
    { 
    } 

    public static Singleton getInstance() 
    { 
      return instance; 
    } 

    public static void initSingleton() 
    { 
     if(instance == null) 
     { 
      instance = new Singleton(); 
     } 
    } 

    public String connect(String userName, String password) 
    { 
     this.userName = userName; 
     this.password = password; 

     try 
     { 
      this.socket = new Socket(InetAddress.getByName("192.168.1.79"), 9000); 
      this.input = new DataInputStream(socket.getInputStream()); 
      this.output = new DataOutputStream(socket.getOutputStream()); 
      output.writeUTF("login"); 
      output.flush(); 
      output.writeUTF(userName); 
      output.flush(); 
      output.writeUTF(password); 
      output.flush(); 
      this.information = input.readUTF(); 
     } 
     catch(IOException ex) 
     { 
      this.information = "Cannot connect to Server"; 
     } 

     return information; 
    } 

    public void setLogged(boolean logged) 
    { 
     this.logged = logged; 
    } 

    public Socket getSocket() 
    { 
     return socket; 
    } 

    public DataInputStream getInput() 
    { 
     return input; 
    } 

    public DataOutputStream getOutput() 
    { 
     return output; 
    } 

    public boolean getLogged() 
    { 
     return logged; 
    } 
} 

代碼UserList.java

package com.example.socketconnection; 

import java.io.DataInputStream; 
import java.io.DataOutputStream; 
import java.io.IOException; 
import java.net.Socket; 
import java.util.List; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.Menu; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 

public class UserList extends Activity 
{ 

    private Socket socket; 
    private DataInputStream input; 
    private DataOutputStream output; 
    private String userName; 
    private String [] users; 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_user_list); 

     Intent intent = getIntent(); 
     userName = intent.getStringExtra(MainActivity.EXTRA_MESSAGE); 



     ListView userList = (ListView)findViewById(R.id.userList); 
     users = new String[0]; 
     ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, users); 
     userList.setAdapter(listAdapter); 
     socket = Singleton.getInstance().getSocket(); 
     input = Singleton.getInstance().getInput(); 
     output = Singleton.getInstance().getOutput(); 
     Runnable task = new UpdateTask(listAdapter); 
     Thread thread = new Thread(task); 
     thread.start(); 
    } 

    class UpdateTask implements Runnable 
    { 
     private ArrayAdapter<String> adapter; 
     private boolean logged = true; 

     public UpdateTask(ArrayAdapter<String> adapter) 
     { 
      this.adapter = adapter; 
     } 

     public void loggedOut(boolean logged) 
     { 
      this.logged = logged; 
     } 

     public void run() 
     { 
      try 
      { 
       while(logged) 
       { 
        if(input.available() > 0) 
        { 
         String message = input.readUTF(); 
         if(!message.equalsIgnoreCase("chat")) 
         { 
          users = getList(message); 
          adapter.notifyDataSetChanged(); 

         } 
        } 
        Thread.sleep(100); 
       } 
      }  
      catch(IOException ex) 
      { 
      } 
      catch(InterruptedException ex) 
      { 
      } 
     } 
     public String[] getList(String message) 
     { 
      String line = message; 
      String[] list; 
      String[] split; 
      int counter = 0; 

      String temp = line.substring(1, line.length() - 1); 
      split = temp.split(", "); 
      if(split.length == 1) 
      { 
       list = new String[0]; 
      } 
      else 
      { 
       list = new String[split.length - 1]; 
       for(int i = 0; i < split.length; i++) 
       { 
        if(!split[i].equalsIgnoreCase(userName)) 
        { 
         list[counter] = split[i]; 
         counter++; 
        } 
       } 
     } 

     return list; 
    } 
} 

}

+0

它是由:android.os.NetworkOnMainThreadException引起的。您正在主線程中進行網絡操作。創建AsyncTask並在後臺方法中執行此操作。 –

回答

0

您不能僅僅阻止這樣的主應用程序線程,請調用您的Singleton.connect

相反,採取AsyncTask好處,你可以從doInBackground :-)這依賴於你的結果代碼的上下文中安全地調用connect然後可以在主應用程序線程的背景下onPostExecute進行安全。

相關問題