2011-04-27 49 views
0

我被困在這個ClassCastException錯誤一段時間了,任何人都可以幫忙看一下嗎?我基本上試圖通過擴展DeviceAdminReceiver來遵循android設備管理示例。我基本上把所有東西都剝離了,只是想測試lockNow()函數。Android/Java ClassCastException錯誤

這是我的代碼:


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> 

而且RU ntime錯誤:

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

回答

0

您的LockNow類沒有擴展任何Activity,而是Receiver,所以您應該在您的androidManifest.xml中聲明它。在那裏你已經爲LockNow類聲明瞭activityreceiver

,必須先刪除活動聲明:

<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 activityLockNow類爲receiver

0

根據Documentation DeviceAdminReceiver不是一個活動,因此它可能不應該被定義爲一個。根據Example,實際的接收器屬於<receiver>標籤。

相關問題