久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Android:當有一個空白的editText字段時,我的應用程

Android : My app crashes when there is a blank editText field(Android:當有一個空白的editText字段時,我的應用程序崩潰了)
本文介紹了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.

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

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模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權益,請聯(lián)系我們刪除處理,感謝您的支持!

相關文檔推薦

Parsing an ISO 8601 string local date-time as if in UTC(解析 ISO 8601 字符串本地日期時間,就像在 UTC 中一樣)
How to convert Gregorian string to Gregorian Calendar?(如何將公歷字符串轉換為公歷?)
Java: What/where are the maximum and minimum values of a GregorianCalendar?(Java:GregorianCalendar 的最大值和最小值是什么/在哪里?)
Calendar to Date conversion for dates before 15 Oct 1582. Gregorian to Julian calendar switch(1582 年 10 月 15 日之前日期的日歷到日期轉換.公歷到儒略歷切換)
java Calendar setFirstDayOfWeek not working(java日歷setFirstDayOfWeek不起作用)
Java: getting current Day of the Week value(Java:獲取當前星期幾的值)
主站蜘蛛池模板: 91精品国产91久久久久福利 | 一本一道久久a久久精品综合蜜臀 | 精品成人 | 日本手机在线 | 亚洲一区二区三区在线免费 | 欧美一级久久久猛烈a大片 日韩av免费在线观看 | 午夜精品久久久久久久久久久久久 | 欧美精品在线一区二区三区 | 天天影视网天天综合色在线播放 | 亚洲精品一区二区三区在线 | 国产精品污污视频 | 久久99精品久久久久久 | 国产视频一二三区 | 精品欧美一区二区精品久久久 | 韩国毛片一区二区三区 | 一级免费毛片 | 黄篇网址 | 日韩精品一区二区三区四区 | 日韩免费看视频 | 成人h动漫精品一区二区器材 | 久久精品综合 | 日韩欧美综合 | 免费一级网站 | 99国内精品 | 精品久久国产 | 午夜激情视频 | 成人在线视频免费观看 | 日本久久综合网 | 午夜免费av | 在线观看黄色电影 | 国产精品国产精品国产专区不蜜 | 国产香蕉视频 | 成人在线看片 | 国产精品日女人 | 大陆一级毛片免费视频观看 | 国产中文原创 | 午夜精品久久久久久久星辰影院 | 在线中文字幕第一页 | 国产色在线 | 91免费观看 | 午夜极品|