私はアンドロイドクラスのプロジェクトに取り組んでいます。この特定の画面で、ロードしようとするとクラッシュします。1 つのヌル ポインター例外をクリーンアップしましたが、新しいエラーが発生し、それを理解できません。アクティビティのコーディングはまだ終わっていません。この時点で xml ファイルを読み込んで、タグの 1 つにスピナーを設定しようとしているだけです。そのアクティビティのコードは次のとおりです。
public class PlayGeoTreasureActivity extends Activity {
String[] treasureList=null;
String[] clue1List=null;
String[] clue2List=null;
String[] clue3List=null;
String[] answerList=null;
String[] locationList=null;
String[] pointValueList=null;
XmlPullParserFactory parser;
XmlPullParser xpp;
Spinner spinnerTreasures;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play_geo_treasure);
spinnerTreasures = (Spinner)findViewById(R.id.treasuresSpinner);
//get the xml file
File filename = new File(getFilesDir(), "treasure.xml");
//check to see if file exists. If it does, read it.
try {
if(filename.exists())
{
readXML(filename);
}
else
{
Toast.makeText(null, "File not Found", Toast.LENGTH_LONG).show();
}
}catch (FileNotFoundException e) {
String errorMessage=(e.getMessage()==null)?"Message is Empty":e.getMessage();
Log.e("GeoTreasureGameLog",errorMessage);
e.printStackTrace();
} catch (XmlPullParserException e) {
String errMessage=(e.getMessage()==null)?"Message is Empty":e.getMessage();
Log.e("GeoTreasureGameLog",errMessage);
e.printStackTrace();
}
}
private void readXML(File filename) throws XmlPullParserException, FileNotFoundException {
// pull parser to read xml file
parser = XmlPullParserFactory.newInstance();
xpp = parser.newPullParser();
// point the xml parser to file
xpp.setInput(new FileReader(filename));
// get start and end tags
int eventType = xpp.getEventType();
// set current tag
String currentTag;
// current value of the tag's element
String currentElement;
//int counter = 0;
try{
// parse the entire xml file until done
while (eventType != XmlPullParser.END_DOCUMENT)
{
// look for start tags
if(eventType == XmlPullParser.START_TAG)
{
// get the name of the start tag
currentTag = xpp.getName();
if (currentTag.equals("TreasureName"))
{
currentElement = xpp.nextText();
treasureList.equals(currentElement);
}
else if (currentTag.equals("ClueOne"))
{
currentElement = xpp.nextText();
clue1List.equals(currentElement);
}
else if (currentTag.equals("ClueTwo"))
{
currentElement = xpp.nextText();
clue2List.equals(currentElement);
}
else if (currentTag.equals("ClueThree"))
{
currentElement = xpp.nextText();
clue3List.equals(currentElement);
}
else if (currentTag.equals("Answer"))
{
currentElement = xpp.nextText();
answerList.equals(currentElement);
}
else if (currentTag.equals("TreasureLocation"))
{
currentElement = xpp.nextText();
locationList.equals(currentElement);
}
else if (currentTag.equals("PointValue"))
{
currentElement = xpp.nextText();
pointValueList.equals(currentElement);
}
}
eventType = xpp.next();
}
} catch (Exception e)
{
Log.e("GeoTreasureGameLog", e.getMessage());
}
Spinner spinner = new Spinner(this);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item,treasureList);
spinner.setAdapter(spinnerArrayAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.play_geo_treasure, menu);
return true;
}
そしてエラーメッセージ: (新しいポインター例外)
09-17 09:13:24.125: E/AndroidRuntime(3816): FATAL EXCEPTION: main
09-17 09:13:24.125: E/AndroidRuntime(3816): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rasmussen.geotreasuresgame/com.rasmussen.geotreasuresgame.PlayGeoTreasureActivity}: java.lang.NullPointerException: println needs a message
09-17 09:13:24.125: E/AndroidRuntime(3816): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
09-17 09:13:24.125: E/AndroidRuntime(3816): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
09-17 09:13:24.125: E/AndroidRuntime(3816): at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-17 09:13:24.125: E/AndroidRuntime(3816): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
09-17 09:13:24.125: E/AndroidRuntime(3816): at android.os.Handler.dispatchMessage(Handler.java:99)
09-17 09:13:24.125: E/AndroidRuntime(3816): at android.os.Looper.loop(Looper.java:137)
09-17 09:13:24.125: E/AndroidRuntime(3816): at android.app.ActivityThread.main(ActivityThread.java:5103)
09-17 09:13:24.125: E/AndroidRuntime(3816): at java.lang.reflect.Method.invokeNative(Native Method)
09-17 09:13:24.125: E/AndroidRuntime(3816): at java.lang.reflect.Method.invoke(Method.java:525)
09-17 09:13:24.125: E/AndroidRuntime(3816): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-17 09:13:24.125: E/AndroidRuntime(3816): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-17 09:13:24.125: E/AndroidRuntime(3816): at dalvik.system.NativeStart.main(Native Method)
09-17 09:13:24.125: E/AndroidRuntime(3816): Caused by: java.lang.NullPointerException: println needs a message
09-17 09:13:24.125: E/AndroidRuntime(3816): at android.util.Log.println_native(Native Method)
09-17 09:13:24.125: E/AndroidRuntime(3816): at android.util.Log.e(Log.java:231)
09-17 09:13:24.125: E/AndroidRuntime(3816): at com.rasmussen.geotreasuresgame.PlayGeoTreasureActivity.readXML(PlayGeoTreasureActivity.java:137)
09-17 09:13:24.125: E/AndroidRuntime(3816): at com.rasmussen.geotreasuresgame.PlayGeoTreasureActivity.onCreate(PlayGeoTreasureActivity.java:49)
09-17 09:13:24.125: E/AndroidRuntime(3816): at android.app.Activity.performCreate(Activity.java:5133)
09-17 09:13:24.125: E/AndroidRuntime(3816): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-17 09:13:24.125: E/AndroidRuntime(3816): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
09-17 09:13:24.125: E/AndroidRuntime(3816): ... 11 more
レイアウトの xml は次のとおりです。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".PlayGeoTreasureActivity" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/treasuresSpinner"
android:layout_alignRight="@+id/treasuresSpinner"
android:contentDescription="@string/cameraShot"
android:src="@drawable/ic_launcher" />
<Spinner
android:id="@+id/treasuresSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView1"
android:layout_centerHorizontal="true" />
<TextView
android:id="@+id/guessLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/treasuresSpinner"
android:layout_below="@+id/getClueBtn"
android:layout_marginTop="48dp"
android:text="@string/guessLblTxt"
android:textStyle="bold" />
<EditText
android:id="@+id/guessEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/guessLabel"
android:layout_alignLeft="@+id/getClueBtn"
android:ems="10"
android:inputType="text" />
<Button
android:id="@+id/foundBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/guessEditText"
android:layout_alignRight="@+id/getClueBtn"
android:layout_below="@+id/guessEditText"
android:layout_marginTop="34dp"
android:text="@string/foundBtnTxt" />
<TextView
android:id="@+id/pointsLbl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/guessLabel"
android:layout_below="@+id/foundBtn"
android:layout_marginTop="28dp"
android:text="@string/pointsEarnedLblTxt"
android:textStyle="bold" />
<EditText
android:id="@+id/pointEarnedEditTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/pointsLbl"
android:layout_alignBottom="@+id/pointsLbl"
android:layout_alignParentRight="true"
android:layout_toRightOf="@+id/foundBtn"
android:ems="10"
android:inputType="number"
android:textStyle="bold" />
<Button
android:id="@+id/getClueBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/treasuresSpinner"
android:layout_marginLeft="29dp"
android:layout_marginTop="28dp"
android:layout_toRightOf="@+id/guessLabel"
android:text="@string/getClueBtnTxt" />
</RelativeLayout>
私はこれに3日間苦労しています。他のすべての画面は正常に動作しています。どんな助けでも大歓迎です。ありがとうございました!!