Python中文网

一个关于 编程问题的解答网站.

有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java My应用程序在点击按钮后崩溃

我对安卓开发非常陌生,只懂少量Java编程知识。我需要帮助!我正在制作一个应用程序,它可以让你输入你花的钱的数量,并计算出你还能花多少钱。它还将跟踪您留在文本文件中的预算。它是在安卓 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 安卓.view.View.performClick(View.java:6935)
    at 安卓.widget.TextView.performClick(TextView.java:12738)
    at 安卓.view.View$PerformClick.run(View.java:26211)
    at 安卓.os.Handler.handleCallback(Handler.java:790)
    at 安卓.os.Handler.dispatchMessage(Handler.java:99)
    at 安卓.os.Looper.loop(Looper.java:164)
    at 安卓.app.ActivityThread.main(ActivityThread.java:7000)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.安卓.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:441)
    at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:1408)

这是我的代码:

package com.concretegames.budgettracker;

import 安卓.content.Context;
import 安卓.support.v7.app.AppCompatActivity;
import 安卓.os.Bundle;
import 安卓.view.View;
import 安卓.widget.Button;
import 安卓.widget.EditText;
import 安卓.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。感谢您的帮助


共 (0) 个答案