0

如果程序是第一次启动,则缓存中没有数据,因此必须从服务器(使用 asyncTask)下载数据并保存到文件中。

public static String filename = "channels.txt";

FileOutputStream fos = null;
ObjectOutputStream out = null;
try {
   fos = openFileOutput(filename, Context.MODE_APPEND);
   out = new ObjectOutputStream(fos);
   out.writeObject(ChannelManager.mInstance);
   out.close();
   fos.close();
}

主要思想是我序列化对象并将其写入文件以供以后使用。

然后我尝试阅读它:

FileInputStream fis = null;
ObjectInputStream in = null;
try {
   fis = openFileInput(filename);
   in = new ObjectInputStream(fis);
   mChannelManager = (ChannelManager) in.readObject();
   Log.e("mChannelManager", mChannelManager.getChannel_list().get(3) + "");
   in.close();
   fis.close();
 } 

如果我在写入文件后尝试读取它,它可以正常工作,但是如果我关闭或终止应用程序并尝试从文件中读取,我会得到空指针异常:/

我尝试将文件名保存到共享首选项中并使用它,但仍然出现空指针异常(认为是字符串/对象/内存引用问题)

那么问题在哪里(或可能是)?

错误日志:

10-07 10:54:15.773  10630-10647/? E/CMC->NioClient3﹕ NioClient thread get exception e: I/O exception while read message:
10-07 10:54:30.123  10741-10741/? E/APKInstallReceiver﹕ context = android.app.ReceiverRestrictedContext@41597d68, intent.getAction() = android.intent.action.PACKAGE_REMOVED, intent.getDataString() = package:com.iptviq.mobile.android.skynettv
10-07 10:54:31.393  12624-12624/com.iptviq.mobile.android.skynettv E/IN CACHE﹕ +
10-07 10:54:31.413  12624-12624/com.iptviq.mobile.android.skynettv E/Returning channel_list﹕ null
10-07 10:54:31.423  12624-12624/com.iptviq.mobile.android.skynettv E/AndroidRuntime﹕    FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.iptviq.mobile.android.skynettv/com.iptviq.mobile.android.engine.SkynetTvMainActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
        at android.app.ActivityThread.access$600(ActivityThread.java:141)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5039)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at com.iptviq.mobile.android.engine.SkynetTvMainActivity.onCreate(SkynetTvMainActivity.java:202)
        at android.app.Activity.performCreate(Activity.java:5104)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5039)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
            at dalvik.system.NativeStart.main(Native Method)

编辑2:

@Override
public void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    setContentView(R.layout.activity_main);
    activity = this;
    preferences = getSharedPreferences("settings", MODE_PRIVATE);
    SERVER_IP = preferences.getString("serverIP", "192.168.37.14");
    SERVERPORT = preferences.getString("serverPort", "8221");
    areChannelsInCache = preferences.getBoolean("areChannelsInCache", false);
    filename = preferences.getString("fileName", filename);
    if (!areChannelsInCache) {
        Log.e("NOT IN CACHE", "+");
        mDownloadChannelsTask = new DownloadChannelsTask();
        mDownloadChannelsTask.execute();
    } else {
        Log.e("IN CACHE", "+");
        FileInputStream fis = null;
        ObjectInputStream in = null;
        try {
            fis = openFileInput(filename);
            in = new ObjectInputStream(fis);
            mChannelManager = (ChannelManager) in.readObject();
            Log.e("mChannelManager", mChannelManager.getChannel_list().get(3) + "");
            in.close();
            fis.close();
        } catch (IOException ex) {
            Log.e("mChannelManager", "error1 on reading " + ex);
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            Log.e("mChannelManager", "error2 on reading " + ex);
            ex.printStackTrace();
        }

    }
}

AsyncTask 工作正常(下载数据)。完成后,它调用正在保存到文件的处理程序:

FileOutputStream fos = null;
ObjectOutputStream out = null;
try {
    fos = openFileOutput(filename, Context.MODE_APPEND);
    out = new ObjectOutputStream(fos);
    out.writeObject(ChannelManager.mInstance);
    out.close();
    fos.close();
    Log.e("SAVED TO FILE", ":)");
    preferences.edit().putBoolean("areChannelsInCache", true).commit();
    preferences.edit().putString("fileName", filename).commit();
    } catch (IOException ex) {
    Log.e("error @ main on saving data to file", ":(( " + ex);
    ex.printStackTrace();
    }
4

1 回答 1

0

在关闭之前刷新输出流

于 2013-10-07T07:53:11.790 回答