Fix Method invocation ‘vibrate may produce java.lang.NullPointerException’

Here is how you can Fix the “Method invocation ‘vibrate’ may produce ‘java.lang.NullPointerException” Error in Android Studio Java

This error is common and can be easily fixed with the following code, it works by encapsulating the java vibrate action code in the if statement

Advertisements

Here is the java code to fix the error

Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
if(vibrator != null) {
vibrator.vibrate(100);
}

For this code to work, you need add vibration permission in the android manifest

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


,

Post navigation

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.