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

Android 安卓应用程序在onClickListener上崩溃,android,Android,我有这个应用程序,它是一个简单的彩票系统,但每当我尝试按下其中一个按钮时,应用程序就会冻结,然后崩溃,我不知道为什么。程序没有给我错误,所以它真的很混乱。我看到了一些关于布局的问题,但我不知道它是什么。我将在问题下方的评论中回答页面布局。提前谢谢 以下是页面脚本: package com.example.myapplication; import android.graphics.Color; import android.icu.text.NumberFormat; import andro

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

以下是页面脚本:

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);
            }
        });
    }
}
package com.example.myapplication;
导入android.graphics.Color;
导入android.icu.text.NumberFormat;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.view.view;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.TextView;
导入java.util.*;
导入java.math.*;
导入org.w3c.dom.Text;
导入javax.xml.transform.Result;
公共类彩票活动{
按钮检查,随机;
编辑文本用户1;
text查看余额2,结果text;
国际收支平衡1=500;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_);
检查=(按钮)findViewById(R.id.CheckNumber);
随机=(按钮)findViewById(R.id.RandomNumber);
User1=findviewbyd(R.id.PickNumber);
Balance2=(TextView)findViewById(R.id.Balance);
ResultText=(TextView)findViewById(R.id.Result);
ResultText.setVisibility(View.GONE);
余额2.setText(“余额:$500”);
Check.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
int User=Integer.parseInt(String.valueOf(User1));
if(User1.toString().trim().equals(“”){
结果背景颜色(颜色:红色);
ResultText.setText(“请输入一个数字”);
ResultText.setVisibility(View.VISIBLE);
}
否则如果(用户>100 | |用户<1){
结果背景颜色(颜色:红色);
ResultText.setText(“请输入一个介于1和100之间的数字”);
ResultText.setVisibility(View.VISIBLE);
}
否则{
int答案=(int)((Math.random()*100)+1);
int占位符=答案%10;
int-AnswerTens=(答案-占位符)/10;
int AnswerOnes=占位符;
占位符=用户%10;
int UserTens=(用户-占位符)/10;
int UserOnes=占位符;
如果(用户==答案){
结果背景颜色(颜色:绿色);
ResultText.setText(“正确!!!号码是“+User+”,您的帐户中已添加$1000”);
ResultText.setVisibility(View.VISIBLE);
余额1+=1000;
余额2.setText(“余额:$”+余额1);
}
如果((UserTens==应答器)和(&(UserOnes==应答器)){
结果背景颜色(颜色:绿色);
ResultText.setText(“有点正确!数字是正确的,但顺序错误,答案是”+答案+“$500已添加到您的帐户”);
ResultText.setVisibility(View.VISIBLE);
余额1+=500;
余额2.setText(“余额:$”+余额1);
}
如果((UserTens==AnswerTens)| |(UserTens==AnswerOnes)| | |(UserOnes==AnswerOnes)| |(UserOnes==AnswerTens)){
结果背景颜色(颜色:绿色);
ResultText.setText(“有点正确!一个数字是正确的,答案是“+答案+”,100美元已添加到您的帐户中”);
ResultText.setVisibility(View.VISIBLE);
余额1+=100;
余额2.setText(“余额:$”+余额1);
}
否则{
结果背景颜色(颜色:红色);
ResultText.setText(“错误的数字是”+答案);
ResultText.setVisibility(View.VISIBLE);
}
}
}
});
Random.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
int答案=(int)((Math.random()*100)+1);
User1.setText(应答);
}
});
}
}
更改此选项:

User1.setText(答案);
为此:

User1.setText(String.valueOf(answer));

我发现了导致崩溃的几个可能原因。正如@Siros提到的,在调用
setText

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

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

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

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)
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
    }
}
您可能需要使用以下内容:

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)
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
    }
}

这是您需要的页面布局代码:它不是
脚本,而是java代码。你能把你的错误贴出来吗?看看