Java.lang.NullPointerException in android.content.ContextWrapper.getPackageName ANDROID

Hi, 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); MainActivity.this.startActivity(myIntent); } 

Another class calls the awe () method, and I get an error and

 05-25 04:06:51.034: E/AndroidRuntime(7161): FATAL EXCEPTION: main 05-25 04:06:51.034: E/AndroidRuntime(7161): java.lang.NullPointerException 05-25 04:06:51.034: E/AndroidRuntime(7161): at android.content.ContextWrapper.getPackageName(ContextWrapper.java:151) 05-25 04:06:51.034: E/AndroidRuntime(7161): at android.content.ComponentName.<init>(ComponentName.java:106) 05-25 04:06:51.034: E/AndroidRuntime(7161): at android.content.Intent.<init>(Intent.java:2895) 05-25 04:06:51.034: E/AndroidRuntime(7161): at package name.MainActivity.awe(MainActivity.java:215) 

Does anyone know what I can do?

Mainactivity

 public class MainActivity extends Activity implements OnClickListener { // (variable stuff) protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); buttonE = (Button) findViewById(R.id.buttonEASY); buttonM = (Button) findViewById(R.id.buttonMED); // here I do all that button stuff for the layout } public void onClick(View arg0) { System.out.println("click"); if (arg0==buttonE) { int checkedRadioButton = radioGroup1.getCheckedRadioButtonId(); String radioButtonSelected = ""; switch (checkedRadioButton) { case R.id.radio0 : radioButtonSelected = "radiobutton1"; Toast.makeText(getApplicationContext(), "Easy, 10 selected", Toast.LENGTH_SHORT).show(); setContentView(R.layout.raten); // Button stuff, again. } public void awe() { Intent tutorial = new Intent(MainActivity.this, Awesome.class); if (tutorial != null) { startActivity(tutorial); } } 

Easy.java

Nothing important here, the place where I am in awe ():

 if (s==max+1){System.out.println("AWESOME!"); MainActivity mA = new MainActivity(); mA.awe();} 

Awesome.java

 public class Awesome extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.awesome); } 

Hopefully now I posted all that is important

+4
source share
4 answers

What to consider when working in Android:

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

Do you know your Context when using Intents ?

To call intents, always check for null if you call through package_name:

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

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

Android Intent requires "Context" and "Class".

Update: here is another post that will help:

Launch an app from another Android app

Hello,

+3
source

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

+4
source

I had the same error, and it took me a while to figure this out. As @csgero mentioned, my problem was that the activity I was trying to start was not initialized. This means that errors occur before the onCreate function is called. And it turned out that in the part of the codes an error occurred when I defined the variables in the current activity. Good luck

0
source

Probably the problem is that MainActivity is null. in my case, the action was destroyed when the abvoe code was executed. therefore, the internal context of the Activity is null.

0
source

Source: https://habr.com/ru/post/1482676/


All Articles