4種類の問題難易度を選択できるクイズアプリを開発中です。質問と回答は、文字列配列として string.xml に保存されます。
すべてのチェックボックスがチェックされている場合、それぞれの文字列配列が ArrayList NUM_ALL_QuestionNames に保存され、後でクイズを取得できるようになります。これは、内部の質問がさらに混合およびシャッフルされるためです。
完全なコーディングは次のとおりです。
public class Num_index extends Activity
{
String[] NUM_SIM_QuestionNames;
String[] NUM_MED_QuestionNames;
String[] NUM_DIF_QuestionNames;
String[] NUM_EXP_QuestionNames;
String[] NUM_SIM_AnswerNames;
String[] NUM_MED_AnswerNames;
String[] NUM_DIF_AnswerNames;
String[] NUM_EXP_AnswerNames;
private List<String> NUM_ALL_QuestionNames; // for quiz
private List<String> NUM_ALL_AnswerNames; // for quiz
private int QuestionImport;
private TextView CheckTextView;
private TextView AlertTextView;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main_num_index);
Button ButtonStart= (Button) findViewById(R.id.buttonStart);
ButtonStart.setOnClickListener(startButtonListener);
CheckTextView = (TextView) findViewById(R.id.checktextView);
AlertTextView = (TextView) findViewById(R.id.alerttextView);
AlertTextView.setText("Start: No alert");
NUM_SIM_QuestionNames = getResources().getStringArray(R.array.Num_Q_Simple_List);
NUM_MED_QuestionNames = getResources().getStringArray(R.array.Num_Q_Medium_List);
NUM_DIF_QuestionNames = getResources().getStringArray(R.array.Num_Q_Diff_List);
NUM_EXP_QuestionNames = getResources().getStringArray(R.array.Num_Q_Expert_List);
NUM_SIM_AnswerNames = getResources().getStringArray(R.array.Num_A_Simple_List);
NUM_MED_AnswerNames = getResources().getStringArray(R.array.Num_A_Medium_List);
NUM_DIF_AnswerNames = getResources().getStringArray(R.array.Num_A_Diff_List);
NUM_EXP_AnswerNames = getResources().getStringArray(R.array.Num_A_Expert_List);
NUM_ALL_QuestionNames = new ArrayList<String>();
NUM_ALL_AnswerNames = new ArrayList<String>();
NUM_ALL_QuestionNames.clear();
NUM_ALL_AnswerNames.clear();
};
private OnClickListener startButtonListener = new OnClickListener()
{
@Override
public void onClick(View v)
{
CheckBox CheckSim = (CheckBox) findViewById(R.id.checkBox2);
CheckBox CheckMed = (CheckBox) findViewById(R.id.checkBox3);
CheckBox CheckDif = (CheckBox) findViewById(R.id.checkBox4);
CheckBox CheckExp = (CheckBox) findViewById(R.id.checkBox5);
if (CheckSim.isChecked())
{
QuestionImport= 0;
QuestionImport = NUM_SIM_QuestionNames.length;
int i =0;
while (i<QuestionImport)
{
String Q_toimport = NUM_SIM_QuestionNames[i];
String A_toimport = NUM_SIM_AnswerNames[i];
NUM_ALL_QuestionNames.add(Q_toimport);
NUM_ALL_AnswerNames.add(A_toimport);
++i;
}
};
// same for other checkbox, not shown here for simplicity //
if ((!CheckSim.isChecked()) && (!CheckMed.isChecked()) && (!CheckDif.isChecked()) && (!CheckExp.isChecked()))
{
int k = 0;
if (NUM_ALL_QuestionNames.size() >0) {k= NUM_ALL_QuestionNames.size();}
CheckTextView.setText(k);
AlertTextView.setText("Please select at least one choice!");
}
if ((CheckSim.isChecked()) || (CheckMed.isChecked()) || (CheckDif.isChecked()) || (CheckExp.isChecked()))
{
int k = 0;
if (NUM_ALL_QuestionNames.size() >0) {k= NUM_ALL_QuestionNames.size();}
CheckTextView.setText(k);
AlertTextView.setText("Have Selection!");
}
}
};
ログキャット:
11-17 21:55:41.510: E/AndroidRuntime(12639): FATAL EXCEPTION: main
11-17 21:55:41.510: E/AndroidRuntime(12639): android.content.res.Resources$NotFoundException: String resource ID #0x0
11-17 21:55:41.510: E/AndroidRuntime(12639): at android.content.res.Resources.getText(Resources.java:260)
11-17 21:55:41.510: E/AndroidRuntime(12639): at android.widget.TextView.setText(TextView.java:3680)
11-17 21:55:41.510: E/AndroidRuntime(12639): at com.pearappx.iq_3.Num_index$1.onClick(Num_index.java:188)
11-17 21:55:41.510: E/AndroidRuntime(12639): at android.view.View.performClick(View.java:3627)
11-17 21:55:41.510: E/AndroidRuntime(12639): at android.view.View$PerformClick.run(View.java:14329)
11-17 21:55:41.510: E/AndroidRuntime(12639): at android.os.Handler.handleCallback(Handler.java:605)
11-17 21:55:41.510: E/AndroidRuntime(12639): at android.os.Handler.dispatchMessage(Handler.java:92)
11-17 21:55:41.510: E/AndroidRuntime(12639): at android.os.Looper.loop(Looper.java:137)
11-17 21:55:41.510: E/AndroidRuntime(12639): at android.app.ActivityThread.main(ActivityThread.java:4511)
11-17 21:55:41.510: E/AndroidRuntime(12639): at java.lang.reflect.Method.invokeNative(Native Method)
11-17 21:55:41.510: E/AndroidRuntime(12639): at java.lang.reflect.Method.invoke(Method.java:511)
11-17 21:55:41.510: E/AndroidRuntime(12639): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
11-17 21:55:41.510: E/AndroidRuntime(12639): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
11-17 21:55:41.510: E/AndroidRuntime(12639): at dalvik.system.NativeStart.main(Native Method)
質問:
アプリを実行すると、 CheckTextView.setText(k);の行である 188 行でエラーが発生します。
NUM_ALL_QuestionNames がすべての文字列をインポートしたかどうかを確認したいだけです。つまり、各配列に 8 つの値がある場合、CheckTextView は、4 つのレベルすべてがチェックされている場合は 32 を表示し、3 つがチェックされている場合は 24 を表示します。
しかし、188行目の問題を解決するには?どうもありがとう!!