Unfortunately, MyApp has Stopped In Emulator

て烟熏妆下的殇ゞ 提交于 2019-12-12 23:08:15

问题


I am very new to Android & now I am having weird problem in my Application. It doesn't give me any error in console. I am just trying to test different notification options of Android and for this reason here is my written code:

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;
    }

}

And Here is my Log:

    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

What could be the problem?


回答1:


There are two issues with your code.

1- Make the class public so that you can access it. 2- You forgot to add

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

That's why you are getting a nullPointer Now, you are good to go!




回答2:


Since the stack trace says:

java.lang.IllegalAccessException: access to class not allowed

It means that

class Home extends Activity {

should be

public class Home extends Activity {

Activities need to be public classes otherwise Android won't be able to access them.



来源:https://stackoverflow.com/questions/16133775/unfortunately-myapp-has-stopped-in-emulator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!