2014-02-26 25 views
0

我第一次使用內部存儲,而我的openFileOutput給了我一個錯誤。我不知道如何使這項工作,但下面的方法工作完美罰款,如果FileOutputStream被刪除。謝謝!FileOutputStream在Android中不起作用

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); 

    } 

} 
+1

錯誤..什麼錯誤? – eltabo

+0

它不識別openFileOutput(FILENAME,Context.MODE_PRIVATE);它建議我用try/catch添加一個throw聲明或環繞聲。謝謝! – user3246935

+0

'openFileOutput'是'ContextWrapper'的一個函數。這個「加法」函數在哪裏聲明? –

回答

2

如果您的類未延伸的上下文的子類,你需要傳遞一個Context實例(一個活動,服務,etc)來調用openFileInput。 您還需要捕獲java.io.FileNotFoundException或向您的方法添加拋出聲明。

希望它有幫助。