我正在尝试制作一个相当简单的测试应用程序,在唯一的活动屏幕上打印加速度计的值。但是当我运行它时,它会在main中出现致命错误:NetworkOnMainThreadException。
我试图跟踪它可能的原因并遵循this question来解决将所有模拟器调用放在各自的AsyncTask类中的问题。但是,我仍然得到错误。
我的MainActivity.java:
导入声明:
package com.example.chillii;
import org.openintents.sensorsimulator.hardware.Sensor;
import org.openintents.sensorsimulator.hardware.SensorEvent;
import org.openintents.sensorsimulator.hardware.SensorEventListener;
import org.openintents.sensorsimulator.hardware.SensorManagerSimulator;
import android.hardware.SensorManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
应该删除上述错误的3个AsyncTask类:
class ConnectToSimulator extends AsyncTask<SensorManagerSimulator, Void, Void>
{
@Override
protected Void doInBackground(SensorManagerSimulator ... arg0) {
try{
arg0[0].connectSimulator();
}
catch(Exception E)
{
Log.i("ConnectTaskError: ", E.getMessage());
}
return null;
}
}
class RegisterToSimulator extends AsyncTask<Object, Void, Void> {
@Override
protected Void doInBackground(Object... sm) {
try{
((SensorManagerSimulator) sm[0]).registerListener((SensorEventListener) sm[1], ((SensorManagerSimulator) sm[0]).getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_FASTEST);
}catch(Exception e)
{
Log.i("error", e.getMessage());
}
return null;
}
}
class UnregisterFromSimulator extends AsyncTask<Object, Void, Void> {
@Override
protected Void doInBackground(Object... sm) {
try{
((SensorManagerSimulator) sm[0]).unregisterListener((SensorEventListener) sm[1]);
}catch(Exception e)
{
Log.i("error : ", e.getMessage());
}
return null;
}
}
我的MainActivity类。在onCreate()中调用了三个AsyncTask类。 onResume()和onPause()(遵循SensorSimulator&#39; s wiki page上的文档)。另外,实现onSensorChanged(),它在相应的TextViews中输出三个值:
public class MainActivity extends ActionBarActivity implements SensorEventListener {
private SensorManagerSimulator mSensorManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
mSensorManager = SensorManagerSimulator.getSystemService(this, SENSOR_SERVICE);
new ConnectToSimulator().execute(mSensorManager);
//mSensorManager.connectSimulator();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
@Override
protected void onResume() {
super.onResume();
new RegisterToSimulator().execute(mSensorManager, this);
}
@Override
protected void onPause() {
new UnregisterFromSimulator().execute(mSensorManager, this);
super.onPause();
}
@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
// Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent E) {
TextView tv;
tv = (TextView) findViewById(R.id.xbox);
tv.setText(Double.toString(E.values[0]));
tv = (TextView) findViewById(R.id.ybox);
tv.setText(Double.toString(E.values[1]));
tv = (TextView) findViewById(R.id.zbox);
tv.setText(Double.toString(E.values[2]));
}
}
这是fragment_main.xml。主要是样板+ 3个TextView,默认情况下打印X,Y和Z:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:id="@+id/xbox"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="X" />
<TextView android:id="@+id/ybox"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Y" />
<TextView android:id="@+id/zbox"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Z" />
</LinearLayout>
</LinearLayout>
logcat中的堆栈跟踪如下:
05-29 05:30:23.239: E/AndroidRuntime(2231): FATAL EXCEPTION: main
05-29 05:30:23.239: E/AndroidRuntime(2231): android.os.NetworkOnMainThreadException
05-29 05:30:23.239: E/AndroidRuntime(2231): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1133)
05-29 05:30:23.239: E/AndroidRuntime(2231): at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:163)
05-29 05:30:23.239: E/AndroidRuntime(2231): at libcore.io.IoBridge.recvfrom(IoBridge.java:506)
05-29 05:30:23.239: E/AndroidRuntime(2231): at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488)
05-29 05:30:23.239: E/AndroidRuntime(2231): at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
05-29 05:30:23.239: E/AndroidRuntime(2231): at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
05-29 05:30:23.239: E/AndroidRuntime(2231): at java.io.InputStreamReader.read(InputStreamReader.java:244)
05-29 05:30:23.239: E/AndroidRuntime(2231): at java.io.BufferedReader.fillBuf(BufferedReader.java:130)
05-29 05:30:23.239: E/AndroidRuntime(2231): at java.io.BufferedReader.readLine(BufferedReader.java:354)
05-29 05:30:23.239: E/AndroidRuntime(2231): at org.openintents.sensorsimulator.hardware.SensorSimulatorClient.readSensor(SensorSimulatorClient.java:654)
05-29 05:30:23.239: E/AndroidRuntime(2231): at org.openintents.sensorsimulator.hardware.SensorSimulatorClient.readSensor(SensorSimulatorClient.java:571)
05-29 05:30:23.239: E/AndroidRuntime(2231): at org.openintents.sensorsimulator.hardware.SensorSimulatorClient.access$1000(SensorSimulatorClient.java:53)
05-29 05:30:23.239: E/AndroidRuntime(2231): at org.openintents.sensorsimulator.hardware.SensorSimulatorClient$1.handleMessage(SensorSimulatorClient.java:505)
05-29 05:30:23.239: E/AndroidRuntime(2231): at android.os.Handler.dispatchMessage(Handler.java:99)
05-29 05:30:23.239: E/AndroidRuntime(2231): at android.os.Looper.loop(Looper.java:137)
05-29 05:30:23.239: E/AndroidRuntime(2231): at android.app.ActivityThread.main(ActivityThread.java:5103)
05-29 05:30:23.239: E/AndroidRuntime(2231): at java.lang.reflect.Method.invokeNative(Native Method)
05-29 05:30:23.239: E/AndroidRuntime(2231): at java.lang.reflect.Method.invoke(Method.java:525)
05-29 05:30:23.239: E/AndroidRuntime(2231): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
05-29 05:30:23.239: E/AndroidRuntime(2231): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-29 05:30:23.239: E/AndroidRuntime(2231): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
因为您使用片段类并在主要活动类中调用异步任务删除以下行
mSensorManager = SensorManagerSimulator.getSystemService(this, SENSOR_SERVICE);
new ConnectToSimulator().execute(mSensorManager);
来自onCreate的并将它放在PlaceholderFragment类中的onActivityCreated()方法中... ...