gpt4 book ai didi

java - 如何访问 Android 中的加速度计

转载 作者:行者123 更新时间:2023-12-01 13:10:45 25 4
gpt4 key购买 nike

所以我是 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);
}


}

我的代码没有运行任何错误,但是当我尝试启动它时,我收到经典的“不幸的是,myApp 已停止”。起初我以为这是因为我的应用程序没有使用加速器的权限,所以这是我的 list 文件,我(希望)添加了正确的代码来获取权限。

<?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 来尝试看看抛出了哪些错误。因为我太新了,所以我并没有真正理解其中的大部分内容,但很明显,抛出的第一个错误是“异常调度输入事件”。但是我看不到行号,并且错误如此模糊,我真的不知道下一步要尝试什么。只是为了让所有比我聪明的人开心一下,这里是 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)

最佳答案

如果你查看 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;

关于java - 如何访问 Android 中的加速度计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22889608/

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