Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android警报对话框Edittext值未显示_Android_Android Alertdialog - Fatal编程技术网

Android警报对话框Edittext值未显示

Android警报对话框Edittext值未显示,android,android-alertdialog,Android,Android Alertdialog,我被代码的一部分所震惊,其中显示了一个警告框,要求用户输入数据集的名称。将在内部存储器中创建同名文件。警报对话框工作正常,但sd卡中创建的文件始终为“null.txt”。经过数小时的搜索,我尝试了所有的解决方案,例如为警报框创建自定义布局,在edittext中添加TextWatcher等,但没有任何帮助。当我手动将数据集的名称分配给变量“Set”时,文件被成功创建。因此,警报对话框存在问题。我正在使用android studio 2.3.1。请帮忙 我也张贴代码 else if (map2.co

我被代码的一部分所震惊,其中显示了一个警告框,要求用户输入数据集的名称。将在内部存储器中创建同名文件。警报对话框工作正常,但sd卡中创建的文件始终为“
null.txt
”。经过数小时的搜索,我尝试了所有的解决方案,例如为警报框创建自定义布局,在
edittext
中添加
TextWatcher
等,但没有任何帮助。当我手动将数据集的名称分配给变量“Set”时,文件被成功创建。因此,
警报对话框
存在问题。我正在使用android studio 2.3.1。请帮忙

我也张贴代码

else if (map2.contains("1") || map2.contains("4")) {
    final EditText edittext = new EditText(Scan.this);
    AlertDialog.Builder alert1 = new AlertDialog.Builder(Scan.this);
    alert1.setMessage("Name of the dataset");
    alert1.setTitle("Enter Your Title");
    alert1.setView(edittext);
    alert1.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            String set = edittext.getText().toString();
            Set = set;
        }
    });
    alert1.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
        public void onClick(DialogInterface dialog, int whichButton) {
        }
    });
    alert1.show();
}
myFile = new File(Environment.getExternalStorageDirectory(),Set+".txt");
if (!myFile.exists()) {
    try {
        myFile.createNewFile();
        FileOutputStream fOut = new FileOutputStream(myFile);
        OutputStreamWriter osw = new OutputStreamWriter(fOut);
        osw.write(message1);
        osw.flush();
        osw.close();
        fOut.close();
    }
    catch (Exception e) {
        e.printStackTrace();
    }
   finally {
   }
}
请尝试以下代码:

对包含
Edittext
的自定义布局进行充气,并像以下一样使用:

 LayoutInflater inflater = LayoutInflater.from(Scan.this);            
final View view = inflater.inflate(R.layout.custom_layout, null);
AlertDialog.Builder alert = new AlertDialog.Builder(Scan.this); 

alert.setTitle("Tilte"); 
alert.setMessage("Name of the dataset"); 
alert.setView(view); 

final EditText et1 = (EditText) view.findViewById(R.id.editText1);

alert.setPositiveButton("ok", new DialogInterface.OnClickListener() { 
   public void onClick(DialogInterface dialog, int whichButton) 
   { 
          String s1=et1.getText().toString();
          //do operations using s1...
          myFile = new File(Environment.getExternalStorageDirectory(),s1+".txt");
            if (!myFile.exists()) {
                try {
                    myFile.createNewFile();
                    FileOutputStream fOut = new FileOutputStream(myFile);
                    OutputStreamWriter osw = new OutputStreamWriter(fOut);
                    osw.write(message1);
                    osw.flush();
                    osw.close();
                    fOut.close();
                }
                catch (Exception e) {
                    e.printStackTrace();
                }
               finally {
               }
        } 
}); 

alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
   public void onClick(DialogInterface dialog, int whichButton) { 
            dialog.cancel(); 
   } 
}); 

alert.show(); 
来自@Deepti Maduskar


我终于可以解决这个问题了。问题在于全局可值集的定义。我只需要声明它,而不是将它定义为
final

在对话框中设置标题之前,您正在创建文件。 试试这个:

else if (map2.contains("1") || map2.contains("4")) {
    final EditText edittext = new EditText(Scan.this);
    AlertDialog.Builder alert1 = new AlertDialog.Builder(Scan.this);
    alert1.setMessage("Name of the dataset");
    alert1.setTitle("Enter Your Title");
    alert1.setView(edittext);
    alert1.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            String set = edittext.getText().toString();
            createFile(set);
        }
    });
    alert1.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
        public void onClick(DialogInterface dialog, int whichButton) {
        }
    });
    alert1.show();
}
}
private void createFile(String set){
   myFile = new 
 File(Environment.getExternalStorageDirectory(),set+".txt");
   if (!myFile.exists()) {
       try {
           myFile.createNewFile();
           FileOutputStream fOut = new FileOutputStream(myFile);
           OutputStreamWriter osw = new OutputStreamWriter(fOut);
           osw.write(message1);
           osw.flush();
           osw.close();
           fOut.close();
       }
       catch (Exception e) {
           e.printStackTrace();
       }
       finally {
       }
   }

}您是否在清单中授予了权限:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

onClick
中打印日志,查看是否获得值…是,正在分配值,但是在警报对话框可以传递值之前创建了该文件。该文件仍然被创建为null.txt。是否在
catch
块中捕获任何异常?…同时删除先前的null文件并运行..我始终删除nul文件,然后运行代码。但问题依然存在。Set变量有问题吗?当我给同一个“Set”变量分配其他值时,它工作正常。正在创建该文件。
   String editString = mEditText.getText().toString();
   filename = fileName_mEditText.getText().toString();

   if (filename.trim().equalsIgnoreCase("")) {
       fileName_mEditText.setError("Enter File Name!");
   }else if (editString.trim().equalsIgnoreCase("")) {
       mEditText.setError("Enter Text!");
   } else {
       try {
            FileOperations fop = new FileOperations();
            fop.write(filename, editString);
            if (fop.write(filename, editString)) {
                 Toast.makeText(getApplicationContext(), filename + ".txt created", Toast.LENGTH_SHORT).show();
            } else {
                 Toast.makeText(getApplicationContext(), "I/O error", Toast.LENGTH_SHORT).show();
            }
            } catch (Exception e) {
                    e.printStackTrace();
            }
       }
  }