gpt4 book ai didi

java - 单击按钮后我的应用程序崩溃

转载 作者:行者123 更新时间:2023-12-02 10:11:31 25 4
gpt4 key购买 nike

我对 Android 开发非常陌生,只有少量的 Java 编程知识。我需要帮助!我正在制作一个应用程序,允许您输入您花费的金额,它会计算您还可以花费多少钱。它还会跟踪您留在文本文件中的预算。它是在 android studio 中使用 Java 制作的。

这是我的错误消息:

2019-03-03 09:42:14.245 4178-4178/? E/SPPClientService: [PackageInfoChangeReceiver] [handlePkgRemovedEvent] PackageName : com.concretegames.budgettracker, true, false
2019-03-03 09:42:27.573 16961-16961/com.concretegames.budgettracker E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.concretegames.budgettracker, PID: 16961
java.lang.NumberFormatException: s == null
at java.lang.Integer.parseInt(Integer.java:570)
at java.lang.Integer.parseInt(Integer.java:643)
at com.concretegames.budgettracker.MainActivity$1.onClick(MainActivity.java:56)
at android.view.View.performClick(View.java:6935)
at android.widget.TextView.performClick(TextView.java:12738)
at android.view.View$PerformClick.run(View.java:26211)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:7000)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:441)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)

这是我的代码:

package com.concretegames.budgettracker;

import android.content.Context;
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.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import static java.lang.System.out;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

final TextView response = findViewById(R.id.response);
final Button calc = findViewById(R.id.Calculate);
Context context = this;
String filepath = "total_expense.txt";
String filestoragepath = "MyFileStorage";

final File edit_file = new File(getExternalFilesDir(filestoragepath), filepath);

EditText $box = findViewById(R.id.expenses);
final String $ = $box.getText().toString();

calc.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {

if (edit_file.exists() && !edit_file.isDirectory()) {

try {

FileReader fr = new FileReader(edit_file);
BufferedReader br = new BufferedReader(fr);

String str;

while ((str = br.readLine()) != null) {
out.println(str + "\n");
}

br.close();

int result = Integer.parseInt(str) - Integer.parseInt($);

FileWriter fw = new FileWriter(edit_file);
PrintWriter pw = new PrintWriter(fw);

pw.print("");
pw.print(result);

pw.close();

response.setText("Budget left over: "+result);

} catch (IOException e) {
out.println("ERROR: " + e.toString());
response.setText("ERROR: " + e.toString());
}
} else {
try {
edit_file.createNewFile();

FileWriter fw = new FileWriter(edit_file);
PrintWriter pw = new PrintWriter(fw);

pw.print("1500");

pw.close();
} catch (IOException e) {
out.println("ERROR: " + e.toString());
response.setText("ERROR: " + e.toString());
}
}

}

});

}
}

附注我只有 13 岁,没有 Java 经验。我更喜欢 HTML、Python 和 JavaScript。如有任何帮助,我们将不胜感激!

最佳答案

如果 Integer.parseInt() 无法成功将 String 解析为 int,则会抛出异常。但抛出的异常是RuntimeException的子类,因此java编译器不会强制你捕获异常。但仍然强烈建议您捕获它。并且可以避免崩溃。一般来说,在将字符串解析为整数时,请执行以下操作:

int result;
try {
result = Integer.parseInt(someString);
} catch (NumberFormatException e) {
result = 0;
}

关于java - 单击按钮后我的应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54971362/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com