我正在尝试在Android中激活振动但每次按下按钮时应用程序崩溃.....
silet.setRingerMode(AudioManager.RINGER_MODE_VIBRATE)
我也做了导入的事情。 并声明:
public AudioManager silent;
logcat的:
07-13 22:06:17.956: E/AndroidRuntime(11925): FATAL EXCEPTION: main
07-13 22:06:17.956: E/AndroidRuntime(11925): java.lang.IllegalStateException: Could not execute method of the activity
07-13 22:06:17.956: E/AndroidRuntime(11925): at android.view.View$1.onClick(View.java:2072)
07-13 22:06:17.956: E/AndroidRuntime(11925): at android.view.View.performClick(View.java:2408)
07-13 22:06:17.956: E/AndroidRuntime(11925): at android.view.View$PerformClick.run(View.java:8816)
07-13 22:06:17.956: E/AndroidRuntime(11925): at android.os.Handler.handleCallback(Handler.java:587)
07-13 22:06:17.956: E/AndroidRuntime(11925): at android.os.Handler.dispatchMessage(Handler.java:92)
07-13 22:06:17.956: E/AndroidRuntime(11925): at android.os.Looper.loop(Looper.java:123)
07-13 22:06:17.956: E/AndroidRuntime(11925): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-13 22:06:17.956: E/AndroidRuntime(11925): at java.lang.reflect.Method.invokeNative(Native Method)
07-13 22:06:17.956: E/AndroidRuntime(11925): at java.lang.reflect.Method.invoke(Method.java:521)
07-13 22:06:17.956: E/AndroidRuntime(11925): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-13 22:06:17.956: E/AndroidRuntime(11925): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-13 22:06:17.956: E/AndroidRuntime(11925): at dalvik.system.NativeStart.main(Native Method)
07-13 22:06:17.956: E/AndroidRuntime(11925): Caused by: java.lang.reflect.InvocationTargetException
07-13 22:06:17.956: E/AndroidRuntime(11925): at com.android.hello.MainActivity.click(MainActivity.java:25)
07-13 22:06:17.956: E/AndroidRuntime(11925): at java.lang.reflect.Method.invokeNative(Native Method)
07-13 22:06:17.956: E/AndroidRuntime(11925): at java.lang.reflect.Method.invoke(Method.java:521)
07-13 22:06:17.956: E/AndroidRuntime(11925): at android.view.View$1.onClick(View.java:2067)
07-13 22:06:17.956: E/AndroidRuntime(11925): ... 11 more
07-13 22:06:17.956: E/AndroidRuntime(11925): Caused by: java.lang.NullPointerException
答案 0 :(得分:0)
我正在猜测,但你宣布:
public AudioManager silent;
您是否已将其初始化为:
AudioManager silent = (AudioManager) getSystemService(AUDIO_SERVICE);
你拼错了silent
错误:
silent.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
//silet.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
答案 1 :(得分:0)
试试这段代码它会帮助你
AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
audio.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
需要在AndroidManifest.xml中添加权限
<uses-permission android:name="android.permission.VIBRATE"/>
享受!!!