android 进程在 adb 上使用简单的应用程序崩溃,不知道我做错了什么
package com.russell.saw;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class learnandroid extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button landroid_button = (Button) findViewById(R.id.landroid_button); {
landroid_button.setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
setContentView(R.layout.button);
}
});
}
Button back_button = (Button) findViewById(R.id.back_button); {
back_button.setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
setContentView(R.layout.main);
}
});
}
}
}
我不确定出了什么问题,这只是一个简单的学习测试应用程序,有两个按钮,从一个页面转到另一个页面,但是当我在手机上运行它时,我就崩溃了。
package com.russell.saw;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class learnandroid extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button landroid_button = (Button) findViewById(R.id.landroid_button); {
landroid_button.setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
setContentView(R.layout.button);
}
});
}
Button back_button = (Button) findViewById(R.id.back_button); {
back_button.setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
setContentView(R.layout.main);
}
});
}
}
}
i'm unsure of what is going wrong, it's just a simple learning tester app, with two buttons, going from one page to another, but i get a crash as soon as i run it on the phone.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
哇哦!您的 onClickListeners 设置完全错误。您正在 onClickListeners 中调用
setContentView
。相反,您需要使用意图从一个活动转到下一个活动。它需要如下所示:另外,不要使用
landroid_button
来引用您的按钮:这只是资源的 XML ID。相反,您需要通过执行以下操作来抓住按钮:然后,当您设置 onClickListener 时,使用该变量:
myLandroidButton
,如myLandroidButton.setOnClickListener
等。 。Woah! You have your onClickListeners set up all wrong. You are calling
setContentView
in the onClickListeners. Instead, you need to use an intent to go from one activity to the next. It needs to look like this:Also, don't use
landroid_button
to reference your button: that's just the XML ID of the resource. Instead you need to grab hold of your button by doing something like this:Then when you set up the onClickListener, use that variable:
myLandroidButton
likemyLandroidButton.setOnClickListener
and so on..如果您尚未将活动标记添加到 AndroidManifest.xml,则需要这样做:
您需要为每个活动(在应用程序标记下方)执行此操作。
If you haven't added an activity tag to AndroidManifest.xml, you will need to do so:
You need to do this for each activity (underneath the application tag).
尽管我怀疑这是导致崩溃的原因,但您的代码存在严重问题。当您在侦听器中的
onClick
内部调用setContentView
时,按钮 landroid_button 和 back_button 不再有效。也就是说,它们是不再绑定到窗口的对象。 (如果新的内容视图具有“相同”按钮,则它们不再具有侦听器。)Although I doubt that this is causing the crash, your code has a serious problem. When you call
setContentView
insideonClick
in your listeners, the buttons landroid_button and back_button are no longer valid. That is, they are objects that are no longer tied to a window. (If the new content views have "the same" buttons, they no longer have listeners.)