Android的初学者的onClick崩溃 [英] Android beginner onClick crash

查看:142
本文介绍了Android的初学者的onClick崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编程的Andr​​oid是第一次,我遇到了麻烦。这样做是为了使在其​​中用户需要在他/她的头编号,而该应用程序试图猜测它一个猜谜游戏应用程序。用户将给出提示像更高和更低的应用程序。出于某种原因,应用程序崩溃后,我preSS启动按钮。正因为如此,我知道有一个在的onClick 方法的错误,但由于它后,我立即关闭preSS启动按钮,我不能用的东西就像一个的println 调试。

所以,其实我有2个问题:

  1. 在哪里我的推理失败? (或告诉我如何找出我的错误)
  2. 如何调试这样的事情?

开始,较高和较低的是在程序中的所有按钮。

  @覆盖
    公共无效的onClick(查看为arg0){
        INT分钟= 0;
        INT最大= 100;
        无规无规=新的随机(100);
        INT答案= 0;

        如果(将arg0 ==开始){
            回答= random.nextInt(100);
            buttonTextView.setText(答案);
        }
        否则,如果(为arg0 ==高){
            分=答案;
            答案= random.nextInt((最大值 - 最小值)+分);
            buttonTextView.setText(答案);
        }
        否则,如果(为arg0 ==低){
            最大=回答;
            答案= random.nextInt((MAX-1) - 分钟);
            buttonTextView.setText(答案);
        }

    }
 

解决方案
  
      
  1. 在哪里我的推理失败?
  2.   

您使用了错误的的setText()方法。 在TextView的文档你会看到有一个它接受一个 INT ,这是检索字符串的资源,你有你的的strings.xml 所以你传递一个资源ID 。所以,你的的setText()正在寻找资源 ID 答案变量。你会希望将其转换为一个字符串的东西,如

  buttonTextView.setText(将String.valueOf(答案));
 

或多种不同的方式之一。

  
      
  1. 如何调试这样的事情?
  2.   

当你的应用程序崩溃,就会出现在你的logcat中的异常。 这个答案可以帮助你读你的logcat。要在Eclipse中打开你的logcat窗口,如果没有准备好,你可以做到这一点。

  

窗口 - >显示视图 - >其他 - > Android的 - > LogCat中

一对夫妇一边笔记

您应该改变你的 PARAMS 的onClick()来点有意义的事情,所以我会改变

 公共无效的onClick(查看arg0中)
 

要像

 公共无效的onClick(视图v)// v表示视图,也可以认为,BTN
                       //无论是有道理的,你和其他人谁可以读取它
 

您也应该比较 ID 查看的点击,而不是查看本身。所以,你会用什么样的比较如下(假设你改变为arg0 v

 如果(v.getId()== R.id.start)//假设开始在你的巴顿XML的ID
                                 //这也可以让你使用switch语句
 

您在变量的onClick()最高答案)应外初始化的onClick()否则将被重置为默认值每次点击我敢pretty的肯定不希望(感谢323go指出了这一点)。

I'm programming Android for the first time and I'm having some difficulties. The idea is to make a guessing game app in which the user takes a number in his/her head and the app tries to guess it. The user will give hints like higher and lower to the app. For some reason the app crashes after I press the start button. Because of this, I know that there is an error in the onClick method but since it shuts down immediately after I press the start button, I can't use something like a println to debug.

So actually I have 2 questions:

  1. Where does my reasoning fail? (or show me how to figure out my mistakes)
  2. How can I debug things like this?

The start, higher and lower are all buttons in the program.

@Override
    public void onClick(View arg0) {
        int min = 0;
        int max = 100;
        Random random = new Random(100);
        int answer = 0;

        if (arg0 == start) {
            answer = random.nextInt(100);
            buttonTextView.setText(answer);
        }
        else if (arg0 == higher){
            min = answer;
            answer = random.nextInt((max - min) + min);
            buttonTextView.setText(answer);
        }
        else if (arg0 == lower) {
            max = answer;
            answer = random.nextInt((max-1) - min);
            buttonTextView.setText(answer);
        }

    }

解决方案

  1. where does my reasoning fail?

You are using the wrong setText() method. In the TextView Docs you will see that there is one which takes an int, this is for retrieving a String resource that you have in your strings.xml so you would pass it a resource id. So your setText() is looking for a resource with the id of whatever your answer variable is. You will want to convert this to a String with something like

buttonTextView.setText(String.valueof(answer));

or one of several different ways.

  1. How can I debug things like this?

When your app crashes there will be an exception in your logcat. This answer can help you to read your logcat. To open your logcat window in Eclipse, if it isn't already, you can do this

Window --> Show View --> Other --> Android --> LogCat

A couple side notes

You should change your params like in onClick() to something meaningful so I would change

public void onClick(View arg0)

to something like

public void onClick(View v)  // v for view, could also be view, btn
                       //  whatever makes sense to you and others who may read it

You also should compare the id of your View clicked instead of the View itself. So you would compare it with something like the following (assuming you changed arg0 to v)

if (v.getId() == R.id.start)  // Assuming start is the id in your xml of your Button
                                 // this will also allow you to use a switch statement

Your variables in onClick() (min, max, and answer) should be initialized outside of onClick() or they will be reset to the default values with each click which I'm pretty sure you don't want (thanks to 323go for pointing that out).

这篇关于Android的初学者的onClick崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆