gpt4 book ai didi

java - Android:openFileOutput 抛出 NullPointerException

转载 作者:行者123 更新时间:2023-12-02 05:55:13 25 4
gpt4 key购买 nike

我正在尝试将字符串写入文件,为此我使用 openFileOutput:

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

由于某种原因,应用程序因 NullPointerException 而崩溃(我读到它发生在模拟器上,而不是在实际设备上,所以我挂上了手机,猜猜看是什么 - 也崩溃了:-( )

这是 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);
}
}

}

有什么想法吗?

最佳答案

似乎您已经使用 newGenerateXml() 实例化了 GenerateXml Activity 。您无法以这种方式实例化 Activity - 它们将无法正确设置为Context等。

从您发布的代码来看,GenerateXml 似乎根本不应该是Activity。删除 extends Activity 并在需要时传入 Context 作为参数,例如:

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

关于java - Android:openFileOutput 抛出 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23161519/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com