简体   繁体   English

如何在Android中使用Eclipse调试

[英]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. 我正在尝试使用java FileInputStream将一些字符串写入将存储在android内部存储器中的文本文件。 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. 但是,我的虚拟设备不断抛出异常,我不确定应该看什么或在哪里寻找,因为DDMS log cat函数无法提供任何有用的信息。 I am using a try/catch structure with a stack trace print as shown below. 我正在使用带有堆栈跟踪打印的try / catch结构,如下所示。 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. 我对与Android有关的debug函数不是很熟悉,我不确定还能在哪里找到发生了什么事情。 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;
import 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). 并且您的设备是droidx(当您插入USB电缆时,它将卸载外部存储设备,使其无法访问)。

Why not run the debugger and put in debug points and see how far it gets before it crashes? 为什么不运行调试器并放入调试点,看看它在崩溃前能走多远?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM