0
每當我使用com4j庫打開一個活動時,我都會崩潰。這是錯誤代碼:無法實例化活動ComponentInfo空指針異常
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
這是正確的。創建類時,佈局和視圖尚未初始化,您必須等到'onCreate()'或'onStart()'能夠以任何方式使用它們。 –