读取android资源文件抛出NullPointerException

时间:2014-11-13 06:02:19

标签: java android robotium android-assets

我正在进行android自动化测试,我在eclipse中创建了一个测试项目。 (在自动化中,它将被打包为apk并部署到模拟器上) 在我的项目中,我想阅读 assets 文件夹中的xml文件。我将我的xml文件“mytest.xml”直接放在文件夹 assets 中。我想加载它并解析。但似乎我总是会得到NullPointerException。 以下是我的代码

1.该函数在OSNCommonLib类中定义。

public class OSNCommonLib extends Activity {
    public  String readTranslationFile(String fileName,String transunitId) 
    {
        if(doc == null)
            {
                try
                    {
                    InputStream in = getAssets().open(fileName);
                    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
                    DocumentBuilder builder = factory.newDocumentBuilder(); 
                    doc = builder.parse(in);

                    }

                    catch ( Exception e )
                    {
                        System.out.println("mytest catch" + e.toString() );
                        e.printStackTrace();
                    }
                }


                org.w3c.dom.Element root = doc.getDocumentElement();
                NodeList nodes = root.getElementsByTagName( "trans-unit" );
            .......
                }
                ....
    }  

上面的函数将在另一个java类中调用

public class TC01_OSNdemo extends ActivityInstrumentationTestCase2 {
OSNCommonLib ocl = new OSNCommonLib();
...
public void testSortByButton(){

    ....
        String getstr = ocl.readTranslationFile("mytest","osn_menu_sortby");
        Assert.assertTrue(solo.searchText(getstr));
    ...
}

在catch中,我将始终获得java.lang.NullPointerException。 这是否意味着它无法加载我的文件? 我尝试了很多次但仍然出现同样的错误。

有人可以就此问题提出一些建议吗?

此外:这是完整的日志

java.lang.NullPointerException
at com.stg.osn.lib.OSNCommonLib.readTranslationFile(OSNCommonLib.java:107) --> **point to org.w3c.dom.Element root = doc.getDocumentElement();**

at com.sgt.osn.junit.test.TC01_OSNdemo.testSortByButton(TC01_OSNdemo.java:142)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)
at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:192)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1661)

最后,问题得到了解决。当我在Instruments上扩展我的测试用例时,我应该使用以下方法来调用: InputStream in = getInstrumentation()。getContext()。getAssets()。open(“mytest.xml”); 有用的链接 http://developer.android.com/reference/android/test/InstrumentationTestCase.html http://www.mail-archive.com/android-developers@googlegroups.com/msg62671.html

感谢所有人!!!

2 个答案:

答案 0 :(得分:1)

你可以试试这个..

public String readDataFromAssets(String file) {
    try {
      InputStream is = getAssets().open(file);
      BufferedReader reader = new BufferedReader(new InputStreamReader(is));
      StringBuilder builder = new StringBuilder();

      String line = null;
      try {
        while ((line = reader.readLine()) != null) {
          builder.append(line);
          // builder.append("\n"); // append a new line
        }
      } catch (IOException e) {

        e.printStackTrace();
      } finally {
        try {
          is.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }

      return builder.toString();      

    } catch (IOException e) {

      e.printStackTrace();
    }
    return null;
  }

答案 1 :(得分:1)

您应该能够使用R - 对象访问该文件。这意味着您将XML文件放在RAW文件夹中。然后,您只需编写

即可访问该文件
  

InputStream inputStream =   。activity.getResources()openRawResource(R.raw.mytest);