gpt4 book ai didi

android - 传感器模拟器的 NetworkOnMainThreadException

转载 作者:行者123 更新时间:2023-11-29 01:47:28 32 4
gpt4 key购买 nike

我按照此说明使用传感器模拟器在模拟器上调试我的应用程序: http://code.google.com/p/openintents/wiki/SensorSimulator#How_to_use_the_in_your_application

我完成了上述所有步骤,但就我而言,一切并不像说明中描述的那么简单。我在尝试连接、注册监听器等时遇到了 NetworkOnMainThreadException。所以我创建了异步任务来解决这个问题。现在,我有以下内容:

连接到模拟器:

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
sensorManager = SensorManagerSimulator.getSystemService(this, SENSOR_SERVICE);
this.new ConnectToSimulator().execute();
}

onResume() 监听器的注册:

@Override
public void onResume() {
super.onResume();
this.new RegisterToSimulator().execute();
}

最后是我的任务:

class ConnectToSimulator extends AsyncTask<Object, Object, Object> {   
@Override
protected Object doInBackground(Object... arg0) {
try{
sensorManager.connectSimulator();
}catch(Exception e)
{
Log.i("error", e.getMessage());
}
return null;
}
}

class RegisterToSimulator extends AsyncTask<Object, Object, Object> {
@Override
protected Object doInBackground(Object... arg0) {
try{
sensorManager.registerListener(Compass.this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_FASTEST);
sensorManager.registerListener(Compass.this, sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD), SensorManager.SENSOR_DELAY_FASTEST);
sensorManager.registerListener(Compass.this, sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_FASTEST);
}catch(Exception e)
{
Log.i("error", e.getMessage());
}
return null;
}

}

但即使在那之后,我仍然收到尝试在模拟器上启动应用程序的错误:

12-15 14:23:28.877: E/AndroidRuntime(3724): FATAL EXCEPTION: main 12-15 14:23:28.877: E/AndroidRuntime(3724): android.os.NetworkOnMainThreadException 12-15 14:23:28.877: E/AndroidRuntime(3724): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117) 12-15 14:23:28.877: E/AndroidRuntime(3724): at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:163) 12-15 14:23:28.877: E/AndroidRuntime(3724): at libcore.io.IoBridge.recvfrom(IoBridge.java:513) 12-15 14:23:28.877: E/AndroidRuntime(3724): at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488) 12-15 14:23:28.877: E/AndroidRuntime(3724): at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46) 12-15 14:23:28.877: E/AndroidRuntime(3724): at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240) 12-15 14:23:28.877: E/AndroidRuntime(3724): at java.io.InputStreamReader.read(InputStreamReader.java:244) 12-15 14:23:28.877: E/AndroidRuntime(3724): at java.io.BufferedReader.fillBuf(BufferedReader.java:130) 12-15 14:23:28.877: E/AndroidRuntime(3724): at java.io.BufferedReader.readLine(BufferedReader.java:354) 12-15 14:23:28.877: E/AndroidRuntime(3724): at org.openintents.sensorsimulator.hardware.SensorSimulatorClient.readSensor(SensorSimulatorClient.java:654) 12-15 14:23:28.877: E/AndroidRuntime(3724): at org.openintents.sensorsimulator.hardware.SensorSimulatorClient.readSensor(SensorSimulatorClient.java:571) 12-15 14:23:28.877: E/AndroidRuntime(3724): at org.openintents.sensorsimulator.hardware.SensorSimulatorClient.access$1000(SensorSimulatorClient.java:53) 12-15 14:23:28.877: E/AndroidRuntime(3724): at org.openintents.sensorsimulator.hardware.SensorSimulatorClient$1.handleMessage(SensorSimulatorClient.java:505) 12-15 14:23:28.877: E/AndroidRuntime(3724): at android.os.Handler.dispatchMessage(Handler.java:99) 12-15 14:23:28.877: E/AndroidRuntime(3724): at android.os.Looper.loop(Looper.java:137) 12-15 14:23:28.877: E/AndroidRuntime(3724): at android.app.ActivityThread.main(ActivityThread.java:5039) 12-15 14:23:28.877: E/AndroidRuntime(3724): at java.lang.reflect.Method.invokeNative(Native Method) 12-15 14:23:28.877: E/AndroidRuntime(3724): at java.lang.reflect.Method.invoke(Method.java:511) 12-15 14:23:28.877: E/AndroidRuntime(3724): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 12-15 14:23:28.877: E/AndroidRuntime(3724): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 12-15 14:23:28.877: E/AndroidRuntime(3724): at dalvik.system.NativeStart.main(Native Method)

有人可以澄清为什么我有这么奇怪的错误吗?此外,模拟器指令中没有关于异步任务使用必要性的信息,也许我做错了什么。请帮忙,提前致谢。

最佳答案

不幸的是,这个错误无法绕过,因为库没有实现"new"SDK 限制。

最简单的解决方案是将 list 中的 minSdkVersion 设置为 Honeycomb 之前的内容(例如 android:minSdkVersion="8")。

对此的原因是,尽管您确实在后台线程上进行了连接,但您仍然创建了UI 线程上的对象,这反过来意味着 SensorSimulatorClient 将使用套接字在您的 UI 线程上进行通信。

关于android - 传感器模拟器的 NetworkOnMainThreadException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20596091/

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