Run Time error while performing Toast Application in Android -
11-01 21:11:03.260 8051-8051/com.example.tejeswar.project0 e/androidruntime: fatal exception: main 11-01 21:11:03.260 8051-8051/com.example.tejeswar.project0 e/androidruntime: process: com.example.tejeswar.project0, pid: 8051 11-01 21:11:03.260 8051-8051/com.example.tejeswar.project0 e/androidruntime: java.lang.runtimeexception: unable instantiate activity componentinfo{com.example.tejeswar.project0/com.example.tejeswar.project0.mainactivity}: java.lang.nullpointerexception: attempt invoke virtual method 'android.content.context android.content.context.getapplicationcontext()' on null object reference 11-01 21:11:03.260 8051-8051/com.example.tejeswar.project0 e/androidruntime: @ android.app.activitythread.performlaunchactivity(activitythread.java:2225) 11-01 21:11:03.260 8051-8051/com.example.tejeswar.project0 e/androidruntime: @ android.app.activitythread.handlelaunchactivity(activitythread.java:2388) 11-01 21:11:03.260 8051-8051/com.example.tejeswar.project0 e/androidruntime: @ android.app.activitythread.access$800(activitythread.java:148) 11-01 21:11:03.260 8051-8051/com.example.tejeswar.project0 e/androidruntime: @ android.app.activitythread$h.handlemessage(activitythread.java:1292) 11-01 21:11:03.260 8051-8051/com.example.tejeswar.project0 e/androidruntime: @ android.os.handler.dispatchmessage(handler.java:102) 11-01 21:11:03.260 8051-8051/com.example.tejeswar.project0 e/androidruntime: @ android.os.looper.loop(looper.java:135) 11-01 21:11:03.260 8051-8051/com.example.tejeswar.project0 e/androidruntime: @ android.app.activitythread.main(activitythread.java:5312) 11-01 21:11:03.260 8051-8051/com.example.tejeswar.project0 e/androidruntime: @ java.lang.reflect.method.invoke(native method) 11-01 21:11:03.260 8051-8051/com.example.tejeswar.project0 e/androidruntime: @ java.lang.reflect.method.invoke(method.java:372) 11-01 21:11:03.260 8051-8051/com.example.tejeswar.project0 e/androidruntime: @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:901) 11-01 21:11:03.260 8051-8051/com.example.tejeswar.project0 e/androidruntime: @ com.android.internal.os.zygoteinit.main(zygoteinit.java:696) 11-01 21:11:03.260 8051-8051/com.example.tejeswar.project0 e/androidruntime: caused by: java.lang.nullpointerexception: attempt invoke virtual method 'android.content.context android.content.context.getapplicationcontext()' on null object reference 11-01 21:11:03.260 8051-8051/com.example.tejeswar.project0 e/androidruntime: @ android.content.contextwrapper.getapplicationcontext(contextwrapper.java:105) 11-01 21:11:03.260 8051-8051/com.example.tejeswar.project0 e/androidruntime: @ com.example.tejeswar.project0.mainactivity.(mainactivity.java:19) 11-01 21:11:03.260 8051-8051/com.example.tejeswar.project0 e/androidruntime: @ java.lang.reflect.constructor.newinstance(native method) 11-01 21:11:03.260 8051-8051/com.example.tejeswar.project0 e/androidruntime: @ java.lang.class.newinstance(class.java:1572) 11-01 21:11:03.260 8051-8051/com.example.tejeswar.project0 e/androidruntime: @ android.app.instrumentation.newactivity(instrumentation.java:1088) 11-01 21:11:03.260 8051-8051/com.example.tejeswar.project0 e/androidruntime: @ android.app.activitythread.performlaunchactivity(activitythread.java:2215) 11-01 21:11:03.260 8051-8051/com.example.tejeswar.project0 e/androidruntime: @ android.app.activitythread.handlelaunchactivity(activitythread.java:2388) 11-01 21:11:03.260 8051-8051/com.example.tejeswar.project0 e/androidruntime: @ android.app.activitythread.access$800(activitythread.java:148) 11-01 21:11:03.260 8051-8051/com.example.tejeswar.project0 e/androidruntime: @ android.app.activitythread$h.handlemessage(activitythread.java:1292) 11-01 21:11:03.260 8051-8051/com.example.tejeswar.project0 e/androidruntime: @ android.os.handler.dispatchmessage(handler.java:102) 11-01 21:11:03.260 8051-8051/com.example.tejeswar.project0 e/androidruntime: @ android.os.looper.loop(looper.java:135) 11-01 21:11:03.260 8051-8051/com.example.tejeswar.project0 e/androidruntime: @ android.app.activitythread.main(activitythread.java:5312) 11-01 21:11:03.260 8051-8051/com.example.tejeswar.project0 e/androidruntime: @ java.lang.reflect.method.invoke(native method) 11-01 21:11:03.260 8051-8051/com.example.tejeswar.project0 e/androidruntime: @ java.lang.reflect.method.invoke(method.java:372) 11-01 21:11:03.260 8051-8051/com.example.tejeswar.project0 e/androidruntime: @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:901) 11-01 21:11:03.260 8051-8051/com.example.tejeswar.project0 e/androidruntime: @ com.android.internal.os.zygoteinit.main(zygoteinit.java:696)
you're calling getapplicationcontext()
in activity object construction phase <init>
i.e. constructor or member variable initialization.
move code needs valid context
oncreate()
or later in activity lifecycle.
Comments
Post a Comment