Android验证码页面如何比对结果?
题目描述
使用计算题的验证方式,验证成功则跳转到新页面,否则提示验证失败
题目来源及自己的思路
通过比对正确结果和用户输入的答案来进行下一步操作
相关代码
粘贴代码文本(请勿用截图)
package com.example.goout;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import java.util.Random;
public class VerifyActivity extends AppCompatActivity {
TextView tv_num;TextView tv_num2;
TextView tv_info;
int result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_verify);
tv_num=findViewById(R.id.num1);
tv_info=findViewById(R.id.textView6);
//用户输入
tv_num2=findViewById(R.id.number2);
//生成随机数(计算)
Random random=new Random();
int a=(int)(Math.random()*(100-50+1));
int b=(int)(Math.random()*(50-1+1));
result=a-b;
int swit=(int)(Math.random()*(3-1+1));
switch (swit){
case 1:tv_num.setText(a+"+"+b+"=");break;
case 2:tv_num.setText(a+"-"+b+"=");break;
}
}
//验证步骤
public void varifyAc(View v){
//获取到用户输入的结果
int num2=Integer.valueOf(tv_num2.getText().toString());
//比较结果
if (result==num2){
Intent intent=new Intent(this,PriceActivity.class);
startActivity(intent);
}else{
tv_info.setText("false");
}
}
//跳回页面1
public void gotoMain(View v){
Intent intent=new Intent(this,MainActivity.class);
startActivity(intent);
}
}
你期待的结果是什么?实际看到的错误信息又是什么?
点击验证按钮时程序闪退
--------- beginning of crash2022-09-18 20:59:03.431 19260-19260/com.example.goout E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.goout, PID: 19260
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.goout/com.example.goout.PriceActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
at com.example.goout.PriceActivity.onCreate(PriceActivity.java:19)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
回答:
int num2=Integer.valueOf(tv_num2.getText().toString());
这里可能抛出异常,如果 tv_num2.getText().toString()
不能解析成数字,则会抛出 NumberFormatException
捕获异常即可,另外建议解析成 int
直接用 Integer.parseInt()
try { int num2=Integer.parseInt(tv_num2.getText().toString());
if (result==num2){
Intent intent=new Intent(this,PriceActivity.class);
startActivity(intent);
return;
}
} catch (NumberFormatException ignored) {
}
tv_info.setText("false");
以上是 Android验证码页面如何比对结果? 的全部内容, 来源链接: utcz.com/p/944723.html