在安卓上创建一个文件

发布于 2024-10-26 21:56:01 字数 1977 浏览 6 评论 0原文

我正在尝试使用我的 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)

I am trying to create a file with my android application. I need to write to the file in a specific class. The code I currently have for it is listed below. I keep getting a nullpointerexception when it is trying to write to the file. The exact error is listed below. I am new to android, so please be detailed.

Can someone please tell me what I am doing wrong and how i can fix this issue?


//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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

So尛奶瓶 2024-11-02 21:56:01

看起来您正在构造函数中执行此操作。你不能那样做。您需要等到 onCreate(),此时您知道该对象已初始化。

It looks like you are doing this in your constructor. You can't do that. You need to wait until onCreate(), which is when you know the object has been initialized.

冷心人i 2024-11-02 21:56:01

您应该通过内容提供程序访问文件,我认为路径错误...看看这个文件教程< /a>

You should acces files via content providers, I think the path is wrong... look at this file tutorial

听,心雨的声音 2024-11-02 21:56:01

以下代码是我想到的并且正在运行。抱歉花了这么长时间才发帖。


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();
        }
    }
}

The following code is what i figured out and is working. Sorry it took so long to post.


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();
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文