Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.NullPointerException at android.content.ContextWrapper.getPackageName ANDROID

Tags:

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

Another class calls the method awe() and what I get is a crash 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)

Someone knows 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 refer to 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);
    }

I hope I now posted everything that is important

like image 328
user2330482 Avatar asked Feb 17 '23 10:02

user2330482


1 Answers

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.

like image 155
csgero Avatar answered May 02 '23 09:05

csgero