我该如何解决这个错误? EditText.getText()返回空字符串“”

时间:2019-05-11 23:55:38

标签: java android io

我的Android Studio项目有错误。 我正在尝试保存文件,为此,我获取了不同edittext的文本,并将它们写在外部存储中的文本文件中。 这里的错误是文件仅包含空字符串字符(“”),这是因为我拥有的字符串变量为空,它们采用edittext的值,但它们仍为空。

我正在我的AppCompatDialogFragment类中进行此操作

这是我的代码:

 private ImageButton CloseB;
    private ImageButton SaveB;
    private EditText EditTitle;
    private EditText EditDesc;
    private Spinner TagSpinner;
    private EditText EditDate;
    private EditText EditHour;

    private String Hour;
    private String FileName;
    private String Title;
    private String STag;
    private String Description;
    private String Date;


    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState)
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        AlertDialog Dialog = builder.create();
        LayoutInflater Inflater = getActivity().getLayoutInflater();
        View view = Inflater.inflate(R.layout.newtaskdialog, null);
        builder.setView(view);
        CloseB = view.findViewById(R.id.NewTCloseB);
        SaveB = view.findViewById(R.id.SaveBN);

        CloseB.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

                SaveB.setOnClickListener(new View.OnClickListener()
        {
                @Override
            public void onClick(View v) {
                Title = EditTitle.getText().toString();
                Date = EditDate.getText().toString();
                Hour = EditHour.getText().toString();
                Description = EditDesc.getText().toString();
                SaveFile();
                //dismiss();
            }
        });
        return builder.create();
      }



    public void SaveFile()
    {
        String State = getExternalStorageState();
        String LTitle = Title.toLowerCase();
        String FileName = Title + ".ard";
        byte Tag = GetTag();
        int ITag = Tag;
        String STag = String.valueOf(ITag);
        if (Environment.MEDIA_MOUNTED.equals(State))
        {
            File Root = Environment.getExternalStorageDirectory();
            File Dir = new File(Root.getAbsolutePath() + "/AcardiaFiles"); //<-- Need to check in which folder is making the task
            if (!Dir.exists())
            {
                Dir.mkdir();
            }
            File file = new File(Dir, FileName);
            String Message = "false" + "\n" +  Title + "\n" + Date + "\n" + Hour + "\n" + STag + "\n" + Description;

            try {
                FileOutputStream fileOutputStream = new FileOutputStream(file);
                fileOutputStream.write(Message.getBytes());
                fileOutputStream.close();
                Toast.makeText(getContext(), "File Saved." + Title, Toast.LENGTH_SHORT).show();

            }
            catch (FileNotFoundException e)
            {
                e.printStackTrace();
            }

            catch (IOException e)
            {
                e.printStackTrace();
            }

        }
        else
        {

        }
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Dialog d = getDialog();
        View v = inflater.inflate(R.layout.newtaskdialog, container, false);
        EditTitle = v.findViewById(R.id.editNTitle);
        EditDate = v.findViewById(R.id.editNDate);
        EditDesc = v.findViewById(R.id.editNDesc);
        EditHour = v.findViewById(R.id.editNHour);
        TAdapter = new TagAdapter(getContext(), tagList);
        TagSpinner = v.findViewById(R.id.TagNSpinner);

        EditDate.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) {
                final Calendar C = Calendar.getInstance();

            }
        });
        return super.onCreateView(inflater, container, savedInstanceState);

    }

0 个答案:

没有答案