2015-02-06 21 views
-1

我目前正在研究一個應用程序,在初始階段,我有一個錯誤,我不知道如何解決!我希望這個應用程序做的事情基本上是從我在資產文件夾(/app/src/main/assets/tvshows.xml)中創建的xml文件中檢索根節點,並使用TextView將其顯示在屏幕上!我得到一個空指針異常,並且應用程序保持「不幸停止」。如果你能爲我澄清這一點,我會非常感激。無法解決此nullPointerException,應用程序始終停止

import android.os.Bundle; 
import android.support.v7.app.ActionBarActivity; 
import android.widget.TextView; 
import org.w3c.dom.Document; 
import org.xml.sax.InputSource; 
import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 


public class MainActivity extends ActionBarActivity { 

    TextView printText; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

       Document xmlDoc = getDocument("/app/src/main/res/TvShows/tvshows.xml"); 
       TextView printText = (TextView) findViewById(R.id.printText); 
       printText.setText(xmlDoc.getDocumentElement().getNodeName()); 


      } 

      private Document getDocument(String domDoc) { 
       try { 
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
        factory.setIgnoringComments(true); 
        factory.setIgnoringElementContentWhitespace(true); 
        factory.setValidating(true); 

        DocumentBuilder builder = factory.newDocumentBuilder(); 
        AssetManager assetManager = this.getAssets(); 
       InputStream is = assetManager.open("tvshows.xml"); 

       InputSource inStream = new InputSource(is); 
       return builder.parse(inStream); 
       } 
       catch(Exception e) { 
        TextView printText = (TextView) findViewById(R.id.printText); 
        printText.setText(e.getMessage()); 
        return null; 
       } 
      } 



    } 

錯誤信息:

02-06 15:59:08.271 1973-1973/com.example.ashwinpraveen1.domdoc E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ashwinpraveen1.domdoc/com.example.ashwinpraveen1.domdoc.MainActivity}: java.lang.NullPointerException 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2223) 
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2273) 
      at android.app.ActivityThread.access$600(ActivityThread.java:144) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1259) 
      at android.os.Handler.dispatchMessage(Handler.java:99) 
      at android.os.Looper.loop(Looper.java:137) 
      at android.app.ActivityThread.main(ActivityThread.java:5145) 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:525) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
      at dalvik.system.NativeStart.main(Native Method) 
    Caused by: java.lang.NullPointerException 
      at com.example.ashwinpraveen1.domdoc.MainActivity.onCreate(MainActivity.java:28) 
      at android.app.Activity.performCreate(Activity.java:5133) 
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2273) 
            at android.app.ActivityThread.access$600(ActivityThread.java:144) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1259) 
            at android.os.Handler.dispatchMessage(Handler.java:99) 
            at android.os.Looper.loop(Looper.java:137) 
            at android.app.ActivityThread.main(ActivityThread.java:5145) 
            at java.lang.reflect.Method.invokeNative(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:525) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
            at dalvik.system.NativeStart.main(Native Method) 
+2

後的錯誤消息。 – Simas 2015-02-06 09:42:57

+0

@ user3249477是的,我很抱歉。 這些是錯誤信息: – 2015-02-06 09:49:19

回答

1

這裏:

"/app/src/main/res/TvShows/tvshows.xml" 

res是不添加像JS,CSS,HTML,JSON或XML文件正確的地方。

放在assets所有文件要在應用程序中獲取數據:

AssetManager assetManager = this.getAssets(); 
InputStream is = assetManager.open("tvshows.xml"); 
.... 
InputSource inStream = new InputSource(is); 
return builder.parse(inStream); 
+0

謝謝。:)我會試試這個。 – 2015-02-06 10:01:54

+0

我已經按照您的建議進行了修改併發布了上面的代碼,但我仍然無法運行它。有什麼建議麼? – 2015-02-06 10:35:06

+0

@AshwinPraveen:MainActivity.java中的行號是什麼? – 2015-02-06 10:36:24

0

你得到空指針異常的getDocument()方法返回null。請檢查解析邏輯,因爲它正在捕獲並返回null。

0

您不能使用代碼中提到的文件路徑。

Document xmlDoc = getDocument("/app/src/main/res/TvShows/tvshows.xml"); 
//This is wrong way. 

而是放在應用&的資產文件夾中的文件,然後將這些文件到本地存儲複製設備,然後採取這一路徑,並嘗試讀取,

也不要忘了檢查閱讀&在清單中寫入權限。

+0

請問您可能會發布代碼嗎?我是新來的! – 2015-02-06 09:52:51

相關問題