簡體   English   中英

Android:當空白editText字段時,我的應用程序崩潰

[英]Android : My app crashes when there is a blank editText field

我的代碼有問題。 當我有一個空白的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"));

        }
    });

}



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

以上完全沒有意義。

首先, task1是一個字符串,因此無需調用toString()即可將其轉換為一個字符串。

其次,您的條件語句( if )正在檢查task1的長度是否小於零。...考慮一下。

第三,如果它的長度小於零,則再次調用toString() (沒有變量接收小於零的結果),然后嘗試設置t1 EditText的文本。

可能是文件讀取失敗(可能是因為稍后僅在onClick(...)方法中保存了字符串)。 因為如果文件讀取失敗,您的task字符串將為null ,那么您需要在嘗試使用null字符串之前對其進行測試。

換句話說,您正在代碼中執行此操作...

String task1 = null;

要修復我一開始附帶的代碼,請使用...

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

...但最重要的是,請確保您的文件中包含需要讀取的字符串。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM