不幸的是,MyApp已停止在模拟器中

时间:2013-04-21 17:00:25

标签: android android-emulator illegalaccessexception

我是Android和Android的新手现在我在我的应用程序中遇到了奇怪的问题。它在控制台中没有给我任何错误。我只是想测试Android的不同通知选项,因此这里是我编写的代码:

class Home extends Activity {

    Button notification;
    Button progress;
    private int ID=1;
    ProgressDialog pd;
    ProgressTask pt = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        notification=(Button)this.findViewById(R.id.start_notification);
        notification.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                StartNotification();
            }});
        Button background = (Button)this.findViewById(R.id.change_background);
        background.setOnClickListener(new OnClickListener() {    
            public void onClick(View v) { 
                showDialog(0);

            }});
        progress.setOnClickListener(new OnClickListener() {    
            public void onClick(View v) { 
                showDialog(1);

            }});
    }
    @Override
    protected Dialog onCreateDialog(int id)
    {
        final CharSequence[] items = {"Black","Red", "Green", "Blue", "Gray"};
        final String[] color = {"#202020","#D11919", "#339966", "#006699", "#818181"};
        switch(id) {
        case 0:
            return new AlertDialog.Builder(this)

            .setTitle("Set Background Color")
            .setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int item) {
                    View mainLayout = findViewById(R.id.activity_home);
                    View root = mainLayout.getRootView();
                    new Color();
                    root.setBackgroundColor(Color.parseColor(color[item]));
                    dismissDialog(0);
                }
            }).create();

        case 1:
            pd = new ProgressDialog(Home.this);
            pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            pd.setMessage("Downloading...");
            return pd;


        }
        return null;
    }

    @Override
    protected void onPrepareDialog(int id, Dialog dialog) {     
        switch(id) {
        case 1:
            pd.setProgress(0);
            pt = new ProgressTask(handler);
            pt.start();            
        }

    }

    final Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            int count = msg.arg1;
            pd.setProgress(count);
            if (count >= 100){
                dismissDialog(1);
                pt.setStatus(false);
            }
        }
    };
    private class ProgressTask extends Thread {
        Handler myHandler;
        int count = 0;
        boolean status = true;

        ProgressTask(Handler _handler) {
            myHandler = _handler;
        }

        public void run() {                      

            while (status) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Message msg = myHandler.obtainMessage();
                msg.arg1 = count;
                myHandler.sendMessage(msg);
                count++;
            }
        }

        public void setStatus(boolean m){
            status = m;
        }
    }

    private void StartNotification()
    {
        Intent intent= new Intent(Home.this, NotificationView.class);
        intent.putExtra("ID", ID);
        PendingIntent pendingIntent=PendingIntent.getActivity(Home.this, 0, intent, 0);
        NotificationManager nm=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Notification notif = new Notification(
                R.drawable.ball,"Reminder: Game starts in 15 minutes",
                System.currentTimeMillis());
        CharSequence from = "Sports Center";
        CharSequence message = "Game: L.A Lakers vs Miami Heat";
        notif.setLatestEventInfo(this, from, message, pendingIntent);

        notif.vibrate = new long[] { 100, 250, 100, 500};

        nm.notify(ID, notif);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_home, menu);
        return true;
    }

}

这是我的日志:

    04-21 16:55:03.829: I/dalvikvm(907): threadid=3: reacting to signal 3
04-21 16:55:03.966: I/dalvikvm(907): Wrote stack traces to '/data/anr/traces.txt'
04-21 16:55:04.265: I/dalvikvm(907): threadid=3: reacting to signal 3
04-21 16:55:04.545: D/dalvikvm(907): newInstance failed: Lcom/example/statusbar/Home; not accessible to Landroid/app/Instrumentation;
04-21 16:55:04.545: I/dalvikvm(907): Wrote stack traces to '/data/anr/traces.txt'
04-21 16:55:04.555: D/AndroidRuntime(907): Shutting down VM
04-21 16:55:04.555: W/dalvikvm(907): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
04-21 16:55:04.595: E/AndroidRuntime(907): FATAL EXCEPTION: main
04-21 16:55:04.595: E/AndroidRuntime(907): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.statusbar/com.example.statusbar.Home}: java.lang.IllegalAccessException: access to class not allowed
04-21 16:55:04.595: E/AndroidRuntime(907):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1880)
04-21 16:55:04.595: E/AndroidRuntime(907):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
04-21 16:55:04.595: E/AndroidRuntime(907):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
04-21 16:55:04.595: E/AndroidRuntime(907):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
04-21 16:55:04.595: E/AndroidRuntime(907):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-21 16:55:04.595: E/AndroidRuntime(907):  at android.os.Looper.loop(Looper.java:137)
04-21 16:55:04.595: E/AndroidRuntime(907):  at android.app.ActivityThread.main(ActivityThread.java:4424)
04-21 16:55:04.595: E/AndroidRuntime(907):  at java.lang.reflect.Method.invokeNative(Native Method)
04-21 16:55:04.595: E/AndroidRuntime(907):  at java.lang.reflect.Method.invoke(Method.java:511)
04-21 16:55:04.595: E/AndroidRuntime(907):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-21 16:55:04.595: E/AndroidRuntime(907):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-21 16:55:04.595: E/AndroidRuntime(907):  at dalvik.system.NativeStart.main(Native Method)
04-21 16:55:04.595: E/AndroidRuntime(907): Caused by: java.lang.IllegalAccessException: access to class not allowed
04-21 16:55:04.595: E/AndroidRuntime(907):  at java.lang.Class.newInstanceImpl(Native Method)
04-21 16:55:04.595: E/AndroidRuntime(907):  at java.lang.Class.newInstance(Class.java:1319)
04-21 16:55:04.595: E/AndroidRuntime(907):  at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
04-21 16:55:04.595: E/AndroidRuntime(907):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1871)
04-21 16:55:04.595: E/AndroidRuntime(907):  ... 11 more
04-21 16:55:04.775: I/dalvikvm(907): threadid=3: reacting to signal 3
04-21 16:55:04.826: I/dalvikvm(907): Wrote stack traces to '/data/anr/traces.txt'
04-21 16:55:05.155: I/dalvikvm(907): threadid=3: reacting to signal 3
04-21 16:55:05.175: I/dalvikvm(907): Wrote stack traces to '/data/anr/traces.txt'
04-21 16:55:07.255: I/Process(907): Sending signal. PID: 907 SIG: 9

可能是什么问题?

2 个答案:

答案 0 :(得分:5)

由于堆栈跟踪显示:

java.lang.IllegalAccessException: access to class not allowed

这意味着

class Home extends Activity {

应该是

public class Home extends Activity {

活动必须是public个类,否则Android将无法访问它们。

答案 1 :(得分:0)

您的代码存在两个问题。

1-制作课程public,以便您可以访问它。 2-你忘了添加

progress = (Button)this.findViewById(R.id.progress);

这就是你得到 nullPointer 的原因 现在,你很高兴去!