DeviceAdminAPI не работает

Я пытаюсь использовать API администратора устройства в своем приложении.

Мой класс вызывающего абонента DeviceAdmin:

public class DeviceAdminCaller extends Activity implements OnClickListener {
static final int activationCode = 47;
Button b;
DevicePolicyManager mgr;
ComponentName deviceAdminComponent = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    b.findViewById(R.id.ButtonAdmin);
    b.setOnClickListener(this);
    deviceAdminComponent = new ComponentName(this, DeviceAdmin.class);
    mgr = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE);
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.ButtonAdmin:
        if (mgr.isAdminActive(deviceAdminComponent)) {
            mgr.lockNow();
        } else {
            Toast.makeText(getApplicationContext(), "starting", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
            intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, deviceAdminComponent);
            intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "We recommend you to enable this");
            startActivity(intent);
        }
    }
}

}

И мой подкласс DeviceAdminReciever это:

public class DeviceAdmin extends DeviceAdminReceiver {
@Override
public void onEnabled(Context context, Intent intent) {
    // TODO Auto-generated method stub
    super.onEnabled(context, intent);
    Log.i("Device Admin", "Enabled");
}

@Override
public String onDisableRequested(Context context, Intent intent) {
    // TODO Auto-generated method stub
    return "Admin disable Requested";
}

@Override
public void onDisabled(Context context, Intent intent) {
    // TODO Auto-generated method stub
    super.onDisabled(context, intent);
    Log.i("Device Admin", "Disables");

}

@Override
public void onPasswordChanged(Context context, Intent intent) {
    // TODO Auto-generated method stub
    super.onPasswordChanged(context, intent);
    Log.i("Device Admin", "Password Changed");
}
}

Часть Манифеста, содержащая DeviceAdmin:

<activity
        android:name=".DeviceAdmin"
        android:label="activity_sample_device_admin" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.SAMPLE_CODE" />
        </intent-filter>
    </activity>
    <activity
        android:name=".DeviceAdminCaller"
        android:label="activity_caller_device_admin" >
        <intent-filter>
            <action android:name="android.intent.action.DEVICE_ADMIN_CALLER" />

            <category android:name="android.intent.category.DEFAULT" />
            <!-- <category android:name="android.intent.category.SAMPLE_CODE" /> -->
        </intent-filter>
    </activity>

    <receiver
        android:name=".DeviceAdmin"
        android:permission="android.permission.BIND_DEVICE_ADMIN" >
        <meta-data
            android:name="android.app.device_admin_sample"
            android:resource="@xml/device_admin_sample" />

        <intent-filter>
            <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
            <action android:name="android.app.action.ACTION_DEVICE_ADMIN_DISABLE_REQUESTED" />
            <action android:name="android.app.action.ACTION_DEVICE_ADMIN_DISABLED" />
        </intent-filter>
    </receiver>

Выход LogCat:

12-11 18:25:56.677: E/AndroidRuntime(32734): Process: com.example.applock, PID: 32734
12-11 18:25:56.677: E/AndroidRuntime(32734): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.applock/com.example.applock.DeviceAdminCaller}: java.lang.NullPointerException

12-11 18:25:56.677: E/AndroidRuntime(32734):    at com.example.applock.DeviceAdminCaller.onCreate(DeviceAdminCaller.java:23)

12-11 18:35:08.759: E/AndroidRuntime(4409): Process: com.example.applock, PID: 4409

12-11 18:35:08.759: E/AndroidRuntime(4409): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.applock/com.example.applock.DeviceAdminCaller}: java.lang.NullPointerException

12-11 18:35:08.759: E/AndroidRuntime(4409):     at com.example.applock.DeviceAdminCaller.onCreate(DeviceAdminCaller.java:23)

Что я делаю неправильно? У меня есть проект, представленный сегодня. Любая помощь будет оценена.:)

0 ответов

Другие вопросы по тегам