无法在模拟器上启动AccelerometerDemo

时间:2013-09-29 23:31:30

标签: android android-emulator accelerometer sensor open-intents

我无法集成我的应用程序,sensorsimulator和模拟器。我想要的只是从Accelerometer读取原始数据。当我调试时,我得到错误Source Not Found。在模拟器上启动应用程序时,它表示遗憾的是无法运行您的应用程序。 主要课程:

    package com.example;

    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.os.Bundle;
    import android.app.Activity;
    import android.widget.RelativeLayout;
    import android.widget.TextView;

public class MainActivity extends Activity implements SensorEventListener {
    private SensorManagerSimulator mSensorManager;
    private Sensor mAccelerometer;

    TextView title, tv, tv1, tv2;
    RelativeLayout layout;

    @Override
    public final void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mSensorManager = SensorManagerSimulator.getSystemService(this,
                SENSOR_SERVICE);
        mAccelerometer = mSensorManager
                .getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

        mSensorManager.connectSimulator();

        // get layout
        layout = (RelativeLayout) findViewById(R.id.relative);

        // get textviews
        title = (TextView) findViewById(R.id.name);
        tv = (TextView) findViewById(R.id.xval);
        tv1 = (TextView) findViewById(R.id.yval);
        tv2 = (TextView) findViewById(R.id.zval);

    }

    @Override
    public final void onAccuracyChanged(Sensor sensor, int accuracy) {
        // Do something here if sensor accuracy changes.
    }

    @Override
    public final void onSensorChanged(SensorEvent event) {
        // Many sensors return 3 values, one for each axis.
        float x = event.values[0];
        float y = event.values[1];
        float z = event.values[2];

        // display values using TextView
        title.setText(R.string.app_name);
        tv.setText("X axis" + "\t\t" + x);
        tv1.setText("Y axis" + "\t\t" + y);
        tv2.setText("Z axis" + "\t\t" + z);

    }

    @Override
    protected void onResume() {
        super.onResume();
        mSensorManager.registerListener(this, mAccelerometer,
                SensorManagerSimulator.SENSOR_DELAY_NORMAL);
    }

    @Override
    protected void onPause() {
        super.onPause();
        mSensorManager.unregisterListener(this);
    }
}

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

记录CAT:

09-30 09:01:05.685: E/Trace(999): error opening trace file: No such file or directory (2)
09-30 09:01:06.425: I/Hardware(999): Starting connection...
09-30 09:01:06.536: I/Hardware(999): Connecting to 10.0.2.2 : 8010
09-30 09:01:06.555: D/AndroidRuntime(999): Shutting down VM
09-30 09:01:06.555: W/dalvikvm(999): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
09-30 09:01:06.575: E/AndroidRuntime(999): FATAL EXCEPTION: main
09-30 09:01:06.575: E/AndroidRuntime(999): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.MainActivity}: android.os.NetworkOnMainThreadException
09-30 09:01:06.575: E/AndroidRuntime(999):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
09-30 09:01:06.575: E/AndroidRuntime(999):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
09-30 09:01:06.575: E/AndroidRuntime(999):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
09-30 09:01:06.575: E/AndroidRuntime(999):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
09-30 09:01:06.575: E/AndroidRuntime(999):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-30 09:01:06.575: E/AndroidRuntime(999):  at android.os.Looper.loop(Looper.java:137)
09-30 09:01:06.575: E/AndroidRuntime(999):  at android.app.ActivityThread.main(ActivityThread.java:4745)
09-30 09:01:06.575: E/AndroidRuntime(999):  at java.lang.reflect.Method.invokeNative(Native Method)
09-30 09:01:06.575: E/AndroidRuntime(999):  at java.lang.reflect.Method.invoke(Method.java:511)
09-30 09:01:06.575: E/AndroidRuntime(999):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-30 09:01:06.575: E/AndroidRuntime(999):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-30 09:01:06.575: E/AndroidRuntime(999):  at dalvik.system.NativeStart.main(Native Method)
09-30 09:01:06.575: E/AndroidRuntime(999): Caused by: android.os.NetworkOnMainThreadException
09-30 09:01:06.575: E/AndroidRuntime(999):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
09-30 09:01:06.575: E/AndroidRuntime(999):  at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
09-30 09:01:06.575: E/AndroidRuntime(999):  at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
09-30 09:01:06.575: E/AndroidRuntime(999):  at libcore.io.IoBridge.connect(IoBridge.java:112)
09-30 09:01:06.575: E/AndroidRuntime(999):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
09-30 09:01:06.575: E/AndroidRuntime(999):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
09-30 09:01:06.575: E/AndroidRuntime(999):  at java.net.Socket.startupSocket(Socket.java:566)
09-30 09:01:06.575: E/AndroidRuntime(999):  at java.net.Socket.tryAllAddresses(Socket.java:127)
09-30 09:01:06.575: E/AndroidRuntime(999):  at java.net.Socket.<init>(Socket.java:177)
09-30 09:01:06.575: E/AndroidRuntime(999):  at java.net.Socket.<init>(Socket.java:149)
09-30 09:01:06.575: E/AndroidRuntime(999):  at org.openintents.sensorsimulator.hardware.SensorSimulatorClient.connect(SensorSimulatorClient.java:116)
09-30 09:01:06.575: E/AndroidRuntime(999):  at org.openintents.sensorsimulator.hardware.SensorManagerSimulator.connectSimulator(SensorManagerSimulator.java:220)
09-30 09:01:06.575: E/AndroidRuntime(999):  at com.example.MainActivity.onCreate(MainActivity.java:29)
09-30 09:01:06.575: E/AndroidRuntime(999):  at android.app.Activity.performCreate(Activity.java:5008)
09-30 09:01:06.575: E/AndroidRuntime(999):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
09-30 09:01:06.575: E/AndroidRuntime(999):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
09-30 09:01:06.575: E/AndroidRuntime(999):  ... 11 more
09-30 09:06:06.676: I/Process(999): Sending signal. PID: 999 SIG: 9

karanbalkar / AndroidAccelerometerDemo指南

谢谢,

1 个答案:

答案 0 :(得分:0)

Android模拟器不支持加速计,但您可以尝试Sensor Simulator