在android上创建一个文件

时间:2011-03-25 23:40:53

标签: java android nullpointerexception android-context fileoutputstream

我正在尝试使用我的Android应用程序创建一个文件。我需要写一个特定类的文件。我目前拥有的代码如下所示。我在尝试写入文件时不断获得nullpointerexception。确切的错误如下所示。我是android的新手,所以请详细说明。

有人可以告诉我我做错了什么以及如何解决这个问题?


//the class where i need to create and write to the file
public class DataRobot {
    Context tThis;
    FileOutputStream fOut;
    OutputStreamWriter writer;

    public DataRobot(SmartApp smartApp) extends Activity {
        tThis = (Context) smartApp;
    }
    public void onCreate(Bundle savedInstanceState) {
        try {
            //file = new File("/sdcard", "test.csv");
            fOut = openFileOutput("test.csv", MODE_WORLD_READABLE);
            writer = new OutputStreamWriter(fOut);
        } catch (NullPointerException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void analyzeData(SmartDataObject temp) {
        data = temp;
        try {
                //the following line is where the error is occurring.
            writer.write(Double.toString(data.getHeartRate()));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
     }
}   

//the error
03-26 03:47:53.924: WARN/System.err(273): java.lang.NullPointerException
03-26 03:47:53.934: WARN/System.err(273):     at cpe495.smartapp.DataRobot.analyzeData(DataRobot.java:80)
03-26 03:47:53.946: WARN/System.err(273):     at cpe495.smartapp.SmartApp$1.dataReceivedReceived(SmartApp.java:49)
03-26 03:47:53.956: WARN/System.err(273):     at cpe495.smartapp.ConnectDevice.fireDataReceivedEvent(ConnectDevice.java:79)
03-26 03:47:53.956: WARN/System.err(273):     at cpe495.smartapp.ConnectDevice.run(ConnectDevice.java:46)
03-26 03:47:53.965: WARN/System.err(273):     at java.lang.Thread.run(Thread.java:1096)

3 个答案:

答案 0 :(得分:3)

看起来你在构造函数中这样做了。你不能这样做。您需要等到onCreate(),这时您知道该对象已被初始化。

答案 1 :(得分:0)

你应该通过内容提供商访问文件,我认为路径是错误的......看看这个file tutorial

答案 2 :(得分:0)

以下代码是我想出来的并且正在发挥作用。对不起,发帖花了这么长时间。


public class DataRobot {
    Context tThis; //was enabled for preferences
    private FileOutputStream fOut;
    private OutputStreamWriter writer;

    public DataRobot(SmartApp smartApp) {
        tThis = (Context) smartApp;

    }

    public void analyzeData(SmartDataObject temp) {
        try {
            fOut = tThis.openFileOutput("test.csv", tThis.MODE_APPEND);
            writer = new OutputStreamWriter(fOut);
        } catch (NullPointerException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            writer.write("Test");//0
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}