无法使用ObjectOutputStream写入文件

时间:2014-12-25 12:03:07

标签: android

我正在[CodeLearn](http://www.codelearn.org/android-tutorial/twitter/9/local-storage-writing-to-file-example" Android教程")进行此练习,似乎我可能做错了什么并且无法让数组写入ObjectOutputStream 。似乎有一个空指针异常。

package com.arvisapps.aryansh.encryptit;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;


public class TweetListActivity extends ListActivity
{
    private ArrayAdapter tweetItemArrayAdapter;
    private List<Tweet> tweets;
    public Context ctx;
    public FileOutputStream fos;
    private static final String TWEETS_CACHE_FILE = "HEADER_BODY.ser";

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tweet_list);

    tweets = new ArrayList<Tweet>();
    for(int i = 0; i < 20 ; i++)
    {
        Tweet tweet = new Tweet();
        tweet.setTitle("Header #" + i);
        Log.d("Fuck Me", "Title captured " + tweet.getTitle());
        tweet.setBody("Body text #" + i);
        tweets.add(tweet);


        try
        {
            fos = ctx.openFileOutput(TWEETS_CACHE_FILE, Context.MODE_PRIVATE);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(tweets);
            Log.d("File Testing", "Successfully written to file");
            oos.close();
            fos.close();
        }

        catch (Exception e)
        {
            e.printStackTrace();
            Log.d("File Testing", "Error writing tweets", e);
        }
    }

    tweetItemArrayAdapter = new TweetAdapter(this, tweets);
    setListAdapter(tweetItemArrayAdapter);
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
    Intent intent = new Intent(this, TweetDetailActivity.class);
    startActivity(intent);
}

}

在日志中,它始终显示将推文写入文件时出错。

这是错误日志:

    12-25 17:46:20.158    6285-6285/com.arvisapps.aryansh.encryptit E/File Testing﹕ Error writing tweets
java.lang.NullPointerException
        at com.arvisapps.aryansh.encryptit.TweetListActivity.onCreate(TweetListActivity.java:43)
        at android.app.Activity.performCreate(Activity.java:5231)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2264)
        at android.app.ActivityThread.access$800(ActivityThread.java:144)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5139)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
        at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:0)

让我们看一下您的错误日志。 NullPointerException由代码的第43行调用:

fos = ctx.openFileOutput(TWEETS_CACHE_FILE, Context.MODE_PRIVATE);

正确?好。尝试另一种变体:

BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(openFileOutput(TWEETS_CACHE_FILE, MODE_PRIVATE)));
if(something!=null){
bw.write(something);
}
bw.close();  

答案 1 :(得分:0)

onCreate方法中,在setContentView(R.layout.activity_tweet_list);

之后添加以下行
ctx = TweetListActivity.this;

答案 2 :(得分:0)

fos = this.openFileOutput(TWEETS_CACHE_FILE, Context.MODE_PRIVATE);