我在android中构建了一个小应用程序。我使用XML pull解析器使用url从xml文件中检索数据。问题是,当我在模拟器上测试应用程序时它工作正常但是当我测试它时一个真实的设备,它显示应用程序已意外停止。强制关闭。这可能是什么原因?请帮助我。
08-20 16:07:30.070 E/AndroidRuntime(10349): FATAL EXCEPTION: main
08-20 16:07:30.070 E/AndroidRuntime(10349): java.lang.IllegalArgumentException
08-20 16:07:30.070 E/AndroidRuntime(10349): at org.kxml2.io.KXmlParser.setInput(KXmlParser.java:1615)
08-20 16:07:30.070 E/AndroidRuntime(10349): at com.example.androidsample4.UNIXclass.getEventsFromAnXML(UNIXclass.java:84)
08-20 16:07:30.070 E/AndroidRuntime(10349): at com.example.androidsample4.UNIXclass$1.onClick(UNIXclass.java:54)
08-20 16:07:30.070 E/AndroidRuntime(10349): at android.view.View.performClick(View.java:3131)
08-20 16:07:30.070 E/AndroidRuntime(10349): at android.view.View$PerformClick.run(View.java:12035)
08-20 16:07:30.070 E/AndroidRuntime(10349): at android.os.Handler.handleCallback(Handler.java:587)
08-20 16:07:30.070 E/AndroidRuntime(10349): at android.os.Handler.dispatchMessage(Handler.java:92)
08-20 16:07:30.070 E/AndroidRuntime(10349): at android.os.Looper.loop(Looper.java:132)
08-20 16:07:30.070 E/AndroidRuntime(10349): at android.app.ActivityThread.main(ActivityThread.java:4123)
08-20 16:07:30.070 E/AndroidRuntime(10349): at java.lang.reflect.Method.invokeNative(Native Method)
08-20 16:07:30.070 E/AndroidRuntime(10349): at java.lang.reflect.Method.invoke(Method.java:491)
08-20 16:07:30.070 E/AndroidRuntime(10349): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
08-20 16:07:30.070 E/AndroidRuntime(10349): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
08-20 16:07:30.070 E/AndroidRuntime(10349): at dalvik.system.NativeStart.main(Native Method)
08-20 16:07:30.080 W/ActivityManager( 2900): Force finishing activity com.example.androidsample4/.UNIXclass
在logcat中获取此内容
package com.example.androidsample4;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class UNIXclass extends Activity
{
String unixcommands[];
AutoCompleteTextView actv1;
static final String URL="http://cympac.com/apps/xmlfile.xml";
static final String KEY_COMMAND = "command"; // parent node
static final String KEY_WORD = "word";
static final String KEY_EXPLANATION = "explanation";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv1=(TextView)findViewById(R.id.textView1);
AutoCompleteTextView actv=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
Button b1=(Button)findViewById(R.id.button1);
unixcommands=getResources().getStringArray(R.array.Commands);
ArrayAdapter<String> adapter =new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_list_item_1,unixcommands);
actv1 = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
actv1.setThreshold(1);
actv1.setAdapter(adapter);
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
TextView myXmlContent = (TextView)findViewById(R.id.textView2);
String stringXmlContent = null;
try {
stringXmlContent = getEventsFromAnXML(getBaseContext()); // 54 line
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
Toast.makeText(getBaseContext(), "error:"+e.getMessage(), Toast.LENGTH_SHORT).show();
} catch (IOException e) {
// TODO Auto-generated catch block
Toast.makeText(getBaseContext(), "error:"+e.getMessage(), Toast.LENGTH_SHORT).show();
}
myXmlContent.setText(stringXmlContent);
}
});
}
public String getEventsFromAnXML(Context baseContext) throws XmlPullParserException,IOException
{
StringBuffer stringBuffer=new StringBuffer();
java.net.URL url=new java.net.URL("http://cympac.com/apps/xmlfile.xml");
XmlPullParserFactory factory=XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp=factory.newPullParser();
xpp.setInput(getInputStream(url),"UTF_8"); //84 line
int eventType = xpp.getEventType();
String word = null;
String tag;
while ((eventType = xpp.next()) != XmlPullParser.END_DOCUMENT)
{
if(XmlPullParser.START_TAG==eventType)
{
tag=xpp.getName();
//stringBuffer.append("\n"+tag);
if (tag.equals("word"))
{
eventType = xpp.next();
word = xpp.getText();
}
else if (tag.equals("explanation"))
{
eventType = xpp.next();
//if ("cancel".equals(word))
if(actv1.getText().toString().equals(word))
{
stringBuffer.append("\n" + xpp.getText());
}
}
}
}
return stringBuffer.toString();
}
这是我的代码。我收到IllegalArgument Exception
public InputStream getInputStream(java.net.URL url2)
{
try
{
return url2.openConnection().getInputStream();
}
catch(Exception e)
{
return null;
}
}
}
答案 0 :(得分:0)
如果它只在您的设备上崩溃,将异常消息放入字符串值并将其显示在警报中会很有帮助。
我遇到了同样的问题,它让我知道设备上出了什么问题。
答案 1 :(得分:0)
在为解析器提供XML数据时,您提供了非法参数。根据来源,您传递一个空值。
请参阅此主题:Execute "doInBackground()" showing IllegalArgumentException
向我们展示将XML传递给KXmlParser.setInput方法的行。
编辑:您的getInputStream()
返回null,因此解析器会抛出IllegalArgumentException。在使用之前记录url2的值,以查看它是否为null。你也可以尝试直接传递流,如另一个问题解决方案所述。
xpp.setInput(getInputStream(url), "UTF_8");
要
xpp.setInput(url.openConnection().getInputStream(), "UTF_8");