0

我正在开发测验应用程序,用户可以选择 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)

问题:

执行应用程序时,在第 188 行出现错误,即
CheckTextView.setText(k) 的行;

我只想看看 NUM_ALL_QuestionNames 是否已经导入了所有字符串,即如果每个数组都有 8 个值,如果勾选了所有 4 个级别,CheckTextView 将显示 32,如果勾选了 3,则显示 24,依此类推。

但是要解决188行的问题呢?非常感谢!!

4

3 回答 3

2

int尝试将一个传递给“setText()”函数时,我也遇到了这个错误。

k在将其传递给之前尝试转换为字符串setText()

于 2012-11-17T14:48:15.020 回答
1

您将 k 定义为 int。您可以将字符串传递给 setText。采用:

CheckTextView.setText(String.valueOf(k));
于 2012-11-17T14:49:55.660 回答
0

当您使用时,CheckTextView.setText(k);您正在调用设置资源 ID 的方法。相反,您希望传入一个表示整数值的字符串。试试.setText("" + k)

于 2012-11-17T14:51:14.927 回答