简体   繁体   中英

Attempt to invoke virtual method 'java.lang.Object android.content.Context

guys i make this new class but when i call this function vibrate in Main Activity i have error

public class vibrate  extends Activity{

public void vibrate() {
        try {
            Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
            // Vibrate for 500 milliseconds
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                v.vibrate(VibrationEffect.createOneShot(200, VibrationEffect.DEFAULT_AMPLITUDE));
            } else {
                //deprecated in API 26
                v.vibrate(200);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
}

and this how i called this class

 vibrate v = new vibrate() ;
 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        v.vibrate();
       
    }

this is the error

W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference

Class vibrate is a subclass of Activity, when you create an instance like this, you are creating an activity. But it's not a right way to create an activity.

vibrate v = new vibrate()

You have to declare your activity inside AndroidManifest.xml file, and invoke the activity by using intent.

Intent intent = new Intent(this, vibrate.class);
startActivity(intent);

But to use the Vibrator, you just need to do like this.

public class VibrateActivity extends Activity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        vibrate();
    }

    private void vibrate() {
        Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
        if (vibrator != null) {
            // Vibrate for 500 milliseconds
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                v.vibrate(VibrationEffect.createOneShot(200, VibrationEffect.DEFAULT_AMPLITUDE));
            } else {
                //deprecated in API 26
                v.vibrate(200);
            }
        }
}

Don't forget to include permission in AndroidManifest.xml file:

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

UPDATE:
Use separate class without doing in the activity.

public class Vibrate extends Activity {

    public void vibrate(Context context) {
        Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
        if (vibrator != null) {
            // Vibrate for 500 milliseconds
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                v.vibrate(VibrationEffect.createOneShot(200, VibrationEffect.DEFAULT_AMPLITUDE));
            } else {
                //deprecated in API 26
                v.vibrate(200);
            }
        }
}

And use it like this in your activity.

Vibrate vibrate = new Vibrate();
vibrate.vibrate(this)

Initialize vibrate v = new vibrate(); in onCreate() and call v.vibrate(); No need to make vibrate as an Activity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String) Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference error:Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference Android app returns "Error: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)" Attempt to invoke virtual method 'android.content.Context android.support.v4.app.FragmentActivity.getApplicationContext()' android Button (android.content.Context) in Button Cannot be Applied to (Java.lang.Object) Flutter:PlatformException(error, Attempt to invoke virtual method 'java.lang.Object android.app.Activity.getSystemService(java.lang.String)' Android NullPointer Attempt to invoke virtual method 'boolean java.util.ArrayList.add(java.lang.Object)' on a null object reference Attempt to invoke virtual method 'android.content.Context.getSharedPreferences(java.lang.String, int)' on a null object reference
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM