android字符串数组 - 计算项目数

时间:2012-11-17 14:44:14

标签: android arrays string checkbox

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

logcat的:

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行,即第194行     CheckTextView.setText(K);

我只想看看NUM_ALL_QuestionNames是否导入了所有字符串,即如果每个数组都有8个值,CheckTextView将显示32,如果所有4个级别都被勾选,并显示24如果3被勾选,那么上。

然而要解决188线问题?非常感谢!!

3 个答案:

答案 0 :(得分:2)

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

尝试将k转换为字符串,然后再将其传递给setText()

答案 1 :(得分:1)

您将k定义为int。您可以将String传递给setText。使用:

CheckTextView.setText(String.valueOf(k));

答案 2 :(得分:0)

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