0

我现在在这个 ClassCastException 错误上停留了一段时间,有人可以帮忙看看吗?我基本上是在尝试通过扩展 DeviceAdminReceiver 来遵循 android Device Admin 示例。我基本上剥离了所有内容,只想测试 lockNow() 函数。

这是我的代码:


package com.example.locknow;

public class LockNow extends DeviceAdminReceiver {

void showToast(Context context, CharSequence msg){
    Toast.makeText(context, "Lock_Now App: " + msg, Toast.LENGTH_SHORT).show();
}

@Override
public void onEnabled(Context context, Intent intent){
    showToast(context, "enabled");
}
@Override
public void onDisabled(Context context, Intent intent){
    showToast(context, "disabled");
}

public static class Controller extends Activity {

    static final int REQUEST_CODE_ENABLE_ADMIN = 1;

    DevicePolicyManager mDPM;
    ActivityManager mAM;
    ComponentName mLockNow;

    Button mEnableButton;
    Button mDisableButton;
    Button mForceLockButton;

    /** Called when the activity is first created. */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
        mAM = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
        mLockNow = new ComponentName(Controller.this, LockNow.class);

        setContentView(R.layout.main);

        mEnableButton = (Button)findViewById(R.id.enable_button);
        mEnableButton.setOnClickListener(mEnableListener);
        mDisableButton = (Button)findViewById(R.id.disable_button);
        mDisableButton.setOnClickListener(mDisableListener);
        mForceLockButton = (Button)findViewById(R.id.force_lock_button);
        mForceLockButton.setOnClickListener(mForceLockListener);
    }

    void updateButtonStates(){
        boolean active = mDPM.isAdminActive(mLockNow);
        if (active) {
            mEnableButton.setEnabled(false);
            mDisableButton.setEnabled(true);
            mForceLockButton.setEnabled(true);
        } else {
            mEnableButton.setEnabled(true);
            mDisableButton.setEnabled(false);
            mForceLockButton.setEnabled(false);
        }
    }

    private OnClickListener mEnableListener = new OnClickListener(){
        public void onClick(View v){
            Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
            intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mLockNow);
            intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "additional text");
            startActivityForResult(intent, REQUEST_CODE_ENABLE_ADMIN);
        }
    };

    private OnClickListener mDisableListener = new OnClickListener(){
        public void onClick(View v){
            mDPM.removeActiveAdmin(mLockNow);
            updateButtonStates();
        }
    };

    private OnClickListener mForceLockListener = new OnClickListener(){
        public void onClick(View v){
            boolean active = mDPM.isAdminActive(mLockNow);
            if (active){
                mDPM.lockNow();
            }
        }
    };


}

}


这是清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.locknow"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.BIND_DEVICE_ADMIN" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".LockNow"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity> 
        <activity android:name=".LockNow$Controller">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.SAMPLE_CODE" />
            </intent-filter>
        </activity>
        <receiver android:name=".LockNow"
                  android:label="@string/sample_lock_now"
                  android:permission="android.permission.BIND_DEVICE_ADMIN">
            <intent-filter>
                <action android:name="android.action.DEVICE_ADMIN_ENABLED" />
            </intent-filter>                  
        </receiver>
    </application>
</manifest>

和运行时错误:

04-27 04:02:47.785: ERROR/AndroidRuntime(1553): FATAL EXCEPTION: main
04-27 04:02:47.785: ERROR/AndroidRuntime(1553): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.locknow/com.example.locknow.LockNow}: java.lang.ClassCastException: com.example.locknow.LockNow
04-27 04:02:47.785: ERROR/AndroidRuntime(1553):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
04-27 04:02:47.785: ERROR/AndroidRuntime(1553):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-27 04:02:47.785: ERROR/AndroidRuntime(1553):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-27 04:02:47.785: ERROR/AndroidRuntime(1553):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-27 04:02:47.785: ERROR/AndroidRuntime(1553):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-27 04:02:47.785: ERROR/AndroidRuntime(1553):     at android.os.Looper.loop(Looper.java:123)
04-27 04:02:47.785: ERROR/AndroidRuntime(1553):     at android.app.ActivityThread.main(ActivityThread.java:4627)
04-27 04:02:47.785: ERROR/AndroidRuntime(1553):     at java.lang.reflect.Method.invokeNative(Native Method)
04-27 04:02:47.785: ERROR/AndroidRuntime(1553):     at java.lang.reflect.Method.invoke(Method.java:521)
04-27 04:02:47.785: ERROR/AndroidRuntime(1553):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-27 04:02:47.785: ERROR/AndroidRuntime(1553):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-27 04:02:47.785: ERROR/AndroidRuntime(1553):     at dalvik.system.NativeStart.main(Native Method)
04-27 04:02:47.785: ERROR/AndroidRuntime(1553): Caused by: java.lang.ClassCastException: com.example.locknow.LockNow
04-27 04:02:47.785: ERROR/AndroidRuntime(1553):     at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
04-27 04:02:47.785: ERROR/AndroidRuntime(1553):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
04-27 04:02:47.785: ERROR/AndroidRuntime(1553):     ... 11 more

我是Java和Android的新手,非常感谢任何帮助!谢谢,-J

4

2 回答 2

0

根据文档DeviceAdminReceiver 不是一个活动,因此它可能不应该被定义为一个。根据示例,实际接收器属于<receiver>标签。

于 2011-04-27T04:43:37.700 回答
0

你的LockNow类没有扩展任何Activity,而是一个Receiver,所以你应该在你的androidManifest.xml. 在那里,您已经为同一个类声明了 anactivity和 a 。receiverLockNow

您必须删除活动声明:

<activity android:name=".LockNow" android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity> 

将基础Controller类保留为main launcher activity,将LockNow类保留为receiver

于 2011-04-27T06:07:26.490 回答