在显示java.lang.NullPointerException ANDROID android.content.ContextWrapper.getPackageName [英] java.lang.NullPointerException at android.content.ContextWrapper.getPackageName ANDROID
问题描述
嘿,我想从我的MainActivity但不是在onCreate方法开始一个活动。
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
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
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
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
推荐答案
需要考虑的事情与Android活动:
Things to consider with Android Activities:
你有你的类,延长活动
中定义的的AndroidManifest.xml
Do you have your classes that extend Activity
defined in the AndroidManifest.xml
?
您知道您的上下文使用时
意图
有关呼叫意图,总是检查空,如果你是通过软件包名美其名曰:
For calling intents, always check for null, if you are calling via packagename:
Intent mTutorial = new Intent(MainActivity.this, TutorialActivity.class);
this.startActivity(mTutorial);
您的问题无非是想打电话给你的敬畏()方法是在没有你的MainActivity正确的上下文中的另一个活动:的 http://developer.android.com/reference/android/content/Intent.html 。
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的意图需要一个背景和类。
Android Intent requires a "Context" and a "Class".
更新:这里是另一篇文章,这将有助于:
<一个href=\"http://stackoverflow.com/questions/3872063/android-launch-an-application-from-another-application/15465797#15465797\">Android从另一个应用程序启动应用程序
问候,
这篇关于在显示java.lang.NullPointerException ANDROID android.content.ContextWrapper.getPackageName的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!