无法解决此 nullPointerException,应用程序一直停止

Can not resolve this nullPointerException, application keeps stopping

我目前正在做一个应用程序,在初始阶段,我有一个错误我不知道如何解决!我想让这个应用程序做的基本上是从我在资产文件夹 (/app/src/main/assets/tvshows.xml) 中创建的 xml 文件中检索根节点,并使用 TextView 在屏幕上显示它!我收到空指针异常,应用程序保持 "Unfortunately, stopping"。如果您能为我澄清一下,我将不胜感激。

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.access0(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.access0(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)

您收到空指针异常,因为 getDocument() 返回 null。请检查解析逻辑,因为它正在捕获并返回 null。

您不能像您在代码中提到的那样使用文件路径。

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

而是将文件放在应用程序的资产文件夹中,然后将文件复制到设备中的本地存储,然后采用该路径并尝试读取,

另外不要忘记检查清单中的读写权限。

这里:

"/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);