Skip to content Skip to sidebar Skip to footer

Java.lang.nullpointerexception At Android.content.contextwrapper.getpackagename Android

Hey I want to start an Activity from my MainActivity but not in the oncreate method. public void awe() { Intent myIntent = new Intent(MainActivity.this, Awesome.class); Mai

Solution 1:

The problem probably is that MainActivity has not been fully initialized yet when you are calling the awe() method, and the internal Context of the Activity is null.

Solution 2:

Things to consider with Android Activities:

Do you have your classes that extend Activity defined in the AndroidManifest.xml?

Are you aware of your Context when using Intents?

For calling intents, always check for null, if you are calling via packagename:

Intent mTutorial = new Intent(MainActivity.this, TutorialActivity.class); 

this.startActivity(mTutorial); 

Your problem was simply trying to call your "awe()" method was in another Activity that did not have the correct Context for your MainActivity: http://developer.android.com/reference/android/content/Intent.html.

Android Intent requires a "Context" and a "Class".

Update: Here is another post that will help:

Launch an application from another application on Android

Regards,

Solution 3:

I got the same error, which took me a while to figure it out. Like @csgero mentioned, my problem was that the activity I tried to start was not initialized. It means errors happen before onCreate is called. And it turned out that there was a error in the codes part where I defined the variables in the to-be-called activity. Good luck!

Solution 4:

The problem probably is that MainActivity is null. in my case, the activity destroyed when run abvoe code. so the internal Context of the Activity is null.

Post a Comment for "Java.lang.nullpointerexception At Android.content.contextwrapper.getpackagename Android"