我在下面的活动中遇到意外的令牌错误和空指针异常,我无法找出问题所在。我正在解析下面的URL,这是一个包含临时高尔夫球员详细信息的XML文件。当我在文件中有较少的节点但我现在收到导致空指针的令牌错误时,我有它工作。任何人都可以指出哪里出错了?
public class AndroidXMLParsingActivity extends ListActivity {
// All static variables
static final String URL = "http://dl.dropbox.com/u/90179278/helloworld.xml";
// XML node keys
static final String KEY_PLAYER = "player"; // parent node
static final String KEY_ID = "id";
static final String KEY_NAME = "playername";
static final String KEY_DOB = "dob";
static final String KEY_WEIGHT = "weight";
static final String KEY_HEIGHT = "height";
static final String KEY_HANDED = "handed";
static final String KEY_COUNTRY = "country";
static final String KEY_BIO = "bio";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_PLAYER);
// looping through all item nodes <item>
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
map.put(KEY_ID, parser.getValue(e, KEY_ID));
map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
map.put(KEY_DOB, parser.getValue(e, KEY_DOB));
map.put(KEY_WEIGHT, parser.getValue(e, KEY_WEIGHT));
map.put(KEY_HEIGHT, parser.getValue(e, KEY_HEIGHT));
map.put(KEY_HANDED, parser.getValue(e, KEY_HANDED));
map.put(KEY_COUNTRY, parser.getValue(e, KEY_COUNTRY));
map.put(KEY_BIO, parser.getValue(e, KEY_BIO));
// adding HashList to ArrayList
menuItems.add(map);
}
// Adding menuItems to ListView
ListAdapter adapter = new SimpleAdapter(this, menuItems,
R.layout.list_item,
new String[] { KEY_NAME, KEY_DOB, KEY_WEIGHT, KEY_HEIGHT, KEY_HANDED, KEY_COUNTRY, KEY_BIO }, new int[] {
R.id.name, R.id.dob, R.id.weight, R.id.height, R.id.handed, R.id.country, R.id.bio });
setListAdapter(adapter);
}
}
输出:
03-30 11:07:48.060:E /错误:(1742):意外的令牌(位置:TEXT@ 1:4 in java.io.StringReader@b514c300)
03-30 11:07:48.070:D / AndroidRuntime(1742):关闭虚拟机
03-30 11:07:48.070:W / dalvikvm(1742):threadid = 1:线程退出未捕获的异常(group = 0xb4e15908)
03-30 11:07:48.090:E / AndroidRuntime(1742):致命异常:主
03-30 11:07:48.090:E / AndroidRuntime(1742):java.lang.RuntimeException:无法启动活动ComponentInfo {com.androidhive.xmlparsing / com.androidhive.xmlparsing.AndroidXMLParsingActivity}:java.lang。的NullPointerException
03-30 11:07:48.090:E / AndroidRuntime(1742):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
03-30 11:07:48.090:E / AndroidRuntime(1742):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-30 11:07:48.090:E / AndroidRuntime(1742):在android.app.ActivityThread.access $ 600(ActivityThread.java:141)
03-30 11:07:48.090:E / AndroidRuntime(1742):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1234)
03-30 11:07:48.090:E / AndroidRuntime(1742):在android.os.Handler.dispatchMessage(Handler.java:99)
03-30 11:07:48.090:E / AndroidRuntime(1742):在android.os.Looper.loop(Looper.java:137) 03-30 11:07:48.090:E / AndroidRuntime(1742):在android.app.ActivityThread.main(ActivityThread.java:5039) 03-30 11:07:48.090:E / AndroidRuntime(1742):at java.lang.reflect.Method.invokeNative(Native Method) 03-30 11:07:48.090:E / AndroidRuntime(1742):at java.lang.reflect.Method.invoke(Method.java:511) 03-30 11:07:48.090:E / AndroidRuntime(1742):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)
03-30 11:07:48.090:E / AndroidRuntime(1742):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-30 11:07:48.090:E / AndroidRuntime(1742):at dalvik.system.NativeStart.main(Native Method)
03-30 11:07:48.090:E / AndroidRuntime(1742):引起:java.lang.NullPointerException
03-30 11:07:48.090:E / AndroidRuntime(1742):at com.androidhive.xmlparsing.AndroidXMLParsingActivity.onCreate(AndroidXMLParsingActivity.java:47)
03-30 11:07:48.090:E / AndroidRuntime(1742):在android.app.Activity.performCreate(Activity.java:5104)
03-30 11:07:48.090:E / AndroidRuntime(1742):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-30 11:07:48.090:E / AndroidRuntime(1742):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-30 11:07:48.090:E / AndroidRuntime(1742):... 11更多
答案 0 :(得分:0)
我发现问题是文件开头出现的BOM或“字节顺序标记”。在记事本++中,可以通过选择编码选项并从下拉菜单中选择UTF-8编码而不使用BOM选项来轻松删除它。
http://www.sitepoint.com/forums/showthread.php?592741-Byte-Order-Mark-found-in-UTF-8-File