Error using Lock Screen

2

I'm trying to block the screen with the following code:

protected void onBloquear(View view) {

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

Source: link

But it gives the following error.

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

Does anyone have an idea what it can be?

    
asked by anonymous 14.12.2016 / 23:41

1 answer

3

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

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

See here in the documentation .

    
15.12.2016 / 01:44