我想从raw文件夹中写入.txt文件。我点击其中任何一个获取NullPointer异常后,在活动上添加了12个按钮,并且不从原始文件夹中写入.txt文件。
这是代码。
public class QuestionActivity extends Activity {
RadioButton answer1;
RadioButton answer2;
RadioButton answer3;
RadioButton answer4;
RadioGroup answers;
int correctAns[];
Button finish;
private android.view.View.OnClickListener finishListener;
Button next;
Button prev;
private android.view.View.OnClickListener nextListener;
int numEvents;
private android.view.View.OnClickListener prevListener;
int quesIndex;
EditText question;
boolean review;
int selected[];
int selectedAnswer;
public QuestionActivity() {
question = null;
answer1 = null;
answer2 = null;
answer3 = null;
answer4 = null;
answers = null;
finish = null;
selectedAnswer = -1;
quesIndex = 0;
numEvents = 0;
selected = null;
correctAns = null;
review = false;
next = null;
nextListener = new android.view.View.OnClickListener() {
public void onClick(View view) {
setAnswer();
QuestionActivity questionactivity = QuestionActivity.this;
questionactivity.quesIndex = 1 + questionactivity.quesIndex;
if (quesIndex >= ComputerActivity.getQuesList().length())
{
quesIndex = -1 + ComputerActivity.getQuesList().length();
}
showQuestion(quesIndex, review);
}
};
prevListener = new android.view.View.OnClickListener() {
public void onClick(View view) {
setAnswer();
QuestionActivity questionactivity = QuestionActivity.this;
questionactivity.quesIndex = -1 + questionactivity.quesIndex;
if (quesIndex < 0)
{
quesIndex = 0;
}
showQuestion(quesIndex, review);
}
};
}
private void setAnswer() {
if (answer1.isChecked()) {
selected[quesIndex] = 0;
}
if (answer2.isChecked()) {
selected[quesIndex] = 1;
}
if (answer3.isChecked()) {
selected[quesIndex] = 2;
}
if (answer4.isChecked()) {
selected[quesIndex] = 3;
}
Log.d("", Arrays.toString(selected));
Log.d("", Arrays.toString(correctAns));
}
private void setScoreTitle() {
setTitle((new StringBuilder("Bank GK-I ")).append(1 + quesIndex)
.append("/").append(ComputerActivity.getQuesList().length())
.toString());
}
private void showQuestion(int i, boolean flag)
{
try
{
JSONObject jsonobject = ComputerActivity.getQuesList().getJSONObject(i);
String s = jsonobject.getString("Question");
if (correctAns[i] == -1)
{
String s5 = jsonobject.getString("CorrectAnswer");
correctAns[i] = Integer.parseInt(s5);
}
question.setText(s.toCharArray(), 0, s.length());
answers.check(-1);
answer1.setTextColor(-1);
answer2.setTextColor(-1);
answer3.setTextColor(-1);
answer4.setTextColor(-1);
JSONArray jsonarray = jsonobject.getJSONArray("Answers");
String s1 = jsonarray.getJSONObject(0).getString("Answer");
answer1.setText(s1.toCharArray(), 0, s1.length());
String s2 = jsonarray.getJSONObject(1).getString("Answer");
answer2.setText(s2.toCharArray(), 0, s2.length());
String s3 = jsonarray.getJSONObject(2).getString("Answer");
answer3.setText(s3.toCharArray(), 0, s3.length());
String s4 = jsonarray.getJSONObject(3).getString("Answer");
answer4.setText(s4.toCharArray(), 0, s4.length());
Log.d("",(new StringBuilder(String.valueOf(selected[i]))).toString());
if (selected[i] == 0) {
answers.check(0x7f060013);
}
if (selected[i] == 1) {
answers.check(0x7f060014);
}
if (selected[i] == 2) {
answers.check(0x7f060015);
}
if (selected[i] == 3) {
answers.check(0x7f060016);
}
setScoreTitle();
if (quesIndex == -1 + ComputerActivity.getQuesList().length()) {
next.setEnabled(false);
}
if (quesIndex == 0) {
prev.setEnabled(false);
}
if (quesIndex > 0) {
prev.setEnabled(true);
}
if (quesIndex < -1 + ComputerActivity.getQuesList().length()) {
next.setEnabled(true);
}
} catch (Exception exception) {
Log.e(getClass().toString(), exception.getMessage(),
exception.getCause());
return;
}
if (!flag) {
}
Log.d("review", (new StringBuilder(String.valueOf(selected[i])))
.append(correctAns[i]).toString());
if (selected[i] != correctAns[i]) {
if (selected[i] == 0) {
answer1.setTextColor(0xffff0000);
}
if (selected[i] == 1) {
answer2.setTextColor(0xffff0000);
}
if (selected[i] == 2) {
answer3.setTextColor(0xffff0000);
}
if (selected[i] == 3) {
answer4.setTextColor(0xffff0000);
}
}
if (correctAns[i] == 0) {
answer1.setTextColor(0xff00ff00);
}
if (correctAns[i] == 1) {
answer2.setTextColor(0xff00ff00);
}
if (correctAns[i] == 2) {
answer3.setTextColor(0xff00ff00);
}
if (correctAns[i] == 3) {
answer4.setTextColor(0xff00ff00);
}
}
public void onCreate(Bundle bundle)
{
super.onCreate(bundle);
setContentView(R.layout.question);
question = (EditText)findViewById(R.id.question);
answers = (RadioGroup)findViewById(R.id.answers);
answer1 = (RadioButton)findViewById(R.id.a0);
answer2 = (RadioButton)findViewById(R.id.a1);
answer3 = (RadioButton)findViewById(R.id.a2);
answer4 = (RadioButton)findViewById(R.id.a3);
((Button)findViewById(R.id.finish)).setOnClickListener(finishListener);
prev = (Button)findViewById(R.id.Prev);
prev.setOnClickListener(prevListener);
next = (Button)findViewById(R.id.Next);
next.setOnClickListener(nextListener);
selected = new int[GAActivity.getQuesList().length()];
Arrays.fill(selected, -1);
correctAns = new int[GAActivity.getQuesList().length()];
Arrays.fill(correctAns, -1);
showQuestion(0, review);
}
}
这是我的log cat stack trace
10-22 12:01:07.832: I/System.out(638): s = {
10-22 12:01:08.278: D/AndroidRuntime(638): Shutting down VM
10-22 12:01:08.278: W/dalvikvm(638): threadid=1: thread exiting with uncaught exception (group=0x40015560)
10-22 12:01:08.312: E/AndroidRuntime(638): FATAL EXCEPTION: main
10-22 12:01:08.312: E/AndroidRuntime(638): java.lang.RuntimeException: Unable to start activity ComponentInfo{banking_awareness.india.banking_gk/banking_awareness.india.banking_gk.QuestionActivity}: java.lang.NullPointerException
10-22 12:01:08.312: E/AndroidRuntime(638): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
10-22 12:01:08.312: E/AndroidRuntime(638): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
10-22 12:01:08.312: E/AndroidRuntime(638): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-22 12:01:08.312: E/AndroidRuntime(638): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
10-22 12:01:08.312: E/AndroidRuntime(638): at android.os.Handler.dispatchMessage(Handler.java:99)
10-22 12:01:08.312: E/AndroidRuntime(638): at android.os.Looper.loop(Looper.java:123)
10-22 12:01:08.312: E/AndroidRuntime(638): at android.app.ActivityThread.main(ActivityThread.java:3683)
10-22 12:01:08.312: E/AndroidRuntime(638): at java.lang.reflect.Method.invokeNative(Native Method)
10-22 12:01:08.312: E/AndroidRuntime(638): at java.lang.reflect.Method.invoke(Method.java:507)
10-22 12:01:08.312: E/AndroidRuntime(638): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-22 12:01:08.312: E/AndroidRuntime(638): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-22 12:01:08.312: E/AndroidRuntime(638): at dalvik.system.NativeStart.main(Native Method)
10-22 12:01:08.312: E/AndroidRuntime(638): Caused by: java.lang.NullPointerException
10-22 12:01:08.312: E/AndroidRuntime(638): at banking_awareness.india.banking_gk.QuestionActivity.onCreate(QuestionActivity.java:312)
10-22 12:01:08.312: E/AndroidRuntime(638): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-22 12:01:08.312: E/AndroidRuntime(638): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
10-22 12:01:08.312: E/AndroidRuntime(638): ... 11 more
以下是GAActivity代码
private void loadQuestions() throws Exception
{
StringBuilder stringbuilder;
bReader = new BufferedReader(new InputStreamReader(getBaseContext().getResources().openRawResource(R.raw.ga)));
stringbuilder = new StringBuilder();
String s = bReader.readLine();
if (s == null)
{
try
{
Log.d(getClass().toString(), stringbuilder.toString());
quesList = (new JSONObject(stringbuilder.toString())).getJSONArray("Questions");
Log.d(getClass().getName(), (new StringBuilder("Num Questions ")).append(quesList.length()).toString());
}
catch (Exception exception2)
{
try
{
bReader.close();
return;
}
catch (Exception exception3)
{
Log.e("", exception3.getMessage().toString(), exception3.getCause());
}
return;
}
finally { }
try
{
bReader.close();
return;
}
catch (Exception exception4)
{
Log.e("", exception4.getMessage().toString(), exception4.getCause());
}
}
stringbuilder.append(s);
try
{
bReader.close();
}
catch (Exception exception1)
{
Log.e("", exception1.getMessage().toString(), exception1.getCause());
}
}
答案 0 :(得分:2)
我认为错误位于onCreate()
方法的以下行:
((Button)findViewById(R.id.finish)).setOnClickListener(finishListener);
您没有定义setOnClickListener
正在注册的按钮。
看到你宣布了一个名为&#34;完成&#34;的按钮,将代码更改为:
finish = (Button)findViewById(R.id.finish);
finish.setOnClickListener(finishListener);
还有一件事:
据我所知,GAActivity
缺少getQuesList()
方法,因此崩溃了。
我想你会看到this代码 - 底部有一个getQuesList()
方法 - 你的代码中没有代码。