简体   繁体   English

按下按钮时,应用程序崩溃

[英]When the button is pressed, the app crashes

I've been having problems with Android buttons. 我一直遇到Android按钮问题。 I try to set an onClick listener, but it fails, crashes and doesn't print any helpeul error messages. 我尝试设置一个onClick监听器,但它失败,崩溃并且不打印任何helpeul错误消息。 Here is my code: 这是我的代码:

Button button;
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        setContentView(R.layout.choose_level);

    }
});

I've tried putting in a try catch statement so it won't display annoying errors but the button still doesn't work. 我已尝试输入try catch语句,因此它不会显示恼人的错误,但按钮仍然无法正常工作。 Would it be because the layout hasn't been loaded? 是不是因为布局还没有加载? or is it something else? 或者是别的什么?

Thanks in advance. 提前致谢。

you must call setContentView(R.layout.XML_LAYOUT); 你必须调用setContentView(R.layout.XML_LAYOUT); method before you call findViewById for your button. 在为按钮调用findViewById之前的方法。

here XML_LAYOUT must be the Layout containing your Button ID. 这里XML_LAYOUT必须是包含您的Button ID的Layout。

Note :- it is not recommanded to call setContentView method multiple times. 注意 : - 不建议多次调用setContentView方法。 if you want to show a different layout/screen add it into Another activity and start that activity on button click. 如果要显示不同的布局/屏幕,请将其添加到另一个活动中,并在按钮单击时启动该活动。

you are calling setContentView(R.Layout.XML_LAYOUT) in your button onClick listener where as it should be above in oncreate method 你在你的按钮onClick监听器中调用setContentView(R.Layout.XML_LAYOUT),在oncreate方法中它应该在上面

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    /
    Button play = (Button)findViewById(R.id.play);
    play.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            show ur text here

        }
    });

I guess what ur trying to do is to set view for an XML file which is some layout file i guess check out inflator and intent 我想你想要做的是设置一个XML文件的视图,这是一些布局文件,我想检查充气机和意图

I put that in a try, catch statement so it won't put annoying errors... 我把它放在try,catch语句中,这样就不会出现烦人的错误......

A catch block will not magically stop your error from occurring - you cannot use it to stop the application "putting annoying errors". 一个catch块不会神奇地阻止你的错误发生 - 你不能用它来阻止应用程序“放置恼人的错误”。

You use them to handle errors when it's possible to recover from those cases (eg wait and retry, fall back to a slower alternative, etc.) 当有可能从这些情况中恢复时,您可以使用它们来处理错误(例如,等待和重试,回退到较慢的替代方案等)

What is the implementation of your catch block? catch块的实现是什么? If you're simply swallowing the error, your app will still fail - only you won't have any diagnostic information with which to deal with it. 如果您只是吞下错误,您的应用仍然会失败 - 只有您不会有任何诊断信息来处理它。

You'll need to go back to your original "annoying error", work out why it was happening and then fix it rather than just suppressing its output. 你需要回到原来的“恼人的错误”,找出它发生的原因,然后解决它,而不是仅仅抑制它的输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM