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

user2330482
  • 1,053
  • 2
  • 11
  • 19
  • 1
    Are both those classes in your AndroidManifest.xml? Always check for null. – Jared Burrows May 25 '13 at 02:21
  • Yeah, both are mentioned in the XML – user2330482 May 25 '13 at 02:24
  • Intent tutorial = new Intent(MainActivity.this, TutorialActivity.class); if (tutorial != null) { startActivity(tutorial); } Are you in your MainActivity? – Jared Burrows May 25 '13 at 02:29
  • Yeah, the awe() is in the MainActivity, your code didn't work :( – user2330482 May 25 '13 at 02:31
  • It works, I use it in all my Android Applications. Can you post your AndroidManifest.xml as well as your activity? – Jared Burrows May 25 '13 at 02:32
  • Yeah I believe you, that it works for you. I have to shorten all that very hardly, because I got my app pretty far, give me 2 minutes – user2330482 May 25 '13 at 02:37
  • done, I hope that says you something – user2330482 May 25 '13 at 02:49
  • "Nothing important here". Looks to be your problem. What is in Easy.java? You can only make you are trying to intent an activity to another activity. You need to call "awe" in MainActivity, you context is wrong. It's hard to explain in the comments. – Jared Burrows May 25 '13 at 03:00
  • Does that mean, I would somehow have to tell MainActivity that it should call awe() to start the activity Awesome? – user2330482 May 25 '13 at 03:04
  • No, don't use Easy.java. You can call awe() within your MainActivity to launch the other Activity. That is the problem. – Jared Burrows May 25 '13 at 03:06
  • Thanks, you were so right, it really works if I do it from MainActivity. Make an answer of it and I'll tick it if you want to. But can you tell me what I can do to call awe from another class? :/ – user2330482 May 25 '13 at 03:11
  • What is the point of calling "awe" from another activity? "awe" is meant to be from MainActivity to Awesome activity. – Jared Burrows May 25 '13 at 03:26
  • I don't know how to change layout in another way than by accessing the UI thread.. – user2330482 May 25 '13 at 03:29
  • That sounds like another question? We are adding a lot of comments here. Can you make another or go to a discussion? – Jared Burrows May 25 '13 at 03:37
  • 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 (...) – user2330482 May 25 '13 at 03:45

4 Answers4

5

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.

csgero
  • 2,753
  • 17
  • 15
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,

Community
  • 1
  • 1
Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
  • I agree to your post, but I have to say that Easy extends Activity – user2330482 May 25 '13 at 03:30
  • 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. – csgero Oct 23 '13 at 13:19
  • 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? – Jared Burrows Oct 24 '13 at 14:44
  • 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. – csgero Jan 09 '14 at 15:46
  • @csgero You only Intent to Activities in Android. What you just said makes no sense, this is why I got the correct answer. – Jared Burrows Jan 09 '14 at 16:11
  • 1
    Ok, let's try this once more. In Java, the new operator will never under any circumstances return null (see http://stackoverflow.com/questions/11103444/java-can-creating-an-object-return-a-null-reference 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. – csgero Feb 03 '14 at 16:57
  • The answered helped the original poster and I got the credit. – Jared Burrows Feb 03 '14 at 20:17
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!

Sam003
  • 540
  • 6
  • 17
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.

David Guo
  • 1,749
  • 3
  • 20
  • 30