安卓:我的应用程序崩溃时,有一个空白领域的EditText [英] Android : My app crashes when there is a blank editText field

查看:350
本文介绍了安卓:我的应用程序崩溃时,有一个空白领域的EditText的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我的code。
它不断崩溃的时候,我有一个空白领域的EditText。
code的这一位是我的应用程序的设置和工作中的数据不错,但时有空白字段崩溃的程序。
这里的code吧。
请,因为这是我的第一个Android应用程序并不苛刻。所以,如果有人知道如何解决空白编辑文本字段的问题,这将是极大的AP preciated! (关于如何提高应用程序的任何其他意见将是一个帮助)。

干杯

 包com.cleanyet.cyv100fp;进口java.io.FileInputStream中;
进口java.io.FileNotFoundException;
进口java.io.FileOutputStream中;
进口java.io.IOException异常;
进口android.app.Activity;
进口android.content.Context;
进口android.content.Intent;
进口android.os.Bundle;
进口android.view.View;
进口android.widget.Button;
进口android.widget.EditText;公共类sTasks延伸活动{
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    // TODO自动生成方法存根
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.tasks);    / *设置变量* /
    按钮来完成=(按钮)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);
    T4的EditText =(EditText上)findViewById(R.id.tbTask4);
    T5的EditText =(EditText上)findViewById(R.id.tbTask5);    字符串FILENAME1 =stask1;
    字符串文件名2 =stask2;
    字符串FILENAME3 =stask3;
    字符串FILENAME4 =stask4;
    字符串FILENAME5 =stask5;    字符串任务1 = NULL;
    字符串TASK2 = NULL;
    字符串TASK3 = NULL;
    字符串task4 = NULL;
    字符串任务5 = NULL;    字符串编辑=编辑任务;    / *修复的空白字段的bug * /    / *设定文件输入* /
    FIS的FileInputStream = NULL;    / *获取任务设置previously * /    / *任务1设置* /
    尝试{
        FIS = openFileInput(文件名);
        字节[] dataArray中=新的字节[fis.available()];
        而(fis.read(dataArray中)!= -1){
            任务1 =新的String(da​​taArray中);        }    }赶上(FileNotFoundException异常五){
        // TODO自动生成catch块
        e.printStackTrace();
    }赶上(IOException异常五){
        // TODO自动生成catch块
        e.printStackTrace();
    }
    最后{
        尝试{
            fis.close();
        }赶上(IOException异常五){
            // TODO自动生成catch块
            e.printStackTrace();
        }
    }
    如果(task1.toString()长度()℃,){    task1.toString();    t1.setText(独立写作);
    }
    其他{
        t1.setText(编辑);
    }
    / *任务2设置* /    尝试{
        FIS = openFileInput(文件名);
        字节[] dataArray中=新的字节[fis.available()];
        而(fis.read(dataArray中)!= -1){
            TASK2 =新的String(da​​taArray中);        }    }赶上(FileNotFoundException异常五){
        // TODO自动生成catch块
        e.printStackTrace();
    }赶上(IOException异常五){
        // TODO自动生成catch块
        e.printStackTrace();
    }
    最后{
        尝试{
            fis.close();
        }赶上(IOException异常五){
            // TODO自动生成catch块
            e.printStackTrace();
        }
    }
    如果(task2.toString()长度()℃,){    task2.toString();    t2.setText(TASK2);
    }
    其他{
        t2.setText(编辑);
    }
    / *任务3设置* /    尝试{
        FIS = openFileInput(FILENAME3);
        字节[] dataArray中=新的字节[fis.available()];
        而(fis.read(dataArray中)!= -1){
            TASK3 =新的String(da​​taArray中);        }    }赶上(FileNotFoundException异常五){
        // TODO自动生成catch块
        e.printStackTrace();
    }赶上(IOException异常五){
        // TODO自动生成catch块
        e.printStackTrace();
    }
    最后{
        尝试{
            fis.close();
        }赶上(IOException异常五){
            // TODO自动生成catch块
            e.printStackTrace();
        }
    }    如果(task3.toString()长度()℃,){    task3.toString();    t3.setText(TASK3);
    }
    其他{
        t3.setText(编辑);
    }
    / *任务4设置* /
    尝试{
        FIS = openFileInput(FILENAME4);
        字节[] dataArray中=新的字节[fis.available()];
        而(fis.read(dataArray中)!= -1){
            task4 =新的String(da​​taArray中);        }    }赶上(FileNotFoundException异常五){
        // TODO自动生成catch块
        e.printStackTrace();
    }赶上(IOException异常五){
        // TODO自动生成catch块
        e.printStackTrace();
    }
    最后{
        尝试{
            fis.close();
        }赶上(IOException异常五){
            // TODO自动生成catch块
            e.printStackTrace();
        }
    }    如果(task4.toString()长度()℃,){    task4.toString();    t4.setText(task4);
    }
    其他{
        t4.setText(编辑);
    }
    / *任务5设置* /    尝试{
        FIS = openFileInput(FILENAME5);
        字节[] dataArray中=新的字节[fis.available()];
        而(fis.read(dataArray中)!= -1){
            任务5 =新的String(da​​taArray中);        }    }赶上(FileNotFoundException异常五){
        // TODO自动生成catch块
        e.printStackTrace();
    }赶上(IOException异常五){
        // TODO自动生成catch块
        e.printStackTrace();
    }
    最后{
        尝试{
            fis.close();
        }赶上(IOException异常五){
            // TODO自动生成catch块
            e.printStackTrace();
        }
    }    如果(task5.toString()长度()℃,){    task5.toString();    t5.setText(任务5);
    }
    其他{
        t5.setText(编辑);
    }    / *当已经作了修改,并进行点击* /
    done.setOnClickListener(新View.OnClickListener(){        公共无效的onClick(视图v){
            // TODO自动生成方法存根            / *设置变量* /
            的EditText T1 =(EditText上)findViewById(R.id.tbTask1);
            的EditText T2 =(EditText上)findViewById(R.id.tbTask2);
            的EditText T3 =(EditText上)findViewById(R.id.tbTask3);
            T4的EditText =(EditText上)findViewById(R.id.tbTask4);
            T5的EditText =(EditText上)findViewById(R.id.tbTask5);            。字符串tasks1 = t1.getText()的toString();
            。字符串任务2 = t2.getText()的toString();
            。字符串tasks3 = t3.getText()的toString();
            。字符串tasks4 = t4.getText()的toString();
            。字符串tasks5 = t5.getText()的toString();            字符串FILENAME1 =stask1;
            字符串文件名2 =stask2;
            字符串FILENAME3 =stask3;
            字符串FILENAME4 =stask4;
            字符串FILENAME5 =stask5;            字符串任务1 = tasks1;
            字符串TASK2 =任务2;
            字符串TASK3 = tasks3;
            字符串task4 = tasks4;
            字符串任务5 = tasks5;
            字符串编辑=去设置,使这项任务。            如果(T1!= NULL){                t1.setText(编辑);
                /*t2.setText(edit);
                t3.setText(编辑);
                t4.setText(编辑);
                t5.setText(编辑); * /            };            / *如果把这里的语句赶上空字段* /            / *使该任务的变化* /
            尝试{                FileOutputStream中fos1 = openFileOutput(FILENAME1,Context.MODE_PRIVATE);
                fos1.write(task1.getBytes());
                fos1.close();            }赶上(FileNotFoundException异常五){
                // TODO自动生成catch块
                e.printStackTrace();
            }赶上(IOException异常五){
                // TODO自动生成catch块
                e.printStackTrace();
            }            尝试{                FileOutputStream中fos2 = openFileOutput(文件名,Context.MODE_PRIVATE);
                fos2.write(task2.getBytes());
                fos2.close();            }赶上(FileNotFoundException异常五){
                // TODO自动生成catch块
                e.printStackTrace();
            }赶上(IOException异常五){
                // TODO自动生成catch块
                e.printStackTrace();
            }            尝试{                FileOutputStream中fos3 = openFileOutput(FILENAME3,Context.MODE_PRIVATE);
                fos3.write(task3.getBytes());
                fos3.close();            }赶上(FileNotFoundException异常五){
                // TODO自动生成catch块
                e.printStackTrace();
            }赶上(IOException异常五){
                // TODO自动生成catch块
                e.printStackTrace();
            }            尝试{                FileOutputStream中fos4 = openFileOutput(FILENAME4,Context.MODE_PRIVATE);
                fos4.write(task4.getBytes());
                fos4.close();            }赶上(FileNotFoundException异常五){
                // TODO自动生成catch块
                e.printStackTrace();
            }赶上(IOException异常五){
                // TODO自动生成catch块
                e.printStackTrace();
            }            尝试{                FileOutputStream中fos5 = openFileOutput(FILENAME5,Context.MODE_PRIVATE);
                fos5.write(task5.getBytes());
                fos5.close();            }赶上(FileNotFoundException异常五){
                // TODO自动生成catch块
                e.printStackTrace();
            }赶上(IOException异常五){
                // TODO自动生成catch块
                e.printStackTrace();
            }
            startActivity(新意图(com.checkin.cyv100fp.settings));        }
    });}}


解决方案

 如果(task1.toString()长()< 0){
    task1.toString();
    t1.setText(独立写作);
}
其他{
    t1.setText(编辑);
}

以上是没有意义的。

首先任务1 是一个字符串,所以没有必要调用的toString()将其转换为一个。

其次,你的条件语句(如果)将被检查是否任务1 的长度小于零。 ......考虑一下。

第三,如果它确实有一个长度不可能小于零,然后调用上再次的toString()(不变量来接收不可能小于零的结果)你再试着设置你的 T1的文字 的EditText

有机会,你的文件读取失败(可能是因为你只是后来在的onClick(...)方法保存字符串)。因为你的任务字符串将如果文件读取失败,那么你需要测试前尝试使用它们。

在换句话说,你会在你的code这样...

 字符串任务1 = NULL;

要解决code的我包围在一开始位,使用...

 如果(独立写作!= NULL){
    t1.setText(独立写作);
}
其他{
    t1.setText(编辑);
}

...但最重要的是,确保你的文件有字符串他们,你需要阅读。

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).

Cheers

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

The above makes no sense at all.

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

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

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.

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.

这篇关于安卓:我的应用程序崩溃时,有一个空白领域的EditText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆