Note that there are some explanatory texts on larger screens.

plurals
  1. POandroid FileInputStream crashes
    text
    copied!<p>I am an amateur programmer developing for android. I am just trying get the basics down right now, but I am having an error and I don't know why.</p> <p>I am creating a activity that has a save and a load button, which using the <code>fileOutputStream</code> and <code>fileInputStream</code> to achieve this task.</p> <p>The problem I am having is if I hit the load button the first time I use the activity, my application crashes. Can anyone help me with how to skip the load section if the file hasn't been created yet? What should I use within my if statement. </p> <p>Thanks a ton, here is my code:</p> <pre><code>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; } } } </code></pre> <p>Thanks for the help Lukas, I have updated my code, and I was wondering if you could look it over to make sure I am using the AsyncTask properly. Thanks again!</p> <pre><code>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&lt;String, Integer, String&gt; dat = new loadInternalData().execute(FILENAME); break; } } public class loadInternalData extends AsyncTask&lt;String, Integer, String&gt; { @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 ); } } </code></pre> <p>}</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload