2011-07-16 84 views
0

我是一個爲android開發的業餘程序員。我現在只是嘗試瞭解基礎知識,但是我有一個錯誤,我不知道爲什麼。android FileInputStream崩潰

我正在創建一個具有保存和加載按鈕的活動,它使用fileOutputStreamfileInputStream來完成此任務。

我遇到的問題是,如果我第一次使用該活動時點擊加載按鈕,我的應用程序崩潰。任何人都可以幫助我如何跳過加載部分,如果該文件尚未創建?我應該在我的if語句中使用什麼。

由於一噸,這是我的代碼:

import android.app.Activity; 
import android.content.Context; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

public class InternalData extends Activity implements OnClickListener { 

    String FILENAME = "InternalString"; 
    EditText sharedData; 
    TextView dataResults; 
    FileOutputStream fos; 
    String d; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.sharedpreferences); 
     Button save = (Button) findViewById(R.id.bSave); 
     Button load = (Button) findViewById(R.id.bLoad); 
     sharedData = (EditText) findViewById(R.id.etSharedPrefs); 
     dataResults = (TextView) findViewById(R.id.tvLoadSharedPrefs); 
     save.setOnClickListener(this); 
     load.setOnClickListener(this); 
     try { 
      fos = openFileOutput(FILENAME, Context.MODE_PRIVATE); 
     } catch (FileNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     switch (v.getId()) { 
     case R.id.bSave: 
      d = sharedData.getText().toString(); 

      try { 

       fos.write(d.getBytes()); 
       fos.close(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

      break; 
     case R.id.bLoad: 
      FileInputStream fis = null; 
      try { 
       if (openFileInput(FILENAME) != null){ 
       fis = openFileInput(FILENAME); 
       byte[] data = new byte[fis.available()]; 
       while(fis.read(data) != -1){ 
        String readData = new String(data); 
        dataResults.setText(readData); 
       }} 
      } catch (FileNotFoundException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      }finally{ 
       try { 
        fis.close(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 

      break; 
     } 
    } 
} 

感謝您的幫助盧卡斯,我已經更新了我的代碼,我在想,如果你可以看看它在確保我使用的AsyncTask正常。再次感謝!

public class InternalData extends Activity implements OnClickListener { 

String FILENAME = "InternalString"; 
EditText sharedData; 
TextView dataResults; 
FileOutputStream fos; 
String d; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.sharedpreferences); 
    Button save = (Button) findViewById(R.id.bSave); 
    Button load = (Button) findViewById(R.id.bLoad); 
    sharedData = (EditText) findViewById(R.id.etSharedPrefs); 
    dataResults = (TextView) findViewById(R.id.tvLoadSharedPrefs); 
    save.setOnClickListener(this); 
    load.setOnClickListener(this); 

} 

public void onClick(View v) { 
    // TODO Auto-generated method stub 
    switch (v.getId()) { 
    case R.id.bSave: 
     d = sharedData.getText().toString(); 

     try { 
      fos = openFileOutput(FILENAME, Context.MODE_PRIVATE); 
      fos.write(d.getBytes()); 
      fos.close(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     break; 
    case R.id.bLoad: 

      AsyncTask<String, Integer, String> dat = new loadInternalData().execute(FILENAME); 

     break; 
    } 
} 

public class loadInternalData extends AsyncTask<String, Integer, String> { 

    @Override 
    protected String doInBackground(String... params) { 
     // TODO Auto-generated method stub 
     FileInputStream fis = null; 
     String collected = null; 
     try { 
      fis = openFileInput(FILENAME); 
      byte[] data = new byte[fis.available()]; 
      while (fis.read(data) != -1){ 
       collected = new String(data); 

      } 
     } catch (FileNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     }finally{ 
      try { 
       fis.close(); 
       return collected; 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
     return collected; 
    } 
    @Override 
    protected void onPostExecute(String result) 
    { 
      super.onPostExecute(result); 
      Log.i("InteralStorage", "onPostExecute(): " + result); 
      dataResults.setText(result); 

    } 
} 

}

回答

2

要調用openFileInput兩次。只需調用一次即可。

取而代之的是

if (openFileInput(FILENAME) != null){ 
    fis = openFileInput(FILENAME); 
} 

這樣做:

fis = openFileInput(FILENAME); 
if (fis != null) { 
    // Read file 
} 
1

如果執行的UI線程的東西,它不應該需要更長的時間5秒鐘或ANR將被觸發。

如果你想做的事情可能需要更長的時間,那麼這5秒鐘,你會想要做一個ServiceAsyncTask。另外,如果你的應用程序被強制關閉並且你不知道爲什麼,你應該總是看看可以在Eclipse中顯示的LogCat輸出。此外,您應該將其包含在此處詢問的每個問題(關於Android)。

+0

感謝您的幫助,我認爲我得到了它的工作,你能否檢查更新的代碼,看看我是否正確使用它? – travis

+0

儘管它起作用,但如果我在將任何內容保存到文件之前點擊加載按鈕,它仍會崩潰,所以我仍然有我最初的問題 – travis

+0

好吧,請添加LogCat輸出。 –