我的问题更多的是"我在这里做错了什么?"我试图导入一个我从W3.schools中获取的示例XML文档。我已经尝试过DOM来获取一个可解析的文件,BufferStream(新的FileBuilder(新文件(xxx))将它变成一个字符串,然后偶然发现XPullParser,试图将文档解析为工作元素.I&#39用maven和DOM完成了这个,但Android看起来有点挑剔。我在整个stackoverflow和其他文档中看到了所有内容,但我似乎无法找到答案。
以下是我所拥有的:
在MainActivity中初始化对象
public void OnClickDisplayResults(View view) {
final Button button = (Button) findViewById(R.id.button3);
button.setOnClickListener(new View.OnClickListener() {
public void onClick (View v) {
TextView textView3 = (TextView) findViewById(R.id.textView3);
textView3.setText(searchResults.initiateParsing(), TextView.BufferType.EDITABLE);
}
});
}
工厂
public String initiateParsing() {
String result = "";
XmlPullParserFactory pullParserFactory;
try {
pullParserFactory = XmlPullParserFactory.newInstance();
XmlPullParser parser = pullParserFactory.newPullParser();
InputStream in_s = getApplicationContext().getAssets().open("plant_catalog.xml");
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
parser.setInput(in_s, null);
parseXML(parser);
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return libInterface.getXmlResult();
}
private void parseXML(XmlPullParser parser) throws XmlPullParserException,IOException
{
ArrayList<SearchResults> products = null;
int eventType = parser.getEventType();
SearchResults currentProduct = null;
while (eventType != XmlPullParser.END_DOCUMENT){
String name = null;
switch (eventType){
case XmlPullParser.START_DOCUMENT:
products = new ArrayList();
break;
case XmlPullParser.START_TAG:
name = parser.getName();
if (name.equals("PLANT")){
currentProduct = new SearchResults();
} else if (currentProduct != null){
if (name.equals("COMMON")){
currentProduct.name = parser.nextText();
} else if (name.equals("BOTANICAL")){
currentProduct.color = parser.nextText();
} else if (name.equals("PRICE")){
currentProduct.quantity= parser.nextText();
}
}
break;
case XmlPullParser.END_TAG:
name = parser.getName();
try {
if (name.equalsIgnoreCase("PLANT") && currentProduct != null) {
products.add(currentProduct);
}
}catch (Exception e) {
e.printStackTrace();
}
}
eventType = parser.next();
}
printProducts(products);
}
private void printProducts(ArrayList<SearchResults> products)
{
String content = "";
Iterator<SearchResults> it = products.iterator();
while(it.hasNext())
{
SearchResults currProduct = it.next();
content = content + "nnnProduct :" + currProduct.name + "n";
content = content + "Quantity :" + currProduct.quantity + "n";
content = content + "Color :" + currProduct.color + "n";
}
libInterface.setXmlResult(content);
}
Stacktrace
Process: com.example.myapp, PID: 7441
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:105)
at com.app.StateLibraryApplication.SearchResults.initiateParsing(SearchResults.java:48)
at com.app.StateLibraryApplication.MyActivity$3.onClick(MyActivity.java:54)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5256)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
02-22 13:00:24.527 7441-7441/com.example.myapp I/Process﹕ Sending signal. PID: 7441 SIG: 9
答案 0 :(得分:1)
这就是我如何解决自己的问题
我读到了关于上下文。
上下文会抛出大部分错误。
您必须将方法更改为:
public String initiateParsing(Context context) {
String result = "";
FileInputStream fis = null;
XmlPullParserFactory pullParserFactory;
try {
pullParserFactory = XmlPullParserFactory.newInstance();
XmlPullParser parser = pullParserFactory.newPullParser();
// uses Context to getApplicationContext().getResources();
Resources res = context.getApplicationContext().getResources();
// Use direct Resource Tag with R
InputStream in_st = res.openRawResource(R.raw.plant_catalog);
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
parser.setInput(in_st, null);
parseXML(parser); // and so forth.....
主要是方法:thisIsMyMethod(Context context){context.SOMETHING}
希望这能为其他人节省6个小时的研究时间。