我是第一次使用内部存储,我的 openFileOutput 给了我一个错误。我不知道如何进行这项工作,但如果删除 FileOutputStream,下面的方法可以完美地工作。谢谢!
public void Addition(View view) {
int num1 = numberone.getText().toString().trim().length();
int num2 = numbertwo.getText().toString().trim().length();
String numone = numberone.getText().toString();
String numtwo = numbertwo.getText().toString();
if(num1 == 0 || num2 == 0){
TextView value = (TextView)findViewById(R.id.answer);
value.setText("Numbers not entered");
}
else if(numone.equals("+") || numtwo.equals("+") || numtwo.equals("-") || numone.equals("-")){
TextView value = (TextView)findViewById(R.id.answer);
value.setText("Please enter numbers");
}
else{
answer = Float.parseFloat(numberone.getText().toString()) + Float.parseFloat(numbertwo.getText().toString());;
TextView value = (TextView)findViewById(R.id.answer);
value.setText("Your sum is " + answer);
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
}
}
最佳答案
如果您的类没有扩展 Context 子类,则需要传递 Context 实例(Activity、Service、 etc )来调用 openFileInput。 此外,您还需要捕获 java.io.FileNotFoundException 或将 throws 声明添加到您的方法中。
希望有帮助。
关于java - FileOutputStream 在 Android 中不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22033253/