私はAndroid用のアプリケーションを作成しています。XMLファイルを読み込み、XMLからSpinnerに入れられたArrayListにデータを追加します。ここ はコードです:XMLの読み取り中にエラーが発生しました
package com.rezoluz.fsx_pocket;
import java.util.ArrayList;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import android.os.Environment;
public class Scanner extends Main {
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();
int Categories = 0;
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG && parser.getName().equals("categories")) {
Categories++;
} if (eventType == XmlPullParser.START_TAG && parser.getName().equals("category") && Categories == 1) {
Categories++;
} if (eventType == XmlPullParser.START_TAG && parser.getName().equals("name") && Categories == 2) {
categorys.add(parser.getText());
}
parser.next();
}
return categorys;
}
}
そして、これは別のクラス(Shortcuts.java)である:
public boolean launched = false;
Scanner scanner = new Scanner();
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
String selText = selection.getSelectedItem().toString(); //Finding the text of the selected item, from the Spinner
//Setting the TextView to X. START
int selected = selection.getSelectedItemPosition();
if (selected==0 && launched==true) {
selection.setSelection(prevSelection);
Context context = getApplicationContext();
final String featureToast = getString(R.string.shortcuts_notValid);
CharSequence text = featureToast;
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
launched = true;
if (selection.getSelectedItemPosition()!=0) {
prevSelection = selection.getSelectedItemPosition();
}
if (selText.equals("Please select a category")) {
Text.setText(Html.fromHtml("Please select a category from the list above."));
}
if (selText.equals("Simulator Commands")) {
Text.setText(Html.fromHtml(simulatorCommands));
}
if (selText.equals("Control Surface Commands")) {
Text.setText(Html.fromHtml(controlSurfaceCommands));
}
//END
}
私がダウンして、私は必要だと思うものにコードをカットしました。ショートカットクラスを開くと強制的に閉じます。ここ はlogcatである:ここでは
> 03-31 17:53:12.340: D/AndroidRuntime(6625): Shutting down VM 03-31
> 17:53:12.340: W/dalvikvm(6625): threadid=1: thread exiting with
> uncaught exception (group=0x4001d7e8) 03-31 17:53:12.385:
> E/AndroidRuntime(6625): FATAL EXCEPTION: main 03-31 17:53:12.385:
> E/AndroidRuntime(6625): java.lang.RuntimeException: Unable to start
> activity
> ComponentInfo{com.rezoluz.fsx_pocket/com.rezoluz.fsx_pocket.Shortcuts}:
> java.lang.NullPointerException 03-31 17:53:12.385:
> E/AndroidRuntime(6625): at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
> 03-31 17:53:12.385: E/AndroidRuntime(6625): at
> android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
> 03-31 17:53:12.385: E/AndroidRuntime(6625): at
> android.app.ActivityThread.access$2300(ActivityThread.java:125) 03-31
> 17:53:12.385: E/AndroidRuntime(6625): at
> android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
> 03-31 17:53:12.385: E/AndroidRuntime(6625): at
> android.os.Handler.dispatchMessage(Handler.java:99) 03-31
> 17:53:12.385: E/AndroidRuntime(6625): at
> android.os.Looper.loop(Looper.java:123) 03-31 17:53:12.385:
> E/AndroidRuntime(6625): at
> android.app.ActivityThread.main(ActivityThread.java:4627) 03-31
> 17:53:12.385: E/AndroidRuntime(6625): at
> java.lang.reflect.Method.invokeNative(Native Method) 03-31
> 17:53:12.385: E/AndroidRuntime(6625): at
> java.lang.reflect.Method.invoke(Method.java:521) 03-31 17:53:12.385:
> E/AndroidRuntime(6625): at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
> 03-31 17:53:12.385: E/AndroidRuntime(6625): at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 03-31
> 17:53:12.385: E/AndroidRuntime(6625): at
> dalvik.system.NativeStart.main(Native Method) 03-31 17:53:12.385:
> E/AndroidRuntime(6625): Caused by: java.lang.NullPointerException
> 03-31 17:53:12.385: E/AndroidRuntime(6625): at
> com.rezoluz.fsx_pocket.Scanner.getCategorys(Scanner.java:31) 03-31
> 17:53:12.385: E/AndroidRuntime(6625): at
> com.rezoluz.fsx_pocket.Shortcuts.onCreate(Shortcuts.java:78) 03-31
> 17:53:12.385: E/AndroidRuntime(6625): at
> android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
> 03-31 17:53:12.385: E/AndroidRuntime(6625): at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
> 03-31 17:53:12.385: E/AndroidRuntime(6625): ... 11 more
は、読みしようとしているXMLファイルです:
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<categories>
<category>
<name>Simulator Commands</name>
<id>1</id>
</category>
<category>
<name>Control Surface Commands</name>
<id>2</id>
</category>
<category>
<name>Control Surface Commands</name>
<id>3</id>
</category>
<category>
<name>General Aircraft Commands</name>
<id>4</id>
</category>
<category>
<name>Light Commands</name>
<id>5</id>
</category>
<category>
<name>Radio Commands</name>
<id>6</id>
</category>
<category>
<name>Autopilot Commands</name>
<id>7</id>
</category>
<category>
<name>Intrument Commands</name>
<id>8</id>
</category>
<category>
<name>View Commands</name>
<id>9</id>
</category>
<category>
<name>Slew Keys</name>
<id>10</id>
</category>
<category>
<name>Mission Commands</name>
<id>11</id>
</category>
<category>
<name>Multiplayer Commands</name>
<id>12</id>
</category>
<category>
<name>C172 Intrument Panels</name>
<id>13</id>
</category>
<category>
<name>BE58 Intrument Panels</name>
<id>14</id>
</category>
</categories>
お時間をありがとう、とヘルプ。 zeokila
Shortcuts.java行番号78では、その行が何を構成するのかにエラーがあります – Ishu