所以我是Java / Android的初学者,我正在尝试一个简单的应用程序,它显示了手机加速器的输入。我从一本书中得到了一些示例代码:
package com.example.userinterfaceandvibra;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.util.Log;
public class MainActivity
extends Activity
implements SensorEventListener
{
TextView statusTv;
TextView messagesTv;
SensorManager sensorManager;
Sensor sensor;
float g=9.81f;
float x, y, z;
/*** Main -- automatically called methods ***/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initGUI();
}
@Override
public void onResume() {
super.onResume();
initAccel();
msg("Running.");
}
@Override
public void onPause() {
super.onPause();
closeAccel();
msg("Paused. \n");
}
@Override
public void onSensorChanged(SensorEvent event) {
x=event.values[1]/g;
y=event.values[2]/g;
z=event.values[3]/g;
statusTv.setText(String.format("x: %3.2f y: %3.2f, z: %3.2f", x,y,z));
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
//Must have when Activity implements SensorEventListener.
}
/*** Accelerometer ***/
void initAccel()
{
msg("Accelerometer initialization...");
sensorManager=(SensorManager) getSystemService(SENSOR_SERVICE);
sensor=sensorManager.getDefaultSensor(
Sensor.TYPE_ACCELEROMETER);
sensorManager.registerListener(
this,
sensor,
sensorManager.SENSOR_DELAY_NORMAL);
}
void closeAccel()
{
msg("Accelerometer closing...");
sensorManager.unregisterListener(this, sensor);
}
void initGUI()
{
//Window
setRequestedOrientation(
ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
getWindow().setFlags(
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
//Contents
LinearLayout container=new LinearLayout(this);
container.setOrientation(android.widget.LinearLayout.VERTICAL);
statusTv = new TextView(this);
container.addView(statusTv);
messagesTv = new TextView(this);
container.addView(messagesTv);
//Show
setContentView(container);
msg("User interface created.");
}
public void msg(String s)
{
if (7<=messagesTv.getLineCount()) messagesTv.setText("");
messagesTv.append(s);
}
}
我的代码没有运行任何错误,但是当我尝试启动它时,我得到了经典&#34;不幸的是,myApp已停止。&#34;起初我认为这是因为我的应用程序没有使用加速器的权限,所以这是我的清单文件,我希望(希望)添加正确的代码以获得许可。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.userinterfaceandvibra"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-feature android:name="android.hardware.sensor.accelerometer"
android:required="true"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.userinterfaceandvibra.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>
最后,我查看了log cat以尝试查看正在抛出的错误。因为我是如此新颖,所以我并没有真正得到它的大部分内容,但很明显,第一个抛出的错误是&#34; Exception调度输入事件。&#34;但是我看不到行号,而且错误太模糊,我真的不知道下一步该尝试什么。对于那些比我更聪明的人来说,这里有logcat读数:
04-05 20:13:00.084: D/SensorManager(21947): registerListener :: create queue :: handler = 0, name = K330 3-axis Accelerometer, delay = 200000,
04-05 20:13:00.104: D/SensorManager(21947): unregisterListener ::
04-05 20:13:00.184: D/SensorManager(21947): registerListener :: create queue :: handler = 0, name = K330 3-axis Accelerometer, delay = 200000,
04-05 20:13:00.304: D/libEGL(21947): loaded /vendor/lib/egl/libEGL_adreno.so
04-05 20:13:00.304: D/libEGL(21947): loaded /vendor/lib/egl/libGLESv1_CM_adreno.so
04-05 20:13:00.314: D/libEGL(21947): loaded /vendor/lib/egl/libGLESv2_adreno.so
04-05 20:13:00.324: I/Adreno-EGL(21947): <qeglDrvAPI_eglInitialize:316>: EGL 1.4 QUALCOMM build: (CL4169980)
04-05 20:13:00.324: I/Adreno-EGL(21947): OpenGL ES Shader Compiler Version: 17.01.10.SPL
04-05 20:13:00.324: I/Adreno-EGL(21947): Build Date: 09/26/13 Thu
04-05 20:13:00.324: I/Adreno-EGL(21947): Local Branch:
04-05 20:13:00.324: I/Adreno-EGL(21947): Remote Branch:
04-05 20:13:00.324: I/Adreno-EGL(21947): Local Patches:
04-05 20:13:00.324: I/Adreno-EGL(21947): Reconstruct Branch:
04-05 20:13:00.424: D/OpenGLRenderer(21947): Enabling debug mode 0
04-05 20:13:00.434: E/SensorManager(21947): Exception dispatching input event.
04-05 20:13:00.434: D/AndroidRuntime(21947): Shutting down VM
04-05 20:13:00.434: W/dalvikvm(21947): threadid=1: thread exiting with uncaught exception (group=0x4176d898)
04-05 20:13:00.444: E/AndroidRuntime(21947): FATAL EXCEPTION: main
04-05 20:13:00.444: E/AndroidRuntime(21947): java.lang.ArrayIndexOutOfBoundsException: length=3; index=3
04-05 20:13:00.444: E/AndroidRuntime(21947): at com.example.userinterfaceandvibra.MainActivity.onSensorChanged(MainActivity.java:54)
04-05 20:13:00.444: E/AndroidRuntime(21947): at android.hardware.SystemSensorManager$SensorEventQueue.dispatchSensorEvent(SystemSensorManager.java:467)
04-05 20:13:00.444: E/AndroidRuntime(21947): at android.os.MessageQueue.nativePollOnce(Native Method)
04-05 20:13:00.444: E/AndroidRuntime(21947): at android.os.MessageQueue.next(MessageQueue.java:132)
04-05 20:13:00.444: E/AndroidRuntime(21947): at android.os.Looper.loop(Looper.java:124)
04-05 20:13:00.444: E/AndroidRuntime(21947): at android.app.ActivityThread.main(ActivityThread.java:5419)
04-05 20:13:00.444: E/AndroidRuntime(21947): at java.lang.reflect.Method.invokeNative(Native Method)
04-05 20:13:00.444: E/AndroidRuntime(21947): at java.lang.reflect.Method.invoke(Method.java:525)
04-05 20:13:00.444: E/AndroidRuntime(21947): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
04-05 20:13:00.444: E/AndroidRuntime(21947): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
04-05 20:13:00.444: E/AndroidRuntime(21947): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
如果你看一下logcat错误:
04-05 20:13:00.444: E/AndroidRuntime(21947): java.lang.ArrayIndexOutOfBoundsException: length=3; index=3
04-05 20:13:00.444: E/AndroidRuntime(21947): at com.example.userinterfaceandvibra.MainActivity.onSensorChanged(MainActivity.java:54)
数组长度为3,访问的索引为3.由于数组以索引0开头,因此可以通过索引0,1,2访问3个数组元素。因此,改变你的onSensorChanged方法:
x=event.values[1]/g;
y=event.values[2]/g;
z=event.values[3]/g;
到
x=event.values[0]/g;
y=event.values[1]/g;
z=event.values[2]/g;