0

我正在做一个测验,其中需要将问题和答案列表从活动 1 传递到活动 2。提取的相关编码如下:

活动一:Num_index.java

   String[] NUM_ALLL_QuestionNames; 
   String[] NUM_ALLL_AnswerNames;   

   @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);

      Button ButtonBackkk= (Button) findViewById(R.id.buttonBackkk);

      CheckTextView = (TextView) findViewById(R.id.checktextView); 

      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>();                              
   };

   private OnClickListener startButtonListener = new OnClickListener() 
   {
      @Override
      public void onClick(View v) 
      {
          CheckBox CheckSim = (CheckBox) findViewById(R.id.checkBox2);
          // other similar checkBox omitted here for simplicity // 

          NUM_ALL_QuestionNames.clear();
          NUM_ALL_AnswerNames.clear();  

          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;                    
              }           
          };    

         // other similar checkBox omitted 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(String.valueOf(k));

                AlertDialog.Builder builder = new AlertDialog.Builder(Num_index.this);
                builder.setTitle("Error");
                builder.setMessage("Please select at least one choice!!");
                builder.setCancelable(false);
                builder.setPositiveButton("OK", null);                 
                AlertDialog ErrorDialog = builder.create();
                ErrorDialog.show();                   
          }

          if ((CheckSim.isChecked()) || (CheckMed.isChecked()) || (CheckDif.isChecked()) || (CheckExp.isChecked()))  
          {
              NUM_ALLL_QuestionNames = new String[NUM_ALL_QuestionNames.size()]; //convert ArrayList<String> to String[]
              NUM_ALLL_AnswerNames = new String[NUM_ALL_AnswerNames.size()]; //convert ArrayList<String> to String[]

              Intent senddata = new Intent (Num_index.this, Num.class); //transmit the list to num.java
              Bundle bundle = new Bundle();
              bundle.putStringArray("dataQ",NUM_ALLL_QuestionNames);
              bundle.putStringArray("dataA",NUM_ALLL_AnswerNames);
              senddata.putExtras(bundle);
              startActivity(senddata);                            

              int k = 0;
              if (NUM_ALLL_QuestionNames.length >0) {k= NUM_ALLL_QuestionNames.length;}
              CheckTextView.setText(String.valueOf(k));                                   
          }           

           Intent intent = new Intent (Num_index.this, Num.class);
           startActivity(intent);
           Num_index.this.finish();               

      } // end method onClick
   }; // end OnClickListener    

活动二:Num.java

   private String[] NUM_ALL_QuestionNames;
   private String[] NUM_ALL_AnswerNames;

   @Override
   public void onCreate(Bundle savedInstanceState) 
   {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main_num); 

      //get bundle from Num_index.java
      Bundle bundle = this.getIntent().getExtras();
      if (bundle!= null)
      {
          NUM_ALL_QuestionNames = bundle.getStringArray("dataQ");
          NUM_ALL_AnswerNames = bundle.getStringArray("dataA");          
      }
      else
      {
          NUM_ALL_QuestionNames = getResources().getStringArray(R.array.Num_Q_Simple_List);
          NUM_ALL_AnswerNames = getResources().getStringArray(R.array.Num_A_Simple_List);             
      }

日志猫:

11-18 20:26:20.905: E/AndroidRuntime(5463): FATAL EXCEPTION: main
11-18 20:26:20.905: E/AndroidRuntime(5463): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pearappx.iq_3/com.pearappx.iq_3.Num}: java.lang.NullPointerException
11-18 20:26:20.905: E/AndroidRuntime(5463):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
11-18 20:26:20.905: E/AndroidRuntime(5463):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
11-18 20:26:20.905: E/AndroidRuntime(5463):     at android.app.ActivityThread.access$600(ActivityThread.java:127)
11-18 20:26:20.905: E/AndroidRuntime(5463):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
11-18 20:26:20.905: E/AndroidRuntime(5463):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-18 20:26:20.905: E/AndroidRuntime(5463):     at android.os.Looper.loop(Looper.java:137)
11-18 20:26:20.905: E/AndroidRuntime(5463):     at android.app.ActivityThread.main(ActivityThread.java:4511)
11-18 20:26:20.905: E/AndroidRuntime(5463):     at java.lang.reflect.Method.invokeNative(Native Method)
11-18 20:26:20.905: E/AndroidRuntime(5463):     at java.lang.reflect.Method.invoke(Method.java:511)
11-18 20:26:20.905: E/AndroidRuntime(5463):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
11-18 20:26:20.905: E/AndroidRuntime(5463):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
11-18 20:26:20.905: E/AndroidRuntime(5463):     at dalvik.system.NativeStart.main(Native Method)
11-18 20:26:20.905: E/AndroidRuntime(5463): Caused by: java.lang.NullPointerException
11-18 20:26:20.905: E/AndroidRuntime(5463):     at com.pearappx.iq_3.Num.onCreate(Num.java:73)
11-18 20:26:20.905: E/AndroidRuntime(5463):     at android.app.Activity.performCreate(Activity.java:4470)
11-18 20:26:20.905: E/AndroidRuntime(5463):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
11-18 20:26:20.905: E/AndroidRuntime(5463):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)

第 73 行是

NUM_ALL_QuestionNames = bundle.getStringArray("dataQ"); 

这意味着 StringArray 未正确存储在 Activity1 中或无法在 Activity2 中正确获取。它显示 NullPointerException。

问题:

我已经通过这个网站搜索过这个网站,比如String back from Bundle,以及如何将 selectedListItem 的对象传递给另一个活动?并按照描述的方法进行操作,但仍然失败。但为什么是 NullPointerException?如何解决这个问题?

非常感谢!!

PS:在活动1中我有

              if (NUM_ALLL_QuestionNames.length >0) {k= NUM_ALLL_QuestionNames.length;}
              CheckTextView.setText(String.valueOf(k)); 

k 给出正确数量的项目。这意味着 NUM_ALLL_QuestionNames 不为空。

4

2 回答 2

4

http://developer.android.com/guide/components/activities.html#StartingAnActivity

您应该以您的第二个意图发送您的数据包。并完全删除第一个意图。

于 2012-11-18T12:50:44.717 回答
2

就像在您的当前代码中一样,您正在启动Num.classActivity 两次,第一次使用startActivity(senddata); ,第二次 startActivity(intent);不包含捆绑对象,这就是您得到的原因NullPointerException

于 2012-11-18T12:49:14.453 回答