2013-08-27 53 views
-2

我寫了如下代碼,但不能理解爲什麼代碼返回空指針異常?不能理解Android應用中的空指針異常

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上運行我的手機這段代碼的意思。

請幫

那麼堆棧跟蹤不返回任何FRUITFULL我猜

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) 
+0

那是什麼下降投票?沒有任何回覆 – user2714061

+1

你可以顯示堆棧跟蹤嗎? – krsteeve

+0

你可以展示完整的'WritingXML'類嗎?我想,它確實擴展了'Activity'或'Context',並且沒有被正確初始化,因爲它沒有被系統啓動。 – Henry

回答

0

堆棧跟蹤是有用的,當然是。

您的代碼處斷裂WritingXML.java的76線(com.example.boombastic.WritingXML.createXML(WritingXML.java:76)),當你調用openFileOutput()。問題是,爲什麼你在openFileOutput()會得到一個NullPointerException?也許這個線程可以幫助你: NullPointerException at openFileOutput in Activity

另外,關閉FileOutputStream(以及其他任何流)在finally {}塊中是一個很好的習慣,以確保在打開它之後有任何異常時關閉它。

0

線76是該方法中的錯誤:

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

我假設這個類,所以你不需要與上下文來引用方法擴展活動。

如果您查看堆棧跟蹤,則說明上下文封裝器爲空,所以即使您的代碼編譯,您也可能在沒有可用上下文的情況下調用此方法。我會建議做的是,如果你從不同的類調用這個方法,你在上下文作爲參數傳遞,並通過調用openFileOutput方法:

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

是的,它延伸服務,使可能的使用openFileOutput。 – user2714061

+0

你是否建議我創建一個服務對象,然後調用它 – user2714061

+0

那麼你可以詳細說明這一點,因爲我嘗試通過服務類的對象調用方法,但沒有任何更改它仍然給出nullpointerexception。 – user2714061