我刚开始 Android 编程,我想用 SensorSimulator 尝试一些东西。一旦到达要连接的位置,我就会收到以下错误:
11-07 08:54:45.195: E/AndroidRuntime(1912): FATAL EXCEPTION: main
11-07 08:54:45.195: E/AndroidRuntime(1912): java.lang.RuntimeException: Unable to start activity ComponentInfo{fnt.android1/fnt.android1.ValuesActivity}: android.os.NetworkOnMainThreadException
11-07 08:54:45.195: E/AndroidRuntime(1912): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
11-07 08:54:45.195: E/AndroidRuntime(1912): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
11-07 08:54:45.195: E/AndroidRuntime(1912): at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-07 08:54:45.195: E/AndroidRuntime(1912): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
11-07 08:54:45.195: E/AndroidRuntime(1912): at android.os.Handler.dispatchMessage(Handler.java:99)
11-07 08:54:45.195: E/AndroidRuntime(1912): at android.os.Looper.loop(Looper.java:137)
11-07 08:54:45.195: E/AndroidRuntime(1912): at android.app.ActivityThread.main(ActivityThread.java:5103)
11-07 08:54:45.195: E/AndroidRuntime(1912): at java.lang.reflect.Method.invokeNative(Native Method)
11-07 08:54:45.195: E/AndroidRuntime(1912): at java.lang.reflect.Method.invoke(Method.java:525)
11-07 08:54:45.195: E/AndroidRuntime(1912): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-07 08:54:45.195: E/AndroidRuntime(1912): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-07 08:54:45.195: E/AndroidRuntime(1912): at dalvik.system.NativeStart.main(Native Method)
11-07 08:54:45.195: E/AndroidRuntime(1912): Caused by: android.os.NetworkOnMainThreadException
11-07 08:54:45.195: E/AndroidRuntime(1912): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1133)
11-07 08:54:45.195: E/AndroidRuntime(1912): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
11-07 08:54:45.195: E/AndroidRuntime(1912): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
11-07 08:54:45.195: E/AndroidRuntime(1912): at libcore.io.IoBridge.connect(IoBridge.java:112)
11-07 08:54:45.195: E/AndroidRuntime(1912): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
11-07 08:54:45.195: E/AndroidRuntime(1912): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
11-07 08:54:45.195: E/AndroidRuntime(1912): at java.net.Socket.startupSocket(Socket.java:566)
11-07 08:54:45.195: E/AndroidRuntime(1912): at java.net.Socket.tryAllAddresses(Socket.java:127)
11-07 08:54:45.195: E/AndroidRuntime(1912): at java.net.Socket.<init>(Socket.java:177)
11-07 08:54:45.195: E/AndroidRuntime(1912): at java.net.Socket.<init>(Socket.java:149)
11-07 08:54:45.195: E/AndroidRuntime(1912): at org.openintents.sensorsimulator.hardware.SensorSimulatorClient.connect(SensorSimulatorClient.java:116)
11-07 08:54:45.195: E/AndroidRuntime(1912): at org.openintents.sensorsimulator.hardware.SensorManagerSimulator.connectSimulator(SensorManagerSimulator.java:220)
11-07 08:54:45.195: E/AndroidRuntime(1912): at fnt.android1.ValuesActivity.onCreate(ValuesActivity.java:26)
11-07 08:54:45.195: E/AndroidRuntime(1912): at android.app.Activity.performCreate(Activity.java:5133)
11-07 08:54:45.195: E/AndroidRuntime(1912): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-07 08:54:45.195: E/AndroidRuntime(1912): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
11-07 08:54:45.195: E/AndroidRuntime(1912): ... 11 more
我确定它连接到正确的 IP 并使用正确的端口。这是我的完整代码:
package fnt.android1;
import android.os.Bundle;
import android.app.Activity;
import android.hardware.SensorManager;
import android.view.Menu;
import org.openintents.sensorsimulator.hardware.Sensor;
import org.openintents.sensorsimulator.hardware.SensorEvent;
import org.openintents.sensorsimulator.hardware.SensorEventListener;
import org.openintents.sensorsimulator.hardware.SensorManagerSimulator;
public class ValuesActivity extends Activity implements SensorEventListener {
private SensorManagerSimulator mSensorManager;
private Sensor mLight;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_values);
mSensorManager = SensorManagerSimulator.getSystemService(this, SENSOR_SERVICE);
mSensorManager.connectSimulator();
mLight = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.values, menu);
return true;
}
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, mLight, SensorManager.SENSOR_DELAY_FASTEST);
System.out.println("Event registered!");
}
protected void onPause() {
super.onPause();
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
for(int i = 0; i < event.values.length; i++) {
System.out.println(event.values[i]);
}
}
}