解析xml错误引起:java.lang.NullPointerException

时间:2013-12-17 10:48:17

标签: java xml-parsing

我的应用在模拟器中运行正常,但在我的手机中没有运行(lg nexus 4) - “我的应用停止工作”。 我的一般目的是显示url xml的问题 - 答案(数据) 在LogCat中显示以下内容:

 12-17 12:30:13.401: E/AndroidRuntime(10251): FATAL EXCEPTION: main
 12-17 12:30:13.401: E/AndroidRuntime(10251): Process: com.michalakis.schoolgame, PID: 10251
 12-17 12:30:13.401: E/AndroidRuntime(10251): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.michalakis.schoolgame/com.michalakis.schoolgame.StartGame}: java.lang.NullPointerException
 12-17 12:30:13.401: E/AndroidRuntime(10251):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 
 12-17 12:30:13.401: E/AndroidRuntime(10251):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
 12-17 12:30:13.401: E/AndroidRuntime(10251):   at android.app.ActivityThread.access$800(ActivityThread.java:135)
 12-17 12:30:13.401: E/AndroidRuntime(10251):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
 12-17 12:30:13.401: E/AndroidRuntime(10251):   at android.os.Handler.dispatchMessage(Handler.java:102)
 12-17 12:30:13.401: E/AndroidRuntime(10251):   at android.os.Looper.loop(Looper.java:136)
 12-17 12:30:13.401: E/AndroidRuntime(10251):   at android.app.ActivityThread.main(ActivityThread.java:5017)
 12-17 12:30:13.401: E/AndroidRuntime(10251):   at java.lang.reflect.Method.invokeNative(Native Method)
 12-17 12:30:13.401: E/AndroidRuntime(10251):   at java.lang.reflect.Method.invoke(Method.java:515)
 12-17 12:30:13.401: E/AndroidRuntime(10251):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
 12-17 12:30:13.401: E/AndroidRuntime(10251):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
 12-17 12:30:13.401: E/AndroidRuntime(10251):   at dalvik.system.NativeStart.main(Native Method)
 12-17 12:30:13.401: E/AndroidRuntime(10251): Caused by: java.lang.NullPointerException
 12-17 12:30:13.401: E/AndroidRuntime(10251):   at com.michalakis.schoolgame.StartGame.init(StartGame.java:75)
 12-17 12:30:13.401: E/AndroidRuntime(10251):   at com.michalakis.schoolgame.StartGame.onCreate(StartGame.java:39)
 12-17 12:30:13.401: E/AndroidRuntime(10251):   at android.app.Activity.performCreate(Activity.java:5231)
 12-17 12:30:13.401: E/AndroidRuntime(10251):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
 12-17 12:30:13.401: E/AndroidRuntime(10251):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)

我的java avtivity如下:

    import java.net.URL;

    import javax.xml.parsers.SAXParser;

import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class StartGame extends Activity {

    private int CurrentQuestion;
    private Button answerButton;
    private Button questionButton;
    private TextView questionView;
    private TextView answerView;
    private EditText answerText;
    TextView[] Question;
    TextView[] id;
    TextView[] Answer;

    /** Create Object For SiteList Class */
    SitesList sitesList = null;

    /** Called when the activity is first created. */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        init();
    }

    public void init(){

        CurrentQuestion = -1;
        answerButton = (Button)findViewById(R.id.AnswerButton);
        questionButton = (Button)findViewById(R.id.QuestionButton);
        questionView = (TextView)findViewById(R.id.QuestionTextView);
        answerView = (TextView)findViewById(R.id.AnswerTextView);
        answerText = (EditText)findViewById(R.id.AnswerText);

        try {

            /** Handling XML */
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();
            XMLReader xr = sp.getXMLReader();

            /** Send URL to parse XML Tags */
            URL sourceUrl = new URL(
                    "http://eclass.sch.gr/modules/document/file.php/T489110/quiz.xml");

            /** Create handler to handle XML Tags ( extends DefaultHandler ) */
            MyXMLHandler myXMLHandler = new MyXMLHandler();
            xr.setContentHandler(myXMLHandler);
            xr.parse(new InputSource(sourceUrl.openStream()));

        } catch (Exception e) {
            System.out.println("XML Pasing Excpetion = " + e);
        }

        /** Get result from MyXMLHandler SitlesList Object */
        sitesList = MyXMLHandler.sitesList;

        /** Assign textview array lenght by arraylist size */
        Question = new TextView[sitesList.getQuestion().size()];
        id = new TextView[sitesList.getQuestion().size()];
        Answer = new TextView[sitesList.getQuestion().size()];

        answerButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                checkAnswer();
            }});
        questionButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                showQuestion();
            }});    
    }   
           public void showQuestion(){
               CurrentQuestion++;
               if (CurrentQuestion == sitesList.getQuestion().size())
                  CurrentQuestion = 0;
               questionView.setText(sitesList.getQuestion().get(CurrentQuestion));
               answerView.setText("");
               answerText.setText("");   
           }
           public boolean isCorrect(String answer){
               return (answer.equalsIgnoreCase(sitesList.getAnswer().get(CurrentQuestion)));
           }
           public void checkAnswer(){
               String answer = answerText.getText().toString();
               if (isCorrect(answer))
                   answerView.setText("Σωστή Απάντηση");
               else 
                   answerView.setText("Λανθασμένη Απάντηση. Προσπαθείστε πάλι!!!"); 
           }   
}

main.xml如下:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#000000"
    android:orientation="vertical"
    android:padding="20dp" >

    <TextView
        android:id="@+id/QuestionTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:text=""
        android:textColor="#FFFFFF"
        android:textSize="15dp"
        android:textStyle="bold" >
    </TextView>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="100"
    android:padding="10dp" >
    <EditText
        android:id="@+id/AnswerText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:textSize="15dp"
        android:textStyle="bold"
        android:layout_weight="70"
        android:hint="Γράψτε εδώ την απάντηση σας..." >
    </EditText>

    <Button
        android:id="@+id/AnswerButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:layout_weight="30"
        android:textColor="#800000"
        android:textStyle="bold"
        android:text="Έλεγχος Απάντησης" />
    </LinearLayout>

<TextView
    android:id="@+id/AnswerTextView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:gravity="center"
    android:textSize="20dp"
    android:textColor="#00FF00"
    android:textStyle="bold"
    android:text="" />

<Button
    android:id="@+id/QuestionButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textStyle="bold"
    android:textSize="18dp"
    android:text="Δείξτε μου την ερώτηση" />

</LinearLayout>

我期待着回答 谢谢

1 个答案:

答案 0 :(得分:0)

第75行:

    Question = new TextView[sitesList.getQuestion().size()];

因此sitesList为空,或者更可能是getQuestion()返回null。