我有一个问题,非常烦人的问题,我完全不知道是什么导致它!我正在尝试使Android应用程序能够读取XML文件,将XML文件中的一些数据添加到ArrayList,然后将ArrayList放入Spinner中。这是我的快捷方式课程的一部分。
Shortcuts.java
Spinner selection = (Spinner) findViewById(R.id.shortcutsSpinner); //Defining the Spinner
/*ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.shortcut_selection, android.R.layout.simple_spinner_item); //Defining the items in the Spinner
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
selection.setAdapter(adapter); //Setting the items to the Spinner*/
ArrayList<String> items;
try {
items = scanner.getCategorys();
} catch (FileNotFoundException e) {
items = null;
e.printStackTrace();
} catch (XmlPullParserException e) {
items = null;
e.printStackTrace();
} catch (IOException e) {
items = null;
e.printStackTrace();
}
@SuppressWarnings({ "unchecked", "rawtypes" })
ArrayAdapter<CharSequence> adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
selection.setAdapter(adapter);
selection.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
final String simulatorCommands = getString(R.string.Simulator_Commands); //Setting Shortcuts
final String controlSurfaceCommands = getString(R.string.Control_Surface_Commands);
TextView Text = (TextView)findViewById(R.id.shortcutsText); //Defining the TextView
Spinner selection = (Spinner) findViewById(R.id.shortcutsSpinner); //Defining the Spinner
这是我的Scanner课程的一部分
Scanner.java
public ArrayList<String> getCategorys() throws XmlPullParserException, IOException {
ArrayList<String> categorys = new ArrayList<String>();
categorys.add("Please select a category");
//START ADDING HERE
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser parser = factory.newPullParser();
File file = new File(Environment.getExternalStorageDirectory()+ "/fsx_kneeboard/categories.xml");
FileInputStream fis = new FileInputStream(file);
parser.setInput(new InputStreamReader(fis));
parser.next();
int eventType = parser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG && parser.getName().equals("name")) {
categorys.add(parser.getText());
}
parser.next();
eventType = parser.getEventType();
}
return categorys;
}
我可以进入Scanner活动,但是当我尝试打开Spinner时,我会收到一个强制关闭,这在logcat中。
04-09 17:30:56.452:D / AndroidRuntime(9980):关闭VM 04-09 17:30:56.452:W / dalvikvm(9980):threadid = 1:线程退出未捕获异常(组= 0x4001d7e8) 04-09 17:30:56.482:E / AndroidRuntime(9980):致命异常:主要 04-09 17:30:56.482:E / AndroidRuntime(9980):java.lang.NullPointerException 04-09 17:30:56.482:E / AndroidRuntime(9980):在android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:355) 04-09 17:30:56.482:E / AndroidRuntime(9980):在android.widget.ArrayAdapter.getDropDownView(ArrayAdapter.java:376) 04-09 17:30:56.482:E / AndroidRuntime(9980):在android.widget.Spinner $ DropDownAdapter.getDropDownView(Spinner.java:329) 04-09 17:30:56.482:E / AndroidRuntime(9980):在android.widget.Spinner $ DropDownAdapter.getView(Spinner.java:325) 04-09 17:30:56.482:E / AndroidRuntime(9980):在android.widget.AbsListView.obtainView(AbsListView.java:1294) 04-09 17:30:56.482:E / AndroidRuntime(9980):在android.widget.ListView.measureHeightOfChildren(ListView.java:1198) 04-09 17:30:56.482:E / AndroidRuntime(9980):在android.widget.ListView.onMeasure(ListView.java:1109) 04-09 17:30:56.482:E / AndroidRuntime(9980):在android.view.View.measure(View.java:8171) 04-09 17:30:56.482:E / AndroidRuntime(9980):在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132) 04-09 17:30:56.482:E / AndroidRuntime(9980):在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012) 04-09 17:30:56.482:E / AndroidRuntime(9980):在android.widget.LinearLayout.measureVertical(LinearLayout.java:381) 04-09 17:30:56.482:E / AndroidRuntime(9980):在android.widget.LinearLayout.onMeasure(LinearLayout.java:304) 04-09 17:30:56.482:E / AndroidRuntime(9980):在android.view.View.measure(View.java:8171) 04-09 17:30:56.482:E / AndroidRuntime(9980):在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132) 04-09 17:30:56.482:E / AndroidRuntime(9980):在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012) 04-09 17:30:56.482:E / AndroidRuntime(9980):在android.widget.LinearLayout.measureVertical(LinearLayout.java:381) 04-09 17:30:56.482:E / AndroidRuntime(9980):在android.widget.LinearLayout.onMeasure(LinearLayout.java:304) 04-09 17:30:56.482:E / AndroidRuntime(9980):at com.android.internal.widget.WeightedLinearLayout.onMeasure(WeightedLinearLayout.java:60) 04-09 17:30:56.482:E / AndroidRuntime(9980):在android.view.View.measure(View.java:8171) 04-09 17:30:56.482:E / AndroidRuntime(9980):在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132) 04-09 17:30:56.482:E / AndroidRuntime(9980):在android.widget.FrameLayout.onMeasure(FrameLayout.java:245) 04-09 17:30:56.482:E / AndroidRuntime(9980):在android.view.View.measure(View.java:8171) 04-09 17:30:56.482:E / AndroidRuntime(9980):在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132) 04-09 17:30:56.482:E / AndroidRuntime(9980):在android.widget.FrameLayout.onMeasure(FrameLayout.java:245) 04-09 17:30:56.482:E / AndroidRuntime(9980):在android.view.View.measure(View.java:8171) 04-09 17:30:56.482:E / AndroidRuntime(9980):在android.view.ViewRoot.performTraversals(ViewRoot.java:801) 04-09 17:30:56.482:E / AndroidRuntime(9980):在android.view.ViewRoot.handleMessage(ViewRoot.java:1727) 04-09 17:30:56.482:E / AndroidRuntime(9980):在android.os.Handler.dispatchMessage(Handler.java:99) 04-09 17:30:56.482:E / AndroidRuntime(9980):在android.os.Looper.loop(Looper.java:123) 04-09 17:30:56.482:E / AndroidRuntime(9980):在android.app.ActivityThread.main(ActivityThread.java:4627) 04-09 17:30:56.482:E / AndroidRuntime(9980):at java.lang.reflect.Method.invokeNative(Native Method) 04-09 17:30:56.482:E / AndroidRuntime(9980):at java.lang.reflect.Method.invoke(Method.java:521) 04-09 17:30:56.482:E / AndroidRuntime(9980):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:858) 04-09 17:30:56.482:E / AndroidRuntime(9980):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 04-09 17:30:56.482:E / AndroidRuntime(9980):at dalvik.system.NativeStart.main(Native Method)
我不太了解logcat,我是Java的新手。我知道问题在于这段代码;当我删除它时,Spinner会打开并显示我的工作情况&#39;请选择一个类别&#39;。
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG && parser.getName().equals("name")) {
categorys.add(parser.getText());
}
parser.next();
eventType = parser.getEventType();
}
但是那个代码对我来说似乎很干净,请帮忙吗?谢谢你的时间和帮助,我将非常感激,zeokila。
答案 0 :(得分:1)
查看javadoc parser.getName()
可能为null:
如果当前事件不是START_TAG,END_TAG或ENTITY_REF,则返回null。
您可以尝试使用此代码:"name".equals(parser.getName())
,以便在parser.getName()
为空时返回false。
int eventType = parser.getEventType();
可能应该在您的while循环中,或者在您调用parser.next();
时其值不会更新。