-2

我写了如下代码但无法理解为什么代码返回空指针异常?

public void createXML()
{
    try
    {
        //FileOutputStream f1 = new FileOutputStream("Userdata_Boombastic.xml");
        FileOutputStream f1 = openFileOutput("Userdata_Boombastic.xml", Context.MODE_PRIVATE);
        //OutputStreamWriter out = new OutputStreamWriter(f1);
        XmlSerializer xmlSerializer = Xml.newSerializer();              
        StringWriter writer = new StringWriter();
        xmlSerializer.setOutput(writer);
        xmlSerializer.startDocument("UTF-8",true);
        xmlSerializer.endDocument();
        xmlSerializer.flush();
        String dataWrite=writer.toString();
        f1.write(dataWrite.getBytes());
        f1.close();
    }
    /*catch (FileNotFoundException e)
    {
    // TODO Auto-generated catch block
        Log.e("FileNotFoundException", e.toString());
        e.printStackTrace();
    }*/
    catch (IllegalArgumentException e)
    {
    // TODO Auto-generated catch block
        Log.e("~~IllegalArgumentException~~", e.toString());
        e.printStackTrace();
    } 
    catch (IllegalStateException e) 
    {
    // TODO Auto-generated catch block
        Log.e("~~IllegalStateException~~", e.toString());
        e.printStackTrace();
    }
    /*catch (IOException e)
    {
    // TODO Auto-generated catch block
        Log.e("IOEXCEPTION", e.toString());
        e.printStackTrace();
    }*/
    catch (Exception e) 
    {
    // TODO Auto-generated catch block
        Log.e("~~Exception~~", e.toString());
        e.printStackTrace();
    }

08-27 18:50:50.310: E/~~Exception~~(31487): java.lang.NullPointerException
08-27 18:50:57.800: E/~~Exception~~(31487): java.lang.NullPointerException
08-27 18:51:00.430: E/~~Exception~~(31487): java.lang.NullPointerException
08-27 18:53:28.050: E/ExternalAccountType(30234): Unsupported attribute readOnly
08-27 18:53:29.680: E/ExternalAccountType(30234): Unsupported attribute readOnly
08-27 18:53:32.500: E/~~Exception~~(32054): java.lang.NullPointerException
08-27 18:53:51.670: E/~~Exception~~(32054): java.lang.NullPointerException

同时,对于其他解释,我通过将其连接到 pc 在我的单元上运行此代码。

请帮忙

好吧,我猜堆栈跟踪没有返回任何富有成果的东西

08-27 19:30:31.330: E/ExternalAccountType(30234): Unsupported attribute readOnly
08-27 19:30:31.820: E/ExternalAccountType(30234): Unsupported attribute readOnly
08-27 19:30:36.030: E/~~Exception~~(2732): java.lang.NullPointerException
08-27 19:30:36.150: E/->>(2732): ~~stacktrace~~
08-27 19:30:36.150: E/->>(2732): java.lang.NullPointerException
08-27 19:30:36.150: E/->>(2732):    at           android.content.ContextWrapper.openFileOutput(ContextWrapper.java:165)
08-27 19:30:36.150: E/->>(2732):    at com.example.boombastic.WritingXML.createXML(WritingXML.java:76)
08-27 19:30:36.150: E/->>(2732):    at com.example.boombastic.BoombasticPlayer.onCreate(BoombasticPlayer.java:22)
08-27 19:30:36.150: E/->>(2732):    at android.app.Activity.performCreate(Activity.java:4470)
08-27 19:30:36.150: E/->>(2732):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
08-27 19:30:36.150: E/->>(2732):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
08-27 19:30:36.150: E/->>(2732):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
08-27 19:30:36.150: E/->>(2732):    at android.app.ActivityThread.access$600(ActivityThread.java:123)
08-27 19:30:36.150: E/->>(2732):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
08-27 19:30:36.150: E/->>(2732):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-27 19:30:36.150: E/->>(2732):    at android.os.Looper.loop(Looper.java:137)
08-27 19:30:36.150: E/->>(2732):    at android.app.ActivityThread.main(ActivityThread.java:4424)
08-27 19:30:36.150: E/->>(2732):    at java.lang.reflect.Method.invokeNative(Native Method)
08-27 19:30:36.150: E/->>(2732):    at java.lang.reflect.Method.invoke(Method.java:511)
08-27 19:30:36.150: E/->>(2732):    at    com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:817)
08-27 19:30:36.150: E/->>(2732):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
08-27 19:30:36.150: E/->>(2732):    at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

0

stacktrace 很有用,当然有用。

当您调用openFileOutput()时,您的代码在 WritingXML.java ( com.example.boombastic.WritingXML.createXML(WritingXML.java:76) ) 的第 76 行中断。问题是,为什么在 openFileOutput() 处会出现 NullPointerException?也许这个线程可以帮助你: NullPointerException at openFileOutput in Activity

此外,最好在 finally{} 块中关闭 FileOutputStream(以及任何流),以确保在打开后出现任何异常时关闭它。

于 2013-08-27T14:11:30.517 回答
0

第 76 行是该方法中的错误:

    FileOutputStream f1 = openFileOutput("Userdata_Boombastic.xml", Context.MODE_PRIVATE);

我假设这个类扩展了活动,因此您不需要使用上下文引用该方法。

但是,如果您查看堆栈跟踪,则说明上下文包装器为空,因此即使您的代码编译,您也可能在没有可用上下文的情况下调用此方法。我建议做的是,如果您从不同的类调用此方法,则将 Context 作为参数传递并通过以下方式调用 openFileOutput 方法:

FileOutputStream f1 = ctx.openFileOutput("Userdata_Boombastic.xml", Context.MODE_PRIVATE);
于 2013-08-27T14:19:26.473 回答