Android:当有一个空白的editText字段时,我的应用程

时间:2023-04-17
本文介绍了Android:当有一个空白的editText字段时,我的应用程序崩溃了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我的代码有问题.当我有一个空白的 editText 字段时,它一直在崩溃.这段代码在我的应用程序的设置中,可以正常工作,但是当有一个空白字段时,它会使程序崩溃.这是它的代码.请不要苛刻,因为这是我的第一个 android 应用程序.因此,如果有人知道如何解决空白编辑文本字段问题,将不胜感激!(有关如何改进应用程序的任何其他评论都会有所帮助).

I have a problem with my code. It keeps on crashing when i have a blank editText field. This bit of code is in the settings of my app and works the data fine but when there is a blank field it crashes the program. Here's the code for it. Please don't be harsh because it is my 1st android app. So if anyone knows how to solves the blank edit text field problem that would be greatly appreciated! (Any other comments on how to improve the app would be a help to).

干杯

package com.cleanyet.cyv100fp;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class sTasks extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tasks);     

    /*Sets Up the Variables*/
    Button done = (Button) findViewById(R.id.done1);
    EditText t1 = (EditText)findViewById(R.id.tbTask1);
    EditText t2 = (EditText)findViewById(R.id.tbTask2);
    EditText t3 = (EditText)findViewById(R.id.tbTask3);
    EditText t4 = (EditText)findViewById(R.id.tbTask4);
    EditText t5 = (EditText)findViewById(R.id.tbTask5);

    String FILENAME1 = "stask1";
    String FILENAME2 = "stask2";
    String FILENAME3 = "stask3";
    String FILENAME4 = "stask4";
    String FILENAME5 = "stask5";

    String task1 = null;
    String task2 = null;
    String task3 = null;
    String task4 = null;
    String task5 = null;

    String edit = "Edit Task";

    /*Fixes the Blank Field bug*/

    /*Sets up the file input*/
    FileInputStream fis = null;

    /*Gets the tasks set previously*/

    /*Task 1 set up*/
    try {
        fis = openFileInput(FILENAME1);
        byte[] dataArray = new byte[fis.available()];
        while (fis.read(dataArray) != -1){
            task1 = new String(dataArray);

        }

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    finally {
        try {
            fis.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    if (task1.toString().length() < 0) {

    task1.toString();

    t1.setText(task1);
    }
    else {
        t1.setText(edit);
    }
    /*Task 2 set up*/

    try {
        fis = openFileInput(FILENAME2);
        byte[] dataArray = new byte[fis.available()];
        while (fis.read(dataArray) != -1){
            task2 = new String(dataArray);

        }

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    finally {
        try {
            fis.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    if (task2.toString().length() < 0) {

    task2.toString();

    t2.setText(task2);
    }
    else {
        t2.setText(edit);
    }
    /*Task 3 set up*/

    try {
        fis = openFileInput(FILENAME3);
        byte[] dataArray = new byte[fis.available()];
        while (fis.read(dataArray) != -1){
            task3 = new String(dataArray);

        }

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    finally {
        try {
            fis.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    if (task3.toString().length() < 0) {

    task3.toString();

    t3.setText(task3);
    }
    else {
        t3.setText(edit);
    }
    /*Task 4 set up*/


    try {
        fis = openFileInput(FILENAME4);
        byte[] dataArray = new byte[fis.available()];
        while (fis.read(dataArray) != -1){
            task4 = new String(dataArray);

        }

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    finally {
        try {
            fis.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    if (task4.toString().length() < 0) {

    task4.toString();

    t4.setText(task4);
    }
    else {
        t4.setText(edit);
    }
    /*Task 5 set up*/           

    try {
        fis = openFileInput(FILENAME5);
        byte[] dataArray = new byte[fis.available()];
        while (fis.read(dataArray) != -1){
            task5 = new String(dataArray);

        }

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    finally {
        try {
            fis.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    if (task5.toString().length() < 0) {

    task5.toString();

    t5.setText(task5);
    }
    else {
        t5.setText(edit);
    }

    /*When changes have been made and done is clicked*/
    done.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub

            /*Sets up the Variables*/
            EditText t1 = (EditText)findViewById(R.id.tbTask1);
            EditText t2 = (EditText)findViewById(R.id.tbTask2);
            EditText t3 = (EditText)findViewById(R.id.tbTask3);
            EditText t4 = (EditText)findViewById(R.id.tbTask4);
            EditText t5 = (EditText)findViewById(R.id.tbTask5);

            String tasks1 = t1.getText().toString();
            String tasks2 = t2.getText().toString();
            String tasks3 = t3.getText().toString();
            String tasks4 = t4.getText().toString();
            String tasks5 = t5.getText().toString();

            String FILENAME1 = "stask1";
            String FILENAME2 = "stask2";
            String FILENAME3 = "stask3";
            String FILENAME4 = "stask4";
            String FILENAME5 = "stask5";

            String task1 = tasks1;
            String task2 = tasks2;
            String task3 = tasks3;
            String task4 = tasks4;
            String task5 = tasks5;
            String edit = "Go to settings to make this task.";

            if (t1 != null){

                t1.setText(edit);
                /*t2.setText(edit);
                t3.setText(edit);
                t4.setText(edit);
                t5.setText(edit);*/

            };

            /*Put if statement here to catch the empty field*/

            /*Makes The Changes to the Tasks*/
            try {

                FileOutputStream fos1 = openFileOutput(FILENAME1, Context.MODE_PRIVATE);
                fos1.write(task1.getBytes());
                fos1.close();

            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            try {

                FileOutputStream fos2 = openFileOutput(FILENAME2, Context.MODE_PRIVATE);
                fos2.write(task2.getBytes());
                fos2.close();

            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            try {

                FileOutputStream fos3 = openFileOutput(FILENAME3, Context.MODE_PRIVATE);
                fos3.write(task3.getBytes());
                fos3.close();

            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            try {

                FileOutputStream fos4 = openFileOutput(FILENAME4, Context.MODE_PRIVATE);
                fos4.write(task4.getBytes());
                fos4.close();

            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            try {

                FileOutputStream fos5 = openFileOutput(FILENAME5, Context.MODE_PRIVATE);
                fos5.write(task5.getBytes());
                fos5.close();

            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


            startActivity(new Intent("com.checkin.cyv100fp.settings"));

        }
    });

}



}

推荐答案

if (task1.toString().length() < 0) {
    task1.toString();
    t1.setText(task1);
}
else {
    t1.setText(edit);
}

以上说的毫无意义.

首先 task1 是一个字符串,因此无需调用 toString() 将其转换为一个.

Firstly task1 IS a string so there's no need to call toString() to convert it to one.

其次,您的条件语句 (if) 正在检查 task1 的长度是否小于零......考虑一下.

Secondly, your conditional statement (if) is checking to see if task1 has a length less than zero....think about it.

第三,如果它确实有一个不可能的长度小于零,那么你再次调用 toString() (没有变量来接收不可能小于零的结果),然后你尝试设置你的 t1 EditText 的文本.

Thirdly, if it does have an impossible length less than zero you then call toString() on it again (with no variable to receive the impossible less than zero result) and you then try to set the text of your t1 EditText.

可能是您的文件读取失败(可能是因为您稍后仅在 onClick(...) 方法中保存字符串).因为如果文件读取失败,您的 task 字符串将为 null,因此您需要在尝试使用它们之前测试 null.

The chances are that your file reading is failing (probably because you only save the strings later in the onClick(...) method). Because your task strings will be null if the file reading fails then you need to test for null before trying to use them.

换句话说,您正在代码中执行此操作...

In other words you're doing this in your code...

String task1 = null;

要修复我在开头附上的那段代码,请使用...

To fix the bit of code I enclosed at the beginning, use...

if (task1 != null) {
    t1.setText(task1);
}
else {
    t1.setText(edit);
}

...但最重要的是,确保您的文件中包含您需要读取的字符串.

...but most importantly, make sure your files have the strings in them that you need to read.

这篇关于Android:当有一个空白的editText字段时,我的应用程序崩溃了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:Android 应用程序在单击按钮时崩溃 下一篇:片段中的功能使模拟器崩溃

相关文章

最新文章