我无法找到问题所在。 当我摇动手机时,它应该做的是CHange textview的文本,但相反,当我摇动它时,它会崩溃。 我的代码中的问题是什么? 我试图查看logcat,但我还不熟悉从中了解的东西 (特别是因为我的母语不是英语)。 谢谢! 主要活动
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();
}
}
Shaker活动
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>
我的LOGCAT:
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)
答案 0 :(得分:1)
正如您的logcat
所说,
java.lang.SecurityException:需要VIBRATE权限
你需要振动许可。所以你需要在清单文件中添加以下行:
<uses-permission android:name="android.permission.VIBRATE"/>