gpt4 book ai didi

java - SensorSimulator 在连接时抛出错误

转载 作者:行者123 更新时间:2023-12-01 04:11:53 31 4
gpt4 key购买 nike

我刚刚开始 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]);
}
}

}

最佳答案

logcat 表示您正在尝试在主 UI Thread 上运行网络调用。您应该将长时间运行的操作移至单独的Thead。您可以使用 AsyncTask 或常规 Thread 来执行此操作。

关于java - SensorSimulator 在连接时抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19839783/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com