Question

I try to start a new activity then I get error "Unfortunally app....." from the emulator.

03-24 12:41:53.242: E/AndroidRuntime(1245): FATAL EXCEPTION: main
03-24 12:41:53.242: E/AndroidRuntime(1245): java.lang.NullPointerException
03-24 12:41:53.242: E/AndroidRuntime(1245):     at android.content.ComponentName.<init>(ComponentName.java:75)
03-24 12:41:53.242: E/AndroidRuntime(1245):     at android.content.Intent.<init>(Intent.java:3662)
03-24 12:41:53.242: E/AndroidRuntime(1245):     at de.basti12354.tage.uebungen.Tag1$1$1.run(Tag1.java:115)
03-24 12:41:53.242: E/AndroidRuntime(1245):     at android.os.Handler.handleCallback(Handler.java:730)
03-24 12:41:53.242: E/AndroidRuntime(1245):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-24 12:41:53.242: E/AndroidRuntime(1245):     at android.os.Looper.loop(Looper.java:137)
03-24 12:41:53.242: E/AndroidRuntime(1245):     at android.app.ActivityThread.main(ActivityThread.java:5103)
03-24 12:41:53.242: E/AndroidRuntime(1245):     at java.lang.reflect.Method.invokeNative(Native Method)
03-24 12:41:53.242: E/AndroidRuntime(1245):     at java.lang.reflect.Method.invoke(Method.java:525)
03-24 12:41:53.242: E/AndroidRuntime(1245):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
03-24 12:41:53.242: E/AndroidRuntime(1245):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-24 12:41:53.242: E/AndroidRuntime(1245):     at dalvik.system.NativeStart.main(Native Method)

I have add the new activity to the ManifestLayout.

That's my code to start the new activity called Uebung2.class.

    @Override
        public void run() {
            while (Running){
                try {
                    Thread.sleep(1000);}

                catch (InterruptedException e) {
                    e.printStackTrace();
                    // TODO: handle exception
                }
                handler.post(new Runnable() {
                    @Override
                    public void run(){
                        number+=1;
                        textfield.setText(String.valueOf(number));
                        if (number>5) {
                            Intent intent = new Intent(null, Uebung2.class);
                            startActivity(intent);

                        }

                    }
                } );
            }

        }
    };
    new Thread(runnable).start();
Was it helpful?

Solution

Intent intent = new Intent(null, Uebung2.class);

You must supply Intent constructor a valid Context instead of a null.

What to use depends on the context (sic) where your code is. Use ActivityName.this to refer to the outer activity class in case the nested inner class is in fact in an activity.

Also, consider using just handler.postDelayed() for producing a delay instead of Thread.sleep() in a background thread.

OTHER TIPS

Your context is null

Intent intent = new Intent(null, Uebung2.class);

Use

Intent intent = new Intent(ActivityName.this, Uebung2.class);

Also i don't see any network or background computation done. So using just the handler is a better choice if you want a dealy.

You should pass the context in the activity like this.

 Intent intent = new Intent(ActivityName.this, Uebung2.class);
 (or)
 Intent intent = new Intent(getApplicationContext(), Uebung2.class);

you can't pass context as a null. So need to change from

Intent intent = new Intent(null, Uebung2.class);

to

Intent intent = new Intent(YourActivityName.this, Uebung2.class);

Replace this line

Intent intent = new Intent(null, Uebung2.class);

with this:

Intent intent = new Intent(ActivityName.this, Uebung2.class);

(or)

Intent intent = new Intent(getApplicationContext(), Uebung2.class);

And don't forget to mention activities in Androidmanifest.xml

Intent intent = new Intent(this, Uebung2.class);

or

Intent intent = new Intent(Youactivityname.this, Uebung2.class);

//Youactivityname mean the activity you was in like Mainactivity

Create a context of your class intially.

Context context = Activityname.this;

Then change your Intent as follows

Intent intent = new Intent(context, Uebung2.class);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top