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

时间:2012-01-30 00:56:39

标签: java android eclipse crash android-edittext

我的代码有问题。 当我有一个空白的editText字段时,它会继续崩溃。 这段代码在我的应用程序的设置中并且可以很好地处理数据但是当有空白字段时它会使程序崩溃。 这是它的代码。 请不要苛刻,因为它是我的第一个Android应用程序。所以,如果有人知道如何解决空白编辑文本字段问题,将不胜感激! (关于如何改进应用程序的任何其他意见将是一个帮助)。

干杯

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"));

        }
    });

}



}

1 个答案:

答案 0 :(得分:2)

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

上述内容毫无意义。

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

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

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

您的文件读取可能会失败(可能是因为您稍后只在EditText方法中保存字符串)。因为如果文件读取失败,您的onClick(...)字符串将为task,那么您需要先测试null,然后再尝试使用它们。

换句话说,你在代码中这样做......

null

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

String task1 = null;

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