Java Android Eclipse AVD“不幸的是,<项目名称>”已经停止了。项目名称>
在 eclipse/android AVD 中,我得到“不幸的是,已停止”
我已经检查了有关此问题的其他问题,但没有任何帮助。
我认为是我的代码有问题,因为当我注释掉代码时,它运行正常。然而,android 代码检查器没有显示任何错误。
这是我的代码:
public TextView TextView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setTextView();
}
public void setTextView(){
TextView tv = (TextView);
findViewById(R.id.textView1);
tv.setText("4");
}
我也尝试过这个,但同样的事情发生了,
public TextView TextView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = (TextView);
findViewById(R.id.textView1);
tv.setText("4");
}
我不确定我的代码的结构是否正确。
In the eclipse/android AVD, I get "Unfortunately, has stopped"
I have checked other Qs about this, but none helped.
I assume it is my code that is faulty, because when I comment the code out, it runs fine. However, the android code-checker thingy shows no errors.
This is my code:
public TextView TextView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setTextView();
}
public void setTextView(){
TextView tv = (TextView);
findViewById(R.id.textView1);
tv.setText("4");
}
I tried this aswell, but the same happened
public TextView TextView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = (TextView);
findViewById(R.id.textView1);
tv.setText("4");
}
I'm not sure whether the structure of my code is correct either.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使
tv
为null
,当您执行tv.set...
时,它会抛出NullPointerException
。尝试下面的代码。
Makes
tv
asnull
and when you dotv.set...
it throwsNullPointerException
.Try below code.
如果您指的是 Eclipse 在运行之前突出显示代码中的错误的方式,它不会捕获这样的错误。您的错误是运行时错误,这意味着您的代码完全能够编译。
ecplise 所做的检查只会捕获导致代码无法编译的错误(主要是拼写错误)。
要捕获运行时错误,您需要在 ecplise 中打开 Logcat 视图,并在应用程序运行/强制关闭时观察日志。
那里会有一个堆栈跟踪来解释发生了什么。
if you are referring to the way that eclipse highlights errors in your code before you run, it will not catch an error like this. Your error is a runtime error, which means that your code is perfectly capable of compiling.
the checking that ecplise does will only catch errors that cause your code to be unable to compile (largely typo errors).
To catch runtime errors you need to open the Logcat view in your ecplise and watch the logs as your application runs / force closes.
there will be a stack trace in there that explains what has happened.