Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android:openFileOutput抛出NullPointerException_Java_Android_Eclipse_Android Intent_Nullpointerexception - Fatal编程技术网

Java Android:openFileOutput抛出NullPointerException

Java Android:openFileOutput抛出NullPointerException,java,android,eclipse,android-intent,nullpointerexception,Java,Android,Eclipse,Android Intent,Nullpointerexception,我试图将字符串写入文件,为此我使用openFileOutput: FileOutputStream fOut = openFileOutput("samplefile.txt",Context.MODE_PRIVATE); 由于某种原因,应用程序因NullPointerException而崩溃 (我读到它发生在模拟器上,而不是在实际设备上,所以我挂上我的手机,猜猜是什么-也崩溃了:-() 以下是LogCat输出: 04-18 22:48:25.520: E/AndroidRuntime(1

我试图将字符串写入文件,为此我使用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); 
     }
}
package com.example.tester;
导入java.io.FileOutputStream;
导入java.io.IOException;
导入java.io.OutputStreamWriter;
导入java.util.List;
导入android.app.Activity;
导入android.content.Context;
导入android.content.pm.ApplicationInfo;
导入android.util.Log;
导入android.util.Xml;
公共类GenerateXml扩展活动{
私有列表包;
私有静态最终字符串标记=MainActivity.class.getName();
私有静态最终字符串FILENAME=“myFile.txt”;
公共无效listToTextFile(列表_包){
请尝试下面的{//IOException
最终字符串TESTSTRING=_packages.toString();
//将文件写入磁盘#####
/*我们必须使用openFileOutput()方法
*ActivityContext提供了
*保护您的文件不受其他人和
*这样做是出于安全原因。
*我们选择了世界可读模式,因为
*我们的文件中没有要隐藏的内容*/
//   
FileOutputStream fOut=openFileOutput(“samplefile.txt”,Context.MODE\u PRIVATE);
OutputStreamWriter osw=新的OutputStreamWriter(fOut);
//将字符串写入文件
write(TESTSTRING);
/*确保一切正常
*真的写出来了,结束了*/
osw.flush();
osw.close();
}捕获(IOE异常){
//Log.e(标记“无法打开文件输出流”,e);
}
}
}


有什么想法吗?

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

从您发布的代码来看,似乎
GenerateXml
根本不应该是
活动
。请删除
扩展活动
,并在需要时将
上下文
作为参数传递,例如:

public void listToTextFile(Context context, List<ApplicationInfo> _packages) {
    //...
    ... context.openFileOutput(...);
public void listToTextFile(上下文,列表包){
//...
…context.openFileOutput(…);

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