Todd Mathison
Todd Mathison

Reputation: 133

Run Android app as Administrator

I'm trying to write a simple app, and follow the ApiDemos sample. My app is crashing on startup. I suspect a problem with my manifest, but I'm not sure. Any ideas please?

    public class managerActivity extends DeviceAdminReceiver {
        public class Controller extends Activity {

        static final int RESULT_ENABLE = 1;

        DevicePolicyManager mDPM;
        ActivityManager mAM;
        ComponentName mDeviceAdminSample;

        Button mEnableButton;
        Button mDisableButton;

        @Override
            public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);

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

            setContentView(R.layout.main);

        }
    }

and then in my manifest, I have:

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name="com.appname.managerActivity$Controller"
              android:label="@string/app_name">

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
            <category android:name="android.intent.category.HOME" />
            <category android:name="android.intent.category.DEFAULT" />
            <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />

        </intent-filter>
    </activity>

   <!-- Device Admin Samples -->

    <receiver android:name="com.appname.managerActivity"
            android:label="@string/sample_device_admin"
            android:description="@string/sample_device_admin_description"
            android:permission="android.permission.BIND_DEVICE_ADMIN">
        <meta-data android:name="android.app.device_admin"
                   android:resource="@xml/device_admin_sample" />
        <intent-filter>
            <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
        </intent-filter>
    </receiver>

Here's the crash:

11-11 15:20:52.310: ERROR/AndroidRuntime(553): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.appname.manager/com.appname.managerActivity$Controller}: java.lang.InstantiationException: com.appname.managerActivity$Controller

Upvotes: 1

Views: 3868

Answers (1)

Seva Alekseyev
Seva Alekseyev

Reputation: 61341

The nested class cannot be created from outside of its enclosing class, unless it's static. Move your activity to a standalone class (i. e. not nested), or make the activity class static.

Upvotes: 1

Related Questions