電話を介してコンピューターを実行しているサーバーに接続しようとしています。接続ボタンをクリックするたびに、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;
}
}
}