gpt4 book ai didi

java - 读取android资源文件抛出NullPointerException

转载 作者:行者123 更新时间:2023-12-02 05:16:30 26 4
gpt4 key购买 nike

我正在从事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 上扩展测试用例时,我应该使用以下方法来调用:输入流 = 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

谢谢大家!!!

最佳答案

你可以试试这个..

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;
}

关于java - 读取android资源文件抛出NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26902338/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com