无法在意图中部署数据

时间:2015-10-16 06:09:12

标签: android android-intent

问题:我在以下代码中设置了分数符:

 intent.putExtra(WorkoutRoutinesActivity.EXTRA_WORKOUT_NAME, workoutName);
 intent.putExtra(WorkoutRoutinesActivity.EXTRA_WORKOUT_DAYS, workoutDays);

,当我在调试模式下运行应用程序时,两者都显示为null。 workoutName包含一个传递给新活动的简单String,而workoutDays包含一个String数组。

完整代码如下:

public class CreateWorkoutActivity extends Activity {


public final String TAG = this.getClass().getSimpleName();
protected String[] workoutDays = new String[7];
protected String workoutName;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_create_workout);

     Button mNextButton = (Button) findViewById(R.id.next_button1);
     CheckBox satBox = (CheckBox) findViewById(R.id.sat_checkbox);
     CheckBox sunBox = (CheckBox) findViewById(R.id.sun_checkbox);
     CheckBox monBox = (CheckBox) findViewById(R.id.mon_checkbox);
     CheckBox tuesBox = (CheckBox) findViewById(R.id.tues_checkbox);
     CheckBox wedBox = (CheckBox) findViewById(R.id.wed_checkbox);
     CheckBox thursBox = (CheckBox) findViewById(R.id.thurs_checkbox);
     CheckBox friBox = (CheckBox) findViewById(R.id.fri_checkbox);

    final EditText mWorkoutName = (EditText) findViewById(R.id.workout_name1);
    workoutName = mWorkoutName.getText().toString();

    Log.i(TAG, workoutName);



   if (satBox.isChecked()) {
      workoutDays[0] = new String(satBox.getText().toString());
   }

    if (sunBox.isChecked()) {
        workoutDays[1] = new String(sunBox.getText().toString());
    }

    if (monBox.isChecked()) {
        workoutDays[2] =  new String(monBox.getText().toString());
    }

    if (tuesBox.isChecked()) {
        workoutDays[3] = new String(tuesBox.getText().toString());
    }

    if (wedBox.isChecked()) {
        workoutDays[4] =  wedBox.getText().toString();
    }

    if (thursBox.isChecked()) {
        workoutDays[5] =  satBox.getText().toString();
    }

    if (friBox.isChecked()) {
        workoutDays[6] = friBox.getText().toString();
        Log.i(TAG, workoutDays[6]);
    }




    mNextButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.i(TAG, workoutDays.toString());
            Intent intent = new Intent(CreateWorkoutActivity.this, WorkoutRoutinesActivity.class);
            intent.putExtra(WorkoutRoutinesActivity.EXTRA_WORKOUT_NAME, workoutName);
            intent.putExtra(WorkoutRoutinesActivity.EXTRA_WORKOUT_DAYS, workoutDays);
            Log.i(TAG, workoutDays.toString());
            startActivity(intent);

        }
    });

}

3 个答案:

答案 0 :(得分:3)

问题不在于意图,而在于你获得workoutName的方式(这是空值)。您创建活动,设置final EditText mWorkoutName = (EditText) findViewById(R.id.workout_name1);然后立即通过workoutName = mWorkoutName.getText().toString();询问输入值,但此时用户仍然没有输入任何内容。你应该将第二行放在下面的监听器中(因此只有在用户按下mNextButton后才会激活它。在它之后进行一些检查并向用户发送一条消息,他们需要填写该字段(如果是确实是必要的。)

答案 1 :(得分:2)

在创建视图时,似乎未填充workoutNameworkoutDays的值。您应该将文本字段中的值检索到onClickListener函数。

答案 2 :(得分:1)

你检查onCreate中的CheckBox和EditText,绝对是EditText是空的并且没有选中所有的CheckBox