1

我在手机上本地保存和读取 ArrayList。当我尝试打开文件时出现 NullPointerException。但我找不到问题。

ArrayList 被全局定义为private static ArrayList<Datapoint> XML = null;private static ArrayList<Datapoint> itemsList = null;

保存文件的代码:

//save arraylist as file 
            //change path to a non-root folder
            String filePath = context.getFilesDir().getPath().toString() + FileSeperator + Name;
            Log.d("FILESTREAMonFolder", "Filepath:"+filePath+ " Projektname:"+Name);
            File f = new File(filePath);
            try {
                FileOutputStream fileOut = new FileOutputStream(f);
                ObjectOutputStream out = new ObjectOutputStream(fileOut);
                out.writeObject(itemsList);
                out.close();
                fileOut.close();
            } catch(IOException ioe) {
                ioe.printStackTrace();
            }       

读取文件的代码:

//read ArrayList which is saved local
//change path to non-root folder
String filePath = context.getFilesDir().getPath().toString()+"/"+Name;
Log.d("FILESTREAMfromFolder", "Filepath:"+filePath+ " Projektname:"+Name);
File f = new File(filePath);
try
{
    FileInputStream fileIn = new FileInputStream(f);
    ObjectInputStream in = new ObjectInputStream(fileIn);
    XML = (ArrayList<Datapoint>) in.readObject();
    Log.d("XML",XML.toString());
    in.close();
    fileIn.close();
} catch(IOException ioe)
{
    ioe.printStackTrace();
    return;
} catch(ClassNotFoundException c)
{
    Log.d("FILESTREAM", ".Datapoint class not found.");
    c.printStackTrace();
    return;
}

LogCat 输出:

10-24 17:19:51.210: D/FILESTREAMonFolder(15446): Filepath:/data/data/de.myapp./files/test Projektname:test
10-24 17:19:51.325: D/AbsListView(15446): Get MotionRecognitionManager
10-24 17:19:53.625: D/CREATED_FILE(15446): test.xml
10-24 17:20:04.110: I/System.out(15446): ..Tunneling, NAT ignored, TP1 medium
10-24 17:20:04.110: I/System.out(15446): ..Local  EP:connect:Exception = null
10-24 17:20:04.110: I/System.out(15446): Can not connect to KNX/IP-Router
10-24 17:20:04.140: D/dalvikvm(15446): GC_CONCURRENT freed 382K, 5% free 14931K/15559K, paused 2ms+6ms
10-24 17:20:04.145: D/AbsListView(15446): [unregisterDoubleTapMotionListener]
10-24 17:20:04.145: I/MotionRecognitionManager(15446):   .unregisterListener : / listener count = 0->0, listener=android.widget.AbsListView$4@418fe150
10-24 17:20:04.195: D/AbsListView(15446): Get MotionRecognitionManager
10-24 17:20:04.200: D/FILESTREAMfromFolder(15446): Filepath:/data/data/de.bertrandt.bertrandtknx/files/test Projektname:test
10-24 17:20:04.200: D/AndroidRuntime(15446): Shutting down VM
10-24 17:20:04.200: W/dalvikvm(15446): threadid=1: thread exiting with uncaught exception (group=0x40c5d1f8)
10-24 17:20:04.205: E/AndroidRuntime(15446): FATAL EXCEPTION: main
10-24 17:20:04.205: E/AndroidRuntime(15446): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.myapp./de.myapp.ProjectView}: java.lang.NullPointerException
10-24 17:20:04.205: E/AndroidRuntime(15446):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
10-24 17:20:04.205: E/AndroidRuntime(15446):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
10-24 17:20:04.205: E/AndroidRuntime(15446):    at android.app.ActivityThread.access$600(ActivityThread.java:128)
10-24 17:20:04.205: E/AndroidRuntime(15446):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
10-24 17:20:04.205: E/AndroidRuntime(15446):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-24 17:20:04.205: E/AndroidRuntime(15446):    at android.os.Looper.loop(Looper.java:137)
10-24 17:20:04.205: E/AndroidRuntime(15446):    at android.app.ActivityThread.main(ActivityThread.java:4514)
10-24 17:20:04.205: E/AndroidRuntime(15446):    at java.lang.reflect.Method.invokeNative(Native Method)
10-24 17:20:04.205: E/AndroidRuntime(15446):    at java.lang.reflect.Method.invoke(Method.java:511)
10-24 17:20:04.205: E/AndroidRuntime(15446):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
10-24 17:20:04.205: E/AndroidRuntime(15446):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
10-24 17:20:04.205: E/AndroidRuntime(15446):    at dalvik.system.NativeStart.main(Native Method)
10-24 17:20:04.205: E/AndroidRuntime(15446): Caused by: java.lang.NullPointerException
10-24 17:20:04.205: E/AndroidRuntime(15446):    at de.myapp.ProjectView.setup(ProjectView.java:220)
10-24 17:20:04.205: E/AndroidRuntime(15446):    at de.myapp.ProjectView.onCreate(ProjectView.java:85)
10-24 17:20:04.205: E/AndroidRuntime(15446):    at android.app.Activity.performCreate(Activity.java:4562)
10-24 17:20:04.205: E/AndroidRuntime(15446):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
10-24 17:20:04.205: E/AndroidRuntime(15446):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
10-24 17:20:04.205: E/AndroidRuntime(15446):    ... 11 more
4

1 回答 1

0

我必须将写入代码放在我的 AsyncTask 的 OnPostExecute() 方法中以下载文件,现在它可以工作了!

于 2012-10-30T07:42:10.300 回答