我正在为一个android类做一个项目。在这个特定的屏幕上,当它尝试加载时,它会崩溃。我清理了一个空指针异常,但现在我遇到了一个新错误,无法弄清楚。我还没有完成对活动的编码,此时只是读取 xml 文件并尝试让其中一个标签填充微调器......这是该活动的代码:
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天。所有其他屏幕工作正常。任何帮助将不胜感激。谢谢!!