Java Android错误:无法启动活动
以下是我用来启动活动的代码:Java Android错误:无法启动活动,java,android,eclipse,error-handling,nullpointerexception,Java,Android,Eclipse,Error Handling,Nullpointerexception,以下是我用来启动活动的代码: startActivity(new Intent(getApplicationContext(), Giveaway.class)); 以下是我提出的活动: public class Giveaway extends Activity implements OnClickListener{ static SharedPreferences settings; SharedPreferences.Editor prefEditor; @Override pro
startActivity(new Intent(getApplicationContext(), Giveaway.class));
以下是我提出的活动:
public class Giveaway extends Activity implements OnClickListener{
static SharedPreferences settings;
SharedPreferences.Editor prefEditor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.giveaway);
settings = getSharedPreferences("firsttime", 0);
LinearLayout facebook = (LinearLayout)findViewById(R.id.facebooklayout);
Button later = (Button)findViewById(R.id.later);
Button dontshowagain = (Button)findViewById(R.id.dontshowagain);
facebook.setOnClickListener(this);
later.setOnClickListener(this);
dontshowagain.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.facebooklayout:
Uri localuri = Uri.parse("http://www.facebook.com/pages/Bright-Design/366832480049386");
startActivity(new Intent("android.intent.action.VIEW", localuri));
break;
case R.id.later:
finish();
break;
case R.id.dontshowagain:
finish();
prefEditor = settings.edit();
prefEditor.putBoolean("showgiveaway", false);
prefEditor.commit();
break;
}
}
我已在我的清单文件夹中声明该活动:
<activity
android:name=".Giveaway"
android:label="@string/app_name"
android:theme="@android:style/Theme.Dialog"
android:screenOrientation="portrait"/>
您需要发布布局代码,但最可能发生的情况是这三行中的一行返回空值:
LinearLayout facebook = (LinearLayout)findViewById(R.id.facebooklayout);
Button later = (Button)findViewById(R.id.later);
Button dontshowagain = (Button)findViewById(R.id.dontshowagain);
在调试器步骤中,通过这些行,如果其中一行为null,则会出现问题,因为一旦尝试设置单击侦听器,它就会失败。您需要发布布局代码,但最有可能发生的情况是,这三行中的一行返回null值:
LinearLayout facebook = (LinearLayout)findViewById(R.id.facebooklayout);
Button later = (Button)findViewById(R.id.later);
Button dontshowagain = (Button)findViewById(R.id.dontshowagain);
Caused by: java.lang.NullPointerException
at com.brightdesign.blackops2.Giveaway.onCreate(Giveaway.java:29)
在调试器步骤中,通过这些行,如果其中一行为null,则会出现问题,因为只要您尝试设置单击侦听器,它就会失败
Caused by: java.lang.NullPointerException
at com.brightdesign.blackops2.Giveaway.onCreate(Giveaway.java:29)
告诉我们Giveway.onCreate()中存在NullPointerException,特别是Giveway.java第29行。可能性是facebook
,以后的
,和/或dontshow
真的是空的。您是否在giveaway.xml中定义了所有这三个
告诉我们Giveway.onCreate()中存在NullPointerException,特别是Giveway.java第29行。可能性是facebook
,以后的
,和/或dontshow
真的是空的。您是否在giveway.xml中定义了所有这三个选项?试试这个
1.
Intent i = new Intent(Your_Activity.this, Another_Activity.class);
startActivity(i);
Uri localuri = Uri.parse("http://www.facebook.com/pages/Bright-Design/366832480049386");
LinearLayout facebook = (LinearLayout)findViewById(R.id.facebooklayout);
Button later = (Button)findViewById(R.id.later);
Button dontshowagain = (Button)findViewById(R.id.dontshowagain);
2.下面的几行指向类和空行
类别:
com.brightdesign.blackops2/com.brightdesign.blackops2.giveway}:java.lang.NullPointerException
行:
请检查以下四行,我认为您在这里得到的是空值。
Intent i = new Intent(Your_Activity.this, Another_Activity.class);
startActivity(i);
Uri localuri = Uri.parse("http://www.facebook.com/pages/Bright-Design/366832480049386");
LinearLayout facebook = (LinearLayout)findViewById(R.id.facebooklayout);
Button later = (Button)findViewById(R.id.later);
Button dontshowagain = (Button)findViewById(R.id.dontshowagain);
试试这个
1.
Intent i = new Intent(Your_Activity.this, Another_Activity.class);
startActivity(i);
Uri localuri = Uri.parse("http://www.facebook.com/pages/Bright-Design/366832480049386");
LinearLayout facebook = (LinearLayout)findViewById(R.id.facebooklayout);
Button later = (Button)findViewById(R.id.later);
Button dontshowagain = (Button)findViewById(R.id.dontshowagain);
2.下面的几行指向类和空行
类别:
com.brightdesign.blackops2/com.brightdesign.blackops2.giveway}:java.lang.NullPointerException
行:
请检查以下四行,我认为您在这里得到的是空值。
Intent i = new Intent(Your_Activity.this, Another_Activity.class);
startActivity(i);
Uri localuri = Uri.parse("http://www.facebook.com/pages/Bright-Design/366832480049386");
LinearLayout facebook = (LinearLayout)findViewById(R.id.facebooklayout);
Button later = (Button)findViewById(R.id.later);
Button dontshowagain = (Button)findViewById(R.id.dontshowagain);
GiveAway类的第29行是什么?我猜GetSharedReferences正在变为null。@PadmaKumar文档的内容是“如果不存在同名的首选项文件,则在检索编辑器(SharedReferences.edit())然后提交更改(editor.commit())时将创建该文件。”所以这不是一个错误的猜测,但我看不出它在这里是如何调用为null的。GiveAway类的第29行是什么?我猜GetSharedReferences正在变为null。@PadmaKumar阅读文档“如果不存在同名的首选项文件,则在检索编辑器(SharedReferences.edit())并提交更改时将创建该文件(Editor.commit())”所以这是一个不错的猜测,但我不知道它在这里如何调用为null。谢谢。我将它命名为facebookLayout而不是facebookLayout。谢谢。我将它命名为facebookLayout而不是facebookLayout。