我正在尝试将字符串写入文件,为此我使用openFileOutput:
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);
}
}
}
有任何想法吗?
最佳答案
似乎您已经用GenerateXml
实例化了new GenerateXml()
活动。您无法以这种方式实例化活动-无法将其正确设置为Context
等。
从您发布的代码看来,GenerateXml
根本不应该是Activity
。删除extends Activity
并在需要的地方传递Context
作为参数,例如:
public void listToTextFile(Context context, List<ApplicationInfo> _packages) {
//...
... context.openFileOutput(...);