gpt4 book ai didi

android - 使用加速度计应用程序时崩溃

转载 作者:行者123 更新时间:2023-12-03 16:53:11 25 4
gpt4 key购买 nike

我找不到问题出在哪里。
当我摇晃手机它应该做的是改变
textview 的文本,但是当我摇动它时它会崩溃。
我的代码有什么问题?
我试图查看 logcat,但我还不熟练从中理解一些东西
(特别是因为我的母语不是英语)。
谢谢!
主要 Activity

package nir.rauch.accelrate;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Vibrator;
import android.widget.TextView;

public class MainActivity extends Activity
{
private ShakeListener mShaker;
public TextView text;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView text =(TextView)findViewById(R.id.textView1);
final Vibrator vibe = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
mShaker = new ShakeListener(this);
mShaker.setOnShakeListener(new ShakeListener.OnShakeListener () {
public void onShake()
{
vibe.vibrate(100);
text.setText("" + "what a suprise! after the shaking The text has been changed and it also "
+ "Vibrates , so AMAZING!");
}
});
}

@Override
public void onResume()
{
mShaker.resume();
super.onResume();
}
@Override
public void onPause()
{
mShaker.pause();
super.onPause();
}
}

摇床 Activity
    package nir.rauch.accelrate;

import android.hardware.SensorListener;
import android.hardware.SensorManager;
import android.content.Context;
import java.lang.UnsupportedOperationException;

public class ShakeListener implements SensorListener
{
private static final int FORCE_THRESHOLD = 350;
private static final int TIME_THRESHOLD = 100;
private static final int SHAKE_TIMEOUT = 500;
private static final int SHAKE_DURATION = 1000;
private static final int SHAKE_COUNT = 3;

private SensorManager mSensorMgr;
private float mLastX=-1.0f, mLastY=-1.0f, mLastZ=-1.0f;
private long mLastTime;
private OnShakeListener mShakeListener;
private Context mContext;
private int mShakeCount = 0;
private long mLastShake;
private long mLastForce;

public interface OnShakeListener
{
public void onShake();
}

public ShakeListener(Context context)
{
mContext = context;
resume();
}

public void setOnShakeListener(OnShakeListener listener)
{
mShakeListener = listener;
}

public void resume() {
mSensorMgr = (SensorManager)mContext.getSystemService(Context.SENSOR_SERVICE);
if (mSensorMgr == null) {
throw new UnsupportedOperationException("Sensors not supported");
}
boolean supported = mSensorMgr.registerListener(this, SensorManager.SENSOR_ACCELEROMETER, SensorManager.SENSOR_DELAY_GAME);
if (!supported) {
mSensorMgr.unregisterListener(this, SensorManager.SENSOR_ACCELEROMETER);
throw new UnsupportedOperationException("Accelerometer not supported");
}
}

public void pause() {
if (mSensorMgr != null) {
mSensorMgr.unregisterListener(this, SensorManager.SENSOR_ACCELEROMETER);
mSensorMgr = null;
}
}

public void onAccuracyChanged(int sensor, int accuracy) { }

public void onSensorChanged(int sensor, float[] values)
{
if (sensor != SensorManager.SENSOR_ACCELEROMETER) return;
long now = System.currentTimeMillis();

if ((now - mLastForce) > SHAKE_TIMEOUT) {
mShakeCount = 0;
}

if ((now - mLastTime) > TIME_THRESHOLD) {
long diff = now - mLastTime;
float speed = Math.abs(values[SensorManager.DATA_X] + values[SensorManager.DATA_Y] + values[SensorManager.DATA_Z] - mLastX - mLastY - mLastZ) / diff * 10000;
if (speed > FORCE_THRESHOLD) {
if ((++mShakeCount >= SHAKE_COUNT) && (now - mLastShake > SHAKE_DURATION)) {
mLastShake = now;
mShakeCount = 0;
if (mShakeListener != null) {
mShakeListener.onShake();
}
}
mLastForce = now;
}
mLastTime = now;
mLastX = values[SensorManager.DATA_X];
mLastY = values[SensorManager.DATA_Y];
mLastZ = values[SensorManager.DATA_Z];
}
}

}

XML 文件:
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="40sp"
android:text="Does the Accelorometer works? Shake it and found out!" />


</RelativeLayout>

我的日志:
    04-12 18:49:15.831: E/SensorManager(5343): Exception dispatching input event.
04-12 18:49:15.831: E/AndroidRuntime(5343): FATAL EXCEPTION: main
04-12 18:49:15.831: E/AndroidRuntime(5343): java.lang.SecurityException: Requires VIBRATE permission
04-12 18:49:15.831: E/AndroidRuntime(5343): at android.os.Parcel.readException(Parcel.java:1431)
04-12 18:49:15.831: E/AndroidRuntime(5343): at android.os.Parcel.readException(Parcel.java:1385)
04-12 18:49:15.831: E/AndroidRuntime(5343): at android.os.IVibratorService$Stub$Proxy.vibrateMagnitude(IVibratorService.java:342)
04-12 18:49:15.831: E/AndroidRuntime(5343): at android.os.SystemVibrator.vibrate(SystemVibrator.java:115)
04-12 18:49:15.831: E/AndroidRuntime(5343): at android.os.SystemVibrator.vibrate(SystemVibrator.java:83)
04-12 18:49:15.831: E/AndroidRuntime(5343): at nir.rauch.accelrate.MainActivity$1.onShake(MainActivity.java:25)
04-12 18:49:15.831: E/AndroidRuntime(5343): at nir.rauch.accelrate.ShakeListener.onSensorChanged(ShakeListener.java:79)
04-12 18:49:15.831: E/AndroidRuntime(5343): at android.hardware.LegacySensorManager$LegacyListener.onSensorChanged(LegacySensorManager.java:274)
04-12 18:49:15.831: E/AndroidRuntime(5343): at android.hardware.SystemSensorManager$SensorEventQueue.dispatchSensorEvent(SystemSensorManager.java:467)
04-12 18:49:15.831: E/AndroidRuntime(5343): at android.os.MessageQueue.nativePollOnce(Native Method)
04-12 18:49:15.831: E/AndroidRuntime(5343): at android.os.MessageQueue.next(MessageQueue.java:132)
04-12 18:49:15.831: E/AndroidRuntime(5343): at android.os.Looper.loop(Looper.java:124)
04-12 18:49:15.831: E/AndroidRuntime(5343): at android.app.ActivityThread.main(ActivityThread.java:5419)
04-12 18:49:15.831: E/AndroidRuntime(5343): at java.lang.reflect.Method.invokeNative(Native Method)
04-12 18:49:15.831: E/AndroidRuntime(5343): at java.lang.reflect.Method.invoke(Method.java:525)
04-12 18:49:15.831: E/AndroidRuntime(5343): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
04-12 18:49:15.831: E/AndroidRuntime(5343): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
04-12 18:49:15.831: E/AndroidRuntime(5343): at dalvik.system.NativeStart.main(Native Method)
04-12 18:55:45.396: E/SensorManager(6408): Exception dispatching input event.
04-12 18:55:45.401: E/AndroidRuntime(6408): FATAL EXCEPTION: main
04-12 18:55:45.401: E/AndroidRuntime(6408): java.lang.SecurityException: Requires VIBRATE permission
04-12 18:55:45.401: E/AndroidRuntime(6408): at android.os.Parcel.readException(Parcel.java:1431)
04-12 18:55:45.401: E/AndroidRuntime(6408): at android.os.Parcel.readException(Parcel.java:1385)
04-12 18:55:45.401: E/AndroidRuntime(6408): at android.os.IVibratorService$Stub$Proxy.vibrateMagnitude(IVibratorService.java:342)
04-12 18:55:45.401: E/AndroidRuntime(6408): at android.os.SystemVibrator.vibrate(SystemVibrator.java:115)
04-12 18:55:45.401: E/AndroidRuntime(6408): at android.os.SystemVibrator.vibrate(SystemVibrator.java:83)
04-12 18:55:45.401: E/AndroidRuntime(6408): at nir.rauch.accelrate.MainActivity$1.onShake(MainActivity.java:25)
04-12 18:55:45.401: E/AndroidRuntime(6408): at nir.rauch.accelrate.ShakeListener.onSensorChanged(ShakeListener.java:79)
04-12 18:55:45.401: E/AndroidRuntime(6408): at android.hardware.LegacySensorManager$LegacyListener.onSensorChanged(LegacySensorManager.java:274)
04-12 18:55:45.401: E/AndroidRuntime(6408): at android.hardware.SystemSensorManager$SensorEventQueue.dispatchSensorEvent(SystemSensorManager.java:467)
04-12 18:55:45.401: E/AndroidRuntime(6408): at android.os.MessageQueue.nativePollOnce(Native Method)
04-12 18:55:45.401: E/AndroidRuntime(6408): at android.os.MessageQueue.next(MessageQueue.java:132)
04-12 18:55:45.401: E/AndroidRuntime(6408): at android.os.Looper.loop(Looper.java:124)
04-12 18:55:45.401: E/AndroidRuntime(6408): at android.app.ActivityThread.main(ActivityThread.java:5419)
04-12 18:55:45.401: E/AndroidRuntime(6408): at java.lang.reflect.Method.invokeNative(Native Method)
04-12 18:55:45.401: E/AndroidRuntime(6408): at java.lang.reflect.Method.invoke(Method.java:525)
04-12 18:55:45.401: E/AndroidRuntime(6408): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
04-12 18:55:45.401: E/AndroidRuntime(6408): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
04-12 18:55:45.401: E/AndroidRuntime(6408): at dalvik.system.NativeStart.main(Native Method)

最佳答案

作为您的logcat说过,
java.lang.SecurityException: 需要 VIBRATE 权限
你需要振动权限。所以你需要将以下行添加到 list 文件中:

 <uses-permission android:name="android.permission.VIBRATE"/>

关于android - 使用加速度计应用程序时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23032751/

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