这是日志。有人可以告诉我为什么会这样吗?

07-29 18:20:44.109: D/AndroidRuntime(8836): Shutting down VM
07-29 18:20:44.109: W/dalvikvm(8836): threadid=1: thread exiting with uncaught exception (group=0x41443300)
07-29 18:20:44.109: E/AndroidRuntime(8836): FATAL EXCEPTION: main
07-29 18:20:44.109: E/AndroidRuntime(8836): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.liamwli.parent.trap/com.liamwli.parent.trap.ParentTrap}: java.lang.ClassCastException: com.liamwli.parent.trap.ParentTrap cannot be cast to android.app.Activity
07-29 18:20:44.109: E/AndroidRuntime(8836):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2096)
07-29 18:20:44.109: E/AndroidRuntime(8836):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2207)
07-29 18:20:44.109: E/AndroidRuntime(8836):     at android.app.ActivityThread.access$600(ActivityThread.java:139)
07-29 18:20:44.109: E/AndroidRuntime(8836):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
07-29 18:20:44.109: E/AndroidRuntime(8836):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-29 18:20:44.109: E/AndroidRuntime(8836):     at android.os.Looper.loop(Looper.java:137)
07-29 18:20:44.109: E/AndroidRuntime(8836):     at android.app.ActivityThread.main(ActivityThread.java:4896)
07-29 18:20:44.109: E/AndroidRuntime(8836):     at java.lang.reflect.Method.invokeNative(Native Method)
07-29 18:20:44.109: E/AndroidRuntime(8836):     at java.lang.reflect.Method.invoke(Method.java:511)
07-29 18:20:44.109: E/AndroidRuntime(8836):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:788)
07-29 18:20:44.109: E/AndroidRuntime(8836):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
07-29 18:20:44.109: E/AndroidRuntime(8836):     at dalvik.system.NativeStart.main(Native Method)
07-29 18:20:44.109: E/AndroidRuntime(8836): Caused by: java.lang.ClassCastException: com.liamwli.parent.trap.ParentTrap cannot be cast to android.app.Activity
07-29 18:20:44.109: E/AndroidRuntime(8836):     at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
07-29 18:20:44.109: E/AndroidRuntime(8836):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2087)
07-29 18:20:44.109: E/AndroidRuntime(8836):     ... 11 more


和活动:

package com.liamwli.parent.trap;

import android.os.Bundle;
import android.app.Activity;
import android.app.admin.DeviceAdminReceiver;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.support.v4.app.NavUtils;



class ParentTrap extends Activity {

    Button dae, dad, spass;
    EditText pass;
    ComponentName mDeviceAdminSample;
    static final int RESULT_ENABLE = 1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        dae = (Button) findViewById(R.id.benablea);
        dae.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                mDeviceAdminSample = new ComponentName(ParentTrap.this,
                        Controller.class);

                Intent intent = new Intent(
                        DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
                intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,
                        mDeviceAdminSample);
                intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
                        "Title");
                startActivityForResult(intent, RESULT_ENABLE);

            }
        });
        dad = (Button) findViewById(R.id.button1);
        dad.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

            }
        });
        spass = (Button) findViewById(R.id.button2);
        spass.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

            }
        });

        pass = (EditText) findViewById(R.id.editText1);

        dad.setEnabled(false);
    }

    public class Controller extends DeviceAdminReceiver {

        void showToast(Context context, String msg) {
            String status = "Status";
            Toast.makeText(context, status, Toast.LENGTH_SHORT).show();
        }

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

        @Override
        public CharSequence onDisableRequested(Context context, Intent intent) {
            return "Disable Requested";
        }

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

        @Override
        public void onPasswordChanged(Context context, Intent intent) {
            showToast(context, "Device PIN Changed");
        }

    }

}

class evilliam {

}
// @Override
// public boolean onCreateOptionsMenu(Menu menu) {
// getMenuInflater().inflate(R.menu.main, menu);
// return true;
// }


谢谢您的帮助-我一直在尝试遵循dev docs:/

新日志:

07-29 18:43:25.404: D/dalvikvm(13404): newInstance failed: Lcom/liamwli/parent/trap/ParentTrap; not accessible to Landroid/app/Instrumentation;
07-29 18:43:25.404: D/AndroidRuntime(13404): Shutting down VM
07-29 18:43:25.404: W/dalvikvm(13404): threadid=1: thread exiting with uncaught exception (group=0x41443300)
07-29 18:43:25.419: E/AndroidRuntime(13404): FATAL EXCEPTION: main
07-29 18:43:25.419: E/AndroidRuntime(13404): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.liamwli.parent.trap/com.liamwli.parent.trap.ParentTrap}: java.lang.IllegalAccessException: access to class not allowed
07-29 18:43:25.419: E/AndroidRuntime(13404):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2096)
07-29 18:43:25.419: E/AndroidRuntime(13404):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2207)
07-29 18:43:25.419: E/AndroidRuntime(13404):    at android.app.ActivityThread.access$600(ActivityThread.java:139)
07-29 18:43:25.419: E/AndroidRuntime(13404):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
07-29 18:43:25.419: E/AndroidRuntime(13404):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-29 18:43:25.419: E/AndroidRuntime(13404):    at android.os.Looper.loop(Looper.java:137)
07-29 18:43:25.419: E/AndroidRuntime(13404):    at android.app.ActivityThread.main(ActivityThread.java:4896)
07-29 18:43:25.419: E/AndroidRuntime(13404):    at java.lang.reflect.Method.invokeNative(Native Method)
07-29 18:43:25.419: E/AndroidRuntime(13404):    at java.lang.reflect.Method.invoke(Method.java:511)
07-29 18:43:25.419: E/AndroidRuntime(13404):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:788)
07-29 18:43:25.419: E/AndroidRuntime(13404):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
07-29 18:43:25.419: E/AndroidRuntime(13404):    at dalvik.system.NativeStart.main(Native Method)
07-29 18:43:25.419: E/AndroidRuntime(13404): Caused by: java.lang.IllegalAccessException: access to class not allowed
07-29 18:43:25.419: E/AndroidRuntime(13404):    at java.lang.Class.newInstanceImpl(Native Method)
07-29 18:43:25.419: E/AndroidRuntime(13404):    at java.lang.Class.newInstance(Class.java:1319)
07-29 18:43:25.419: E/AndroidRuntime(13404):    at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
07-29 18:43:25.419: E/AndroidRuntime(13404):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2087)
07-29 18:43:25.419: E/AndroidRuntime(13404):    ... 11 more
07-29 18:43:27.214: I/Process(13404): Sending signal. PID: 13404 SIG: 9

最佳答案

看来您的应用程序假设ParentTrap是一个Activity(不是,Controller是Activity),您可以:


从ParentTrap的声明中删除public

class ParentTrap extends DeviceAdminReceiver {


并将Controller设为public类(即主类):

public class Controller extends Activity {

将ParentTrap类嵌套在Controller中:

public class Controller extends Activity {
    public class ParentTrap extends DeviceAdminReceiver {...}

    ...
}

将ParentTrap放入其自己的文件(ParentTrap.java)

09-04 22:49