嘿,我想从我的 MainActivity 启动一个 Activity,但不是在 oncreate 方法中。
public void awe()
{
Intent myIntent = new Intent(MainActivity.this, Awesome.class);
MainActivity.this.startActivity(myIntent);
}
另一个类调用方法 awe() ,我得到的是一个崩溃和
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)
有人知道我能做什么吗?
主要活动
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); }
}
易.java
这里没什么重要的,我提到 awe() 的地方:
if (s==max+1){System.out.println("AWESOME!"); MainActivity mA = new MainActivity(); mA.awe();}
真棒.java
public class Awesome extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.awesome);
}
我希望我现在发布了所有重要的内容