Error using Lock Screen

Asked

Viewed 42 times

2

I am trying to Block the Screen with the following code:

protected void onBloquear(View view) {

    DevicePolicyManager mDPM = null;
    mDPM.lockNow();
}

Source: https://developer.android.com/guide/topics/admin/device-admin.html

But it makes the following mistake.

Caused by: java.lang.NullPointerException: 
Attempt to invoke virtual method 'void android.app.admin.DevicePolicyManager.lockNow()' 
on a null object reference

Does anyone have any idea what it might be?

1 answer

3


The error is occurring because your DevicePolicyManager is not instantiated, so it returns null. You should be able to receive an identifier for the DevicePolicyManager as follows:

DevicePolicyManager mDPM =
    (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);

Behold here in the documentation.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.