安卓:openFileOutput抛出NullPointerException异常 [英] Android: openFileOutput throws NullPointerException

查看:258
本文介绍了安卓:openFileOutput抛出NullPointerException异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想字符串写入文件和我使用openFileOutput:

I'm trying to write Strings into a file and for that i use openFileOutput:

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

由于某些原因,应用程序崩溃是由于一个NullPointerException异常
(我看,它发生在模拟器上,而不是实际的设备,所以我迷上我的电话,你猜怎么上 - CRASHED TOO :-()

For some reason the app crashes due to a NullPointerException (I read that it happens on the emulator and not on the actual device so I hooked my phone and guess what - CRASHED TOO :-( )

这里是LogCat中输出:

here is the LogCat output:

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)

这是我的code:

here is my code:

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 活动新GenerateXml() 。无法实例活动的方式 - 他们将无法正确设置为上下文和等

Seems like you've instantiated your GenerateXml activity with new GenerateXml(). You cannot instantiate activities that way - they won't be properly set up as Contexts and so on.

从code你张贴,它看起来像 GenerateXml 在所有的不应该是一个活动。删除延伸活动,并通过在上下文如在需要的地方的参数,例如:

From the code you posted, it seems like GenerateXml should not be an Activity at all. Remove the extends Activity and pass in a Context as an argument where it's needed, for example:

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

这篇关于安卓:openFileOutput抛出NullPointerException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆