0

我正在尝试在 android 中激活振动,但每次按下按钮时应用程序崩溃.....

silet.setRingerMode(AudioManager.RINGER_MODE_VIBRATE)

我也做了进口的事情。并声明:

public AudioManager silent;

日志猫:

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
4

2 回答 2

0

我在猜测,但您声明:

public AudioManager silent;

您是否使用以下方法对其进行了初始化:

AudioManager silent = (AudioManager) getSystemService(AUDIO_SERVICE);

silent在这里拼错了:

silent.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
//silet.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
于 2012-07-13T19:09:32.120 回答
0

试试这个代码它会帮助你

       AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

       audio.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);

需要在AndroidManifest.xml中添加权限

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

享受!!!

于 2012-07-13T19:12:49.573 回答