首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >安卓应用程序在onClickListener上崩溃

安卓应用程序在onClickListener上崩溃
EN

Stack Overflow用户
提问于 2018-07-10 06:07:52
回答 2查看 59关注 0票数 0

我有一个应用程序,这是一个简单的彩票系统,但每当我尝试按下其中一个按钮,应用程序冻结,然后崩溃,我不知道为什么。这个程序不会给我错误,所以它真的很令人困惑。我看到了一些关于布局的问题,但我不知道这是什么。我将在问题下的评论中回答页面布局。提前谢谢。

下面是页面脚本:

代码语言:javascript
复制
package com.example.myapplication;

import android.graphics.Color;
import android.icu.text.NumberFormat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.util.*;
import java.math.*;

import org.w3c.dom.Text;

import javax.xml.transform.Result;

public class Lottery extends AppCompatActivity {
    Button Check, Random;
    EditText User1;
    TextView Balance2, ResultText;
    int  Balance1 = 500;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lottery);
        Check = (Button)findViewById(R.id.CheckNumbers);
        Random = (Button)findViewById(R.id.RandomNumber);
        User1 = findViewById(R.id.PickNumber);
        Balance2 = (TextView)findViewById(R.id.Balance);
        ResultText = (TextView)findViewById(R.id.Result);
        ResultText.setVisibility(View.GONE);

       Balance2.setText("Balance: $500");

        Check.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int User=Integer.parseInt(String.valueOf(User1));
                if(User1.toString().trim().equals("")){
                    ResultText.setBackgroundColor(Color.RED);
                    ResultText.setText("Please Enter a Number");
                    ResultText.setVisibility(View.VISIBLE);

                }
                else if(User > 100 || User < 1){
                    ResultText.setBackgroundColor(Color.RED);
                    ResultText.setText("Please Enter a Number between 1 and 100");
                    ResultText.setVisibility(View.VISIBLE);
                }
                else {
                    int answer = (int)((Math.random()*100)+1);
                    int placeholder = answer % 10;
                    int AnswerTens = (answer - placeholder)/10;
                    int AnswerOnes = placeholder;
                    placeholder = User %10;
                    int UserTens = (User- placeholder)/10;
                    int UserOnes = placeholder;

                    if(User == answer){
                        ResultText.setBackgroundColor(Color.GREEN);
                        ResultText.setText("Correct!!! The Number was "+User+" $1000 has been added to your account");
                        ResultText.setVisibility(View.VISIBLE);
                        Balance1 += 1000;
                        Balance2.setText("Balance: $ "+  Balance1);
                    }
                    else if((UserTens == AnswerOnes)&&(UserOnes == AnswerTens)){
                        ResultText.setBackgroundColor(Color.GREEN);
                        ResultText.setText(" Somewhat Correct! The digit were correct but in the wrong order The answer was "+answer+" $500 has been added to your account");
                        ResultText.setVisibility(View.VISIBLE);
                        Balance1 += 500;
                        Balance2.setText("Balance: $ "+  Balance1);
                    }
                    else if((UserTens == AnswerTens)|| (UserTens == AnswerOnes)||(UserOnes == AnswerOnes)||(UserOnes==AnswerTens)){
                        ResultText.setBackgroundColor(Color.GREEN);
                        ResultText.setText("Kinda Correct! One digit was  correct The answer was "+answer+" $100 has been added to your account");
                        ResultText.setVisibility(View.VISIBLE);
                        Balance1 += 100;
                        Balance2.setText("Balance: $ "+  Balance1);
                    }
                    else{
                        ResultText.setBackgroundColor(Color.RED);
                        ResultText.setText("Incorrect the Number was "+answer);
                        ResultText.setVisibility(View.VISIBLE);
                    }


                }
            }
        });
        Random.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               int answer = (int)((Math.random()*100)+1);

                User1.setText(answer);
            }
        });
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-07-10 08:38:59

我看到了导致崩溃的几个可能原因。正如@Siros提到的,您需要在调用setText时使用String.valueOf(answer)

此外,要从EditText中获取字符串,必须调用User1.getText().toString()。调用User1.toString()将返回类似android.support.v7.widget.AppCompatEditText{cab8e45 VFED..CL. ......I. 0,0-0,0 #7f08003e app:id/editText}的内容,它肯定不能被解析为整数。

调用Integer.parseInt(String.valueOf(User1));总是会使用NumberFormatException崩溃(因为它试图解析的字符串与上面的字符串类似)。

当您调用Integer.parseInt(string);时,如果字符串不是有效的整数(例如,空字符串或0.1.2.3),它将抛出异常。您将看到如下所示的堆栈跟踪:

代码语言:javascript
复制
Process: com.project.testproject, PID: 30357
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.project.testproject/com.project.testproject.MainActivity}: java.lang.NumberFormatException: Invalid int: "android.support.v7.widget.AppCompatEditText{8c2c99a VFED..CL. ......I. 0,0-0,0 #7f08003e app:id/editText}"
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
    at android.app.ActivityThread.-wrap11(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
 Caused by: java.lang.NumberFormatException: Invalid int: "android.support.v7.widget.AppCompatEditText{8c2c99a VFED..CL. ......I. 0,0-0,0 #7f08003e app:id/editText}"
    at java.lang.Integer.invalidInt(Integer.java:138)
    at java.lang.Integer.parse(Integer.java:410)
    at java.lang.Integer.parseInt(Integer.java:367)
    at java.lang.Integer.parseInt(Integer.java:334)
    at com.project.testproject.MainActivity.onCreate(MainActivity.java:37)
    at android.app.Activity.performCreate(Activity.java:6237)

您可能想要使用下面这样的内容:

代码语言:javascript
复制
String UserString = User1.getText().toString();

if( UserString.trim().isEmpty() ) {
    //handle empty string
}
else {
    try {
       int User = Integer.parseInt(UserString);

       // Handle valid number cases
    }
    catch(NumberFormatException ne) {
      // handle invalid entry
    }
}
票数 0
EN

Stack Overflow用户

发布于 2018-07-10 06:30:35

更改此设置:

代码语言:javascript
复制
User1.setText(answer);

要这样做:

代码语言:javascript
复制
User1.setText(String.valueOf(answer));
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51254689

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档