4

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

15
  • 1
    Are both those classes in your AndroidManifest.xml? Always check for null. Commented May 25, 2013 at 2:21
  • Yeah, both are mentioned in the XML Commented May 25, 2013 at 2:24
  • Intent tutorial = new Intent(MainActivity.this, TutorialActivity.class); if (tutorial != null) { startActivity(tutorial); } Are you in your MainActivity? Commented May 25, 2013 at 2:29
  • Yeah, the awe() is in the MainActivity, your code didn't work :( Commented May 25, 2013 at 2:31
  • 1
    Thanks for your answer, I will ask in another forum because I know that there will be thousands of answers where people say I should use google (...) Commented May 25, 2013 at 3:45

4 Answers 4

6

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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,

7 Comments

I agree to your post, but I have to say that Easy extends Activity
I don't see how checking for null would make sense here. You check for null in the line right next to the one that initialized the variable with a new Intent instance. tutorial could only be null if the Intent constructor threw an exception, in which case the execution would never get to the null check anyway.
Why was this answer downvoted? This works and I have tested this many times. You always check for null when initiated an object, especially when starting an activity. What if the second class is not an activity?
If the second parameter is not an activity, then the Intent constructor will throw an exception, in which case the following lines (including the null check)will not be executed. If you look at the call stack in the question you'll see that the exception is coming from the constructor of Intent, so putting a null check after this line will not help. The code you wrote will work exactly in the same cases as the same code without the null check would.
Ok, let's try this once more. In Java, the new operator will never under any circumstances return null (see stackoverflow.com/questions/11103444/… if you do not believe me). Thus checking the result of a new operator for null does not make sense, as the result will always be false. This has nothing to do with intents or android. And no, you do not "only Intent to Activities in Android". Intents can also be sent to services and broadcast listeners.
|
0

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!

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.