开发者

How to use eclipse debug in android

I am attempting to use java FileInputStream to write some strings to a text file that will be stored on the android internal storage. However my virtual device keeps throwing an exception and I am not sure what or where I should be looking as the DDMS log cat function does not give me any useful information. I am using a try/catch structure with a stack trace print as shown below. I am not very familiar with the debug function in relation to android and I am not sure where else I can look to find out what is going on. Code is below.

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import android.app.Activity;
impor开发者_开发问答t android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
    private EditText textBox;
    private static final int READ_BLOCK_SIZE = 100;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        textBox = (EditText)findViewById(R.id.textView1);        

        Button saveBtn = (Button)findViewById(R.id.button1);
        Button loadBtn  = (Button)findViewById(R.id.button2);

        saveBtn.setOnClickListener(new View.OnClickListener() {         
            public void onClick(View v) {
                String str = textBox.getText().toString();
                try{
                    FileOutputStream fOut =
                        openFileOutput("textfile.txt", MODE_WORLD_READABLE);
                    OutputStreamWriter osw = new OutputStreamWriter(fOut);

                    //---write the string to the file---
                    osw.write(str);
                    osw.flush();
                    osw.close();

                    //---display file saved message---
                    Toast.makeText(getBaseContext(), "File saved successfully!!", Toast.LENGTH_SHORT).show();

                    //---clears the EditText---
                    textBox.setText("");

                }catch(IOException ioe){
                    ioe.printStackTrace();
                }
            }
        });

        loadBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                try{
                    FileInputStream fIn = openFileInput("textfile.txt");
                    InputStreamReader isr = new InputStreamReader(fIn);

                    char[]inputBuffer = new char[READ_BLOCK_SIZE];
                    String s = "";

                    int charRead;
                    while((charRead = isr.read(inputBuffer))>0){

                        //---convert the char to a String---
                        String readString = String.copyValueOf(inputBuffer, 0, charRead);
                        s += readString;

                        inputBuffer = new char[READ_BLOCK_SIZE];
                    }
                    //---set the EditText to the text that has been read---
                    textBox.setText(s);

                    Toast.makeText(getBaseContext(), "File loaded successfully!!", Toast.LENGTH_SHORT).show();
                }catch(IOException ioe){
                    ioe.printStackTrace();
                }
            }
        });
    }
}


did you set your permissions in your manifest for writing? and is your device a droidx (which when you plug in the USB cable, unmounts the external storage, making it inaccessible).

Why not run the debugger and put in debug points and see how far it gets before it crashes?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜