无法实例化活动ComponentInfo Null Pointer Exception

时间:2013-10-08 21:16:23

标签: java android xml crash

每当我使用com4j库打开活动时,我都会崩溃。这是错误代码:

10-08 16:10:32.615: E/AndroidRuntime(18210): FATAL EXCEPTION: main
10-08 16:10:32.615: E/AndroidRuntime(18210): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.twotowersstudios.quizmaker/com.twotowersstudios.quizmaker.MakeQuizActivity}: java.lang.NullPointerException
10-08 16:10:32.615: E/AndroidRuntime(18210):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2024)
10-08 16:10:32.615: E/AndroidRuntime(18210):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
10-08 16:10:32.615: E/AndroidRuntime(18210):    at android.app.ActivityThread.access$600(ActivityThread.java:140)
10-08 16:10:32.615: E/AndroidRuntime(18210):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
10-08 16:10:32.615: E/AndroidRuntime(18210):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-08 16:10:32.615: E/AndroidRuntime(18210):    at android.os.Looper.loop(Looper.java:137)
10-08 16:10:32.615: E/AndroidRuntime(18210):    at android.app.ActivityThread.main(ActivityThread.java:4898)
10-08 16:10:32.615: E/AndroidRuntime(18210):    at java.lang.reflect.Method.invokeNative(Native Method)
10-08 16:10:32.615: E/AndroidRuntime(18210):    at java.lang.reflect.Method.invoke(Method.java:511)
10-08 16:10:32.615: E/AndroidRuntime(18210):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
10-08 16:10:32.615: E/AndroidRuntime(18210):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
10-08 16:10:32.615: E/AndroidRuntime(18210):    at dalvik.system.NativeStart.main(Native Method)
10-08 16:10:32.615: E/AndroidRuntime(18210): Caused by: java.lang.NullPointerException
10-08 16:10:32.615: E/AndroidRuntime(18210):    at android.app.Activity.findViewById(Activity.java:1882)
10-08 16:10:32.615: E/AndroidRuntime(18210):    at com.twotowersstudios.quizmaker.MakeQuizActivity.<init>(MakeQuizActivity.java:44)
10-08 16:10:32.615: E/AndroidRuntime(18210):    at java.lang.Class.newInstanceImpl(Native Method)
10-08 16:10:32.615: E/AndroidRuntime(18210):    at java.lang.Class.newInstance(Class.java:1319)
10-08 16:10:32.615: E/AndroidRuntime(18210):    at android.app.Instrumentation.newActivity(Instrumentation.java:1057)
10-08 16:10:32.615: E/AndroidRuntime(18210):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2015)
10-08 16:10:32.615: E/AndroidRuntime(18210):    ... 11 more

这是崩溃的类:

package com.twotowersstudios.quizmaker;

import java.io.FileWriter;
import java.io.IOException;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.XMLWriter;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;

public class MakeQuizActivity extends Activity {
    /**String filename = "Question1.xml";
    String string = "";
    FileOutputStream outputStream;
     * @return */
    private RadioButton JRQ1A1;
    private RadioButton JRQ1A2;
    private RadioButton JRQ1A3;
    private RadioButton JRQ1A4;
    private String rightAnswerValue;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_make_quiz);

        }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.make_quiz, menu);
        return true;
    }
    EditText Q1 = (EditText)findViewById(R.id.Q1Actual);
    EditText Q1A1 = (EditText)findViewById(R.id.Q1A1);
    EditText Q1A2 = (EditText)findViewById(R.id.Q1A2);
    EditText Q1A3 = (EditText)findViewById(R.id.Q1A3);
    EditText Q1A4 = (EditText)findViewById(R.id.Q1A4);
    String Q1Str = Q1.getText().toString();
    String Q1A1Str = Q1A1.getText().toString();
    String Q1A2Str = Q1A2.getText().toString();
    String Q1A3Str = Q1A3.getText().toString();
    String Q1A4Str = Q1A4.getText().toString();

    public void rightAnswerSelection (View view){
        if (JRQ1A1.isChecked()){
             rightAnswerValue = "@id/Q1A1";
        }else if (JRQ1A2.isChecked()){
             rightAnswerValue = "@id/Q1A2";
        }else if (JRQ1A3.isChecked()){
             rightAnswerValue = "@id/Q1A3";
        }else if (JRQ1A4.isChecked()){
             rightAnswerValue = "@id/Q1A4";
        }
    }
    public void createXML() throws IOException {
        if (JRQ1A1.isChecked()){
             rightAnswerValue = "@id/Q1A1";
        }else if (JRQ1A2.isChecked()){
             rightAnswerValue = "@id/Q1A2";
        }else if (JRQ1A3.isChecked()){
             rightAnswerValue = "@id/Q1A3";
        }else if (JRQ1A4.isChecked()){
             rightAnswerValue = "@id/Q1A4";
        }
        Document document = DocumentHelper.createDocument();
        Element rootElement = document.addElement("Questions");
        Element studentElement = rootElement.addElement("Question1").addAttribute("rightanswer", rightAnswerValue);
        studentElement.addElement("Q1").addText(Q1Str);
        studentElement.addElement("Q1A1").addText(Q1A1Str);
        studentElement.addElement("Q1A2").addText(Q1A2Str);
        studentElement.addElement("Q1A3").addText(Q1A3Str);
        studentElement.addElement("Q1A4").addText(Q1A4Str);
        studentElement.addElement("name").addText("Peter");
        XMLWriter writer = new XMLWriter(new FileWriter("Students.xml"));
        //Note that You can format this XML document
        /*
         * FileWriter output = new FileWriter(new File("Students.xml"));
        OutputFormat format = OutputFormat.createPrettyPrint();
        XMLWriter writer = new XMLWriter(output,format);<- will fomat the output
         */

        //You can print this to the console and see what it looks like
        String xmlElement = document.asXML();
        System.out.println(xmlElement);
        writer.write(document);
        writer.close();

    }

}

Android清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.twotowersstudios.quizmaker"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="18" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <activity
            android:name="com.twotowersstudios.quizmaker.MainTitleScreen"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.twotowersstudios.quizmaker.ReportABugActivity"
            android:label="@string/title_activity_report_abug"
            android:parentActivityName="com.twotowersstudios.quizmaker.MainTitleScreen" >
        </activity>
        <activity
            android:name="com.twotowersstudios.quizmaker.MakeQuizActivity"
            android:label="@string/title_activity_make_quiz"
            android:parentActivityName="com.twotowersstudios.quizmaker.MainTitleScreen" >
        </activity>
    </application>

</manifest>

最后,整个项目:https://github.com/coldblade2000/QuizMaker

1 个答案:

答案 0 :(得分:2)

我不是Android开发人员,但我猜您必须移动代码:

EditText Q1 = (EditText)findViewById(R.id.Q1Actual);
EditText Q1A1 = (EditText)findViewById(R.id.Q1A1);
EditText Q1A2 = (EditText)findViewById(R.id.Q1A2);
EditText Q1A3 = (EditText)findViewById(R.id.Q1A3);
EditText Q1A4 = (EditText)findViewById(R.id.Q1A4);
String Q1Str = Q1.getText().toString();
String Q1A1Str = Q1A1.getText().toString();
String Q1A2Str = Q1A2.getText().toString();
String Q1A3Str = Q1A3.getText().toString();
String Q1A4Str = Q1A4.getText().toString();

在方法内部例如,在onCreate()