2014-04-18 93 views
0

我试图写字符串到文件和我使用openFileOutput:安卓:openFileOutput引发NullPointerException异常

FileOutputStream fOut = openFileOutput("samplefile.txt",Context.MODE_PRIVATE); 

出于某种原因,应用程序崩溃是由于一个NullPointerException (我读它发生在模拟器而不是实际的设备上,所以我迷上我的手机和你猜怎么着 - CRASHED TOO :-()

这里是logcat的输出:

04-18 22:48:25.520: E/AndroidRuntime(12045): FATAL EXCEPTION: main 
04-18 22:48:25.520: E/AndroidRuntime(12045): java.lang.NullPointerException 
04-18 22:48:25.520: E/AndroidRuntime(12045): at android.content.ContextWrapper.openFileOutput(ContextWrapper.java:165) 
04-18 22:48:25.520: E/AndroidRuntime(12045): at  com.example.tester.GenerateXml.listToTextFile(GenerateXml.java:48) 
04-18 22:48:25.520: E/AndroidRuntime(12045): at com.example.tester.MainActivity$1.parseAppListToXML(MainActivity.java:81) 
04-18 22:48:25.520: E/AndroidRuntime(12045): at com.example.tester.MainActivity$1.onClick(MainActivity.java:62) 
04-18 22:48:25.520: E/AndroidRuntime(12045): at android.view.View.performClick(View.java:3517) 
04-18 22:48:25.520: E/AndroidRuntime(12045): at android.view.View$PerformClick.run(View.java:14155) 
04-18 22:48:25.520: E/AndroidRuntime(12045): at android.os.Handler.handleCallback(Handler.java:605) 
04-18 22:48:25.520: E/AndroidRuntime(12045): at android.os.Handler.dispatchMessage(Handler.java:92) 
04-18 22:48:25.520: E/AndroidRuntime(12045): at android.os.Looper.loop(Looper.java:154) 
04-18 22:48:25.520: E/AndroidRuntime(12045): at android.app.ActivityThread.main(ActivityThread.java:4624) 
04-18 22:48:25.520: E/AndroidRuntime(12045): at java.lang.reflect.Method.invokeNative(Native Method) 
04-18 22:48:25.520: E/AndroidRuntime(12045): at java.lang.reflect.Method.invoke(Method.java:511) 
04-18 22:48:25.520: E/AndroidRuntime(12045): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809) 
04-18 22:48:25.520: E/AndroidRuntime(12045): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576) 
04-18 22:48:25.520: E/AndroidRuntime(12045): at dalvik.system.NativeStart.main(Native Method) 

这里是我的代码:

package com.example.tester; 

import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.OutputStreamWriter; 
import java.util.List; 

import android.app.Activity; 
import android.content.Context; 
import android.content.pm.ApplicationInfo; 
import android.util.Log; 
import android.util.Xml; 

public class GenerateXml extends Activity{ 

private List<ApplicationInfo> packages; 
private static final String TAG = MainActivity.class.getName(); 
private static final String FILENAME = "myFile.txt"; 

public void listToTextFile(List<ApplicationInfo> _packages) { 


    try { // catches IOException below 
     final String TESTSTRING = _packages.toString(); 

     // ##### Write a file to the disk ##### 
     /* We have to use the openFileOutput()-method 
      * the ActivityContext provides, to 
      * protect your file from others and 
      * This is done for security-reasons. 
      * We chose MODE_WORLD_READABLE, because 
      * we have nothing to hide in our file */    
     // 

     FileOutputStream fOut =  openFileOutput("samplefile.txt",Context.MODE_PRIVATE); 

     OutputStreamWriter osw = new OutputStreamWriter(fOut); 

     // Write the string to the file 
     osw.write(TESTSTRING); 
     /* ensure that everything is 
      * really written out and close */ 
     osw.flush(); 
     osw.close(); 
    }catch (IOException e){ 
     //Log.e(TAG,"could not open file out stream", e); 
    } 
} 

}

任何想法?

回答

2

好像你已经用new GenerateXml()实例化了你的GenerateXml活动。您不能以这种方式实例化活动 - 它们不会被正确设置为Context s等等。

从您发布的代码看来,GenerateXml好像根本不应该是Activity。删除extends Activity并通过Context作为它需要的参数,例如:

public void listToTextFile(Context context, List<ApplicationInfo> _packages) { 
    //... 
    ... context.openFileOutput(...); 
+0

很棒!就是这样,感谢您的帮助! –

+0

请参阅下面的我的下一个问题:-( –

相关问题