Reputation: 1103
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
Upvotes: 4
Views: 10654
Reputation: 1759
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.
Upvotes: 0
Reputation: 550
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!
Upvotes: 0
Reputation: 55517
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,
Upvotes: 3
Reputation: 2773
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.
Upvotes: 5