Androidでクライアントソケットを実装しています。しかし、実行時にクラッシュする実際には、Androidを使用してLinuxシステムを制御しようとしています。したがって、サーバーはLinuxです。ここに私のコードがあります
package com.example.clientsocket;
import java.io.OutputStreamWriter;
import java.net.Socket;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
@SuppressLint("HandlerLeak")
public class MainActivity extends Activity {
TextView serverMessage;
Thread m_objThreadClient;
Socket clientSocket;
private SensorManager sensorManager;
private long lastUpdate;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
serverMessage=(TextView)findViewById(R.id.textView1);
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
lastUpdate = System.currentTimeMillis();
Button b=(Button)findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
m_objThreadClient=new Thread(new Runnable() {
public void run()
{
try
{
EditText ipt=(EditText)findViewById(R.id.editText1);
String ip=ipt.getText().toString();
Socket sock = new Socket("192.168.42.130",20056);
OutputStreamWriter osw = new OutputStreamWriter(sock.getOutputStream());
char a='g';
osw.write(a);
osw.flush();
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
m_objThreadClient.start();
}
});
}
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
getAccelerometer(event);
}
}
private void getAccelerometer(SensorEvent event) {
float[] values = event.values;
// Movement
float y = values[1];
long actualTime = System.currentTimeMillis();
if (y>3 ) //
{
if (actualTime - lastUpdate < 200) {
return;
}
lastUpdate = actualTime;
m_objThreadClient=new Thread(new Runnable() {
public void run()
{
try
{
EditText ipt=(EditText)findViewById(R.id.editText1);
String ip=ipt.getText().toString();
Socket sock = new Socket("192.168.42.130",20056);
OutputStreamWriter osw = new OutputStreamWriter(sock.getOutputStream());
char a='r';
osw.write(a);
osw.flush();
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
m_objThreadClient.start();
}
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
@Override
protected void onResume() {
super.onResume();
// register this class as a listener for the orientation and
// accelerometer sensors
sensorManager.registerListener((SensorEventListener) this,
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onPause() {
// unregister listener
super.onPause();
sensorManager.unregisterListener((SensorListener) this);
}
Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
messageDisplay(msg.obj.toString());
}
};
public void messageDisplay(String servermessage)
{
serverMessage.setText(""+servermessage);
}
}
ログキャットは
09-09 22:36:46.000: D/SensorManager(3492): ====>>>>>Num Sensor: 1
09-09 22:36:46.000: D/SensorManager(3492): ====>>>>>Num Sensor: 2
09-09 22:36:46.000: D/SensorManager(3492): ====>>>>>Num Sensor: 3
09-09 22:36:46.000: D/SensorManager(3492): ====>>>>>Num Sensor: 4
09-09 22:36:46.000: D/SensorManager(3492): ====>>>>>Num Sensor: 5
09-09 22:36:46.000: D/SensorManager(3492): ====>>>>>Num Sensor: 6
09-09 22:36:46.000: D/SensorManager(3492): ====>>>>>Num Sensor: 0
09-09 22:36:46.000: D/AndroidRuntime(3492): Shutting down VM
09-09 22:36:46.000: W/dalvikvm(3492): threadid=1: thread exiting with uncaught exception (group=0x40018578)
09-09 22:36:46.007: E/AndroidRuntime(3492): FATAL EXCEPTION: main
09-09 22:36:46.007: E/AndroidRuntime(3492): java.lang.RuntimeException: Unable to resume activity {com.example.clientsocket/com.example.clientsocket.MainActivity}: java.lang.ClassCastException: com.example.clientsocket.MainActivity
09-09 22:36:46.007: E/AndroidRuntime(3492): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2124)
09-09 22:36:46.007: E/AndroidRuntime(3492): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2139)
09-09 22:36:46.007: E/AndroidRuntime(3492): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1672)
09-09 22:36:46.007: E/AndroidRuntime(3492): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-09 22:36:46.007: E/AndroidRuntime(3492): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
09-09 22:36:46.007: E/AndroidRuntime(3492): at android.os.Handler.dispatchMessage(Handler.java:99)
09-09 22:36:46.007: E/AndroidRuntime(3492): at android.os.Looper.loop(Looper.java:130)
09-09 22:36:46.007: E/AndroidRuntime(3492): at android.app.ActivityThread.main(ActivityThread.java:3687)
09-09 22:36:46.007: E/AndroidRuntime(3492): at java.lang.reflect.Method.invokeNative(Native Method)
09-09 22:36:46.007: E/AndroidRuntime(3492): at java.lang.reflect.Method.invoke(Method.java:507)
09-09 22:36:46.007: E/AndroidRuntime(3492): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
09-09 22:36:46.007: E/AndroidRuntime(3492): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
09-09 22:36:46.007: E/AndroidRuntime(3492): at dalvik.system.NativeStart.main(Native Method)
09-09 22:36:46.007: E/AndroidRuntime(3492): Caused by: java.lang.ClassCastException: com.example.clientsocket.MainActivity
09-09 22:36:46.007: E/AndroidRuntime(3492): at com.example.clientsocket.MainActivity.onResume(MainActivity.java:139)
09-09 22:36:46.007: E/AndroidRuntime(3492): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1150)
09-09 22:36:46.007: E/AndroidRuntime(3492): at android.app.Activity.performResume(Activity.java:3832)
09-09 22:36:46.007: E/AndroidRuntime(3492): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2114)
09-09 22:36:46.007: E/AndroidRuntime(3492): ... 12 more