为什么我的应用程序只有在使用OpenFileInput / Ouput函数时将字符串直接赋值给代码时才起作用

我的应用程序具有将数据保存到内部存储的function,并从edittext字段获取文件的名称。 这个edittext是用setText函数填充的。 但是,当它尝试使用从“自动填充”的edittext字段获取的名称的OpenFileOutput时,它崩溃。

但是,当我将相同的字符串分配给代码中的同一个variables(如:文件名=“a”而不是文件名= smth.text.toString()),它工作得很好。

我也试着用toast函数来看看它从字段中得到了什么,我得到了我期望得到的,自动填充字段的字符串,所以它可以与其他函数一起使用,但不能与OpenFileOutput函数一起使用。 我已经尝试了一切,我不知道什么会导致问题。

fun saveResults(view: View) { //Gets a string from a field with the .setText() function val showName = findViewById(R.id.saveName) as EditText var filename: String = showName.text.toString() //If I assign any string here (filename = "John") it works //Date val getDate = Date() val stringDate: String = getDate.toString() val date = stringDate.split(" ") //Creates a string that will be saved ti internal storage and splitted in the future with the .split("_") function var string: String = aText + "_" + date[2] + "_" + date[1] + "_" + date[5] //Saves data to the internal storage var fos: FileOutputStream fos = openFileOutput(filename, Context.MODE_PRIVATE) fos.write(string.toByteArray(Charset.defaultCharset())) fos.close() //Shows a toast Toast.makeText(this, "Saved to $filename", Toast.LENGTH_SHORT).show()} 

PS我也尝试从内部存储导入名称,但问题是相同的。 我是Kotlin或Android的新手。 谢谢。

Interesting Posts