質問と回答を含む.txtファイルを使用して、JSONで簡単なクイズを作成しようとしています。私にとってはそれほど単純ではありません。トライアル txt ファイルには 23 の質問しかありません。とにかく、エラーが発生しています: 原因: Kviz クラスの 65 行目の java.lang.NullPointerExeption、それは「newGame();」のすぐ下にあります。. Kviz.java のコードは次のとおりです。
package rs.androidaplikacijekvizopstekulture;
import java.io.IOException;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
public class Kviz extends Activity implements View.OnClickListener{
Button bIzlazIzKviza, bOdgovor1, bOdgovor2, bOdgovor3, bOdgovor4;
TextView question;
kvizData kvizData = null;
boolean mWait = false;
private void newGame(){
try {
if(this.kvizData == null)
this.kvizData = new kvizData();
this.kvizData.randomQuestion();
this.question.setText(this.kvizData.getQuestion());
this.bOdgovor1.setText(this.kvizData.getAnswer()[0]);
this.bOdgovor2.setText(this.kvizData.getAnswer()[1]);
this.bOdgovor3.setText(this.kvizData.getAnswer()[2]);
this.bOdgovor4.setText(this.kvizData.getAnswer()[3]);
this.bOdgovor1.setEnabled(true);
this.bOdgovor2.setEnabled(true);
this.bOdgovor3.setEnabled(true);
this.bOdgovor4.setEnabled(true);
return;
} catch (IOException localIOException) {
while(true)
localIOException.printStackTrace();
}
}
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.kviz);
bIzlazIzKviza = (Button) findViewById(R.id.bIzlazIzKviza);
question = (TextView) findViewById(R.id.tvPitanje);
bOdgovor1 = (Button) findViewById(R.id.bOdgovor1);
bOdgovor2 = (Button) findViewById(R.id.bOdgovor2);
bOdgovor3 = (Button) findViewById(R.id.bOdgovor3);
bOdgovor4 = (Button) findViewById(R.id.bOdgovor4);
bOdgovor1.setOnClickListener(this);
bOdgovor2.setOnClickListener(this);
bOdgovor3.setOnClickListener(this);
bOdgovor4.setOnClickListener(this);
newGame();
bIzlazIzKviza.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish();
}
});
}
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
kvizData.java のコードは次のとおりです。
package rs.androidaplikacijekvizopstekulture;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import com.google.gson.JsonArray;
import com.google.gson.JsonParser;
public class kvizData {
static String resourceFile;
static int size = -1;
String[] answer = new String[4];
String question;
int right;
public kvizData()
{
if (size < 0);
try
{
loadResource("/rs/androidaplikacije/kvizopstekulture/data/pitanja.txt");
return;
}
catch (IOException localIOException)
{
while (true)
localIOException.printStackTrace();
}
}
private void loadResource(String paramString)
throws IOException
{
resourceFile = paramString;
InputStream localInputStream = getClass().getResourceAsStream(paramString);
BufferedReader localBufferedReader = new BufferedReader(new InputStreamReader(localInputStream, "UTF-8"));
for (size = 0; ; size = 1 + size)
{
String str;
do
{
str = localBufferedReader.readLine();
if (str == null)
{
localInputStream.close();
return;
}
}
while ((str == null) || (str.trim().length() <= 0));
}
}
public static void main(String[] paramArrayOfString)
throws IOException
{
kvizData localkvizData = new kvizData();
localkvizData.loadResource("/rs/kvizopstekulture/data/pitanja.txt");
for (int i = 0; ; i++)
{
if (i >= 23)
return;
localkvizData.randomQuestion();
System.out.println();
}
}
public String[] getAnswer()
{
return this.answer;
}
public String getQuestion()
{
return this.question;
}
public int getRight()
{
return this.right;
}
public void randomQuestion()
throws IOException
{
int i = (int)(Math.random() * size);
BufferedReader localBufferedReader = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream(resourceFile), "UTF-8"));
int j = 0;
while (true)
{
String str = localBufferedReader.readLine();
if (str == null)
return;
if ((str != null) && (str.trim().length() > 0))
{
j++;
if (j == i)
{
System.out.println(str);
JsonArray localJsonArray = new JsonParser().parse(str).getAsJsonArray();
this.question = localJsonArray.get(0).getAsString();
System.out.println(this.question);
this.right = localJsonArray.get(1).getAsInt();
this.answer[0] = localJsonArray.get(2).getAsString();
this.answer[1] = localJsonArray.get(3).getAsString();
this.answer[2] = localJsonArray.get(4).getAsString();
this.answer[3] = localJsonArray.get(5).getAsString();
}
}
}
}
public void setAnswer(String[] paramArrayOfString)
{
this.answer = paramArrayOfString;
}
public void setQuestion(String paramString)
{
this.question = paramString;
}
public void setRigth(int paramInt)
{
this.right = paramInt;
}
}