如果有人可以提供帮助,那就太好了。 我已经查看了其他地方提供的解决方案,他们似乎无法解决我的问题。
我正在使用DeviceAdminReceiver,并在按下按钮时拥有测试锁定设备。 它实际上工作,但我第一次运行应用程序时,我得到一个活动崩溃。 (此崩溃只会在第一次运行时发生)。
java.lang.RuntimeException: Unable to instantiate receiver c: java.lang.InstantiationException: can't instantiate class com.example.deviceadmin.MainActivity$myAdmin; no empty constructor
我已经尝试了很多东西来解决这个问题:
1)将构造函数添加到DeviceAdminReceiver的子类中 2)在构造函数中调用Super。
这是我见过的解决方案,但它不起作用。
以下是我的应用的简化代码:
公共类MainActivity扩展了Activity {
protected static final int REQUEST_ENABLE = 0;
private static final String LOG = "adminClient";
private DevicePolicyManager mDPM;
private ComponentName mAdminName;
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.d(LOG, "onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
mAdminName = new ComponentName(this, myAdmin.class);
Log.d(LOG, "mAdminName = " + mAdminName.toString());
Button deviceLock = (Button) findViewById(R.id.button1);
deviceLock.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.d(LOG, "LockPhone clicked");
lockPhone();
}
});
}
protected void lockPhone() {
if (!mDPM.isAdminActive(mAdminName)) {
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,
mAdminName);
intent.putExtra("force-locked", DeviceAdminInfo.USES_POLICY_FORCE_LOCK);
startActivityForResult(intent, REQUEST_ENABLE);
}
else {
Log.d(LOG, "lockDown phone");
mDPM.lockNow();
}
}
class myAdmin extends DeviceAdminReceiver {
public myAdmin(){
super();
}
}
}
日志
08-16 14:46:06.383: E/AndroidRuntime(17963): FATAL EXCEPTION: main
08-16 14:46:06.383: E/AndroidRuntime(17963): java.lang.RuntimeException: Unable to instantiate receiver com.example.deviceadmin.MainActivity$myAdmin: java.lang.InstantiationException: can't instantiate class com.example.deviceadmin.MainActivity$myAdmin; no empty constructor
08-16 14:46:06.383: E/AndroidRuntime(17963): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2356)
08-16 14:46:06.383: E/AndroidRuntime(17963): at android.app.ActivityThread.access$1500(ActivityThread.java:140)
08-16 14:46:06.383: E/AndroidRuntime(17963): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1309)
08-16 14:46:06.383: E/AndroidRuntime(17963): at android.os.Handler.dispatchMessage(Handler.java:99)
08-16 14:46:06.383: E/AndroidRuntime(17963): at android.os.Looper.loop(Looper.java:137)
08-16 14:46:06.383: E/AndroidRuntime(17963): at android.app.ActivityThread.main(ActivityThread.java:5047)
08-16 14:46:06.383: E/AndroidRuntime(17963): at java.lang.reflect.Method.invokeNative(Native Method)
08-16 14:46:06.383: E/AndroidRuntime(17963): at java.lang.reflect.Method.invoke(Method.java:525)
08-16 14:46:06.383: E/AndroidRuntime(17963): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
08-16 14:46:06.383: E/AndroidRuntime(17963): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:588)
08-16 14:46:06.383: E/AndroidRuntime(17963): at dalvik.system.NativeStart.main(Native Method)
08-16 14:46:06.383: E/AndroidRuntime(17963): Caused by: java.lang.InstantiationException: can't instantiate class com.example.deviceadmin.MainActivity$myAdmin; no empty constructor
08-16 14:46:06.383: E/AndroidRuntime(17963): at java.lang.Class.newInstanceImpl(Native Method)
08-16 14:46:06.383: E/AndroidRuntime(17963): at java.lang.Class.newInstance(Class.java:1130)
08-16 14:46:06.383: E/AndroidRuntime(17963): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2351)
08-16 14:46:06.383: E/AndroidRuntime(17963): ... 10 more
答案 0 :(得分:0)
myAdmin
需要是public static
内部类,或者是单独的public
Java类。你有一个非public
非 - static
内部类,它不起作用。