Bundle = getIntent().getExtras() 总是空的

Bundle = getIntent().getExtras() is always null

我正在编写一个闹钟应用程序。当我保存警报时,它显示在我的 MainActivity.
的 ListView 中 当我想单击该行并编辑该提醒时,getIntent().getExtras() 始终为空,因此我没有返回

的价值

这是我的主Activity

   protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main); 
    startActivity(new Intent(MainActivity.this, AlarmChooser.class));

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View row = convertView;
        ViewHolder holder = null;
        if(row==null || row.getTag()==null){
            LayoutInflater inflater = LayoutInflater.from(activity);
            row = inflater.inflate(layoutResource,null);
            holder=new ViewHolder();

            holder.mTitle=(TextView)row.findViewById(R.id.name);
            holder.mDate=(TextView)row.findViewById(R.id.dateText);
            holder.mTime=(TextView)row.findViewById(R.id.timeText);

            row.setTag(holder);
        }else{
            holder = (ViewHolder)row.getTag();
        }

        holder.myAlarm = getItem(position);
        holder.mTitle.setText(holder.myAlarm.getTitle());
        holder.mDate.setText(holder.myAlarm.getRecordDate());
        holder.mTime.setText(holder.myAlarm.getRecordTime());

        final ViewHolder finalHolder = holder;
        holder.mTitle.setOnClickListener(new View.OnClickListener() {
              @Override
             public void onClick(View view) {
    Bundle extras = getIntent().getExtras();
                if (extras != null) {
                    String titles = extras.getString("title");
                    int day = extras.getInt("day");
                    int month = extras.getInt("month");
                    int year = extras.getInt("year");
                    int minute = extras.getInt("minute");
                    int hour = extras.getInt("hour");
      String typeee = cursor.getString(cursor.getColumnIndex(Constants.COLUMN_TYPE));

                    Log.e("title", titles);

                    Log.e("year", "value=" + year);
                    Log.e("month", "value=" + month);
                    Log.e("day", "value=" + day);
                    Log.e("hour", "value=" + hour);
                    Log.e("minute", "value=" + minute);
                    Log.e("type", typeee);

                    Class next = null;

                    switch (typeee) {
                        case "TimeDateRem":
                            next = TimeDateRem.class;
                            break;
                        case "BirthdayRem":
                            next = BirthdayRem.class;
                            break;
                        case "HolidayReminderKuwait":
                            next = HolidayReminderKuwait.class;
                            break;
                        case "HolidayReminderLebanon":
                            next = HolidayReminderLebanon.class;
                            break;
                        case "HolidayReminderSaudiArabia":
                            next = HolidayReminderSaudiArabia.class;
                            break;
                        case "HolidayReminderUAE":
                            next = HolidayReminderUAE.class;
                            break;
                    }

                    if (next != null) {
                        Intent return_intent = new Intent(MainActivity.this, next);
                        return_intent.putExtra("alarm_title", titles);
                        return_intent.putExtra("alarm_month", month);
                        return_intent.putExtra("alarm_year", year);
                        return_intent.putExtra("alarm_day", day);
                        return_intent.putExtra("alarm_hour", hour);
                        return_intent.putExtra("alarm_minute", minute);
                        //   next_intent.putExtra("alarm_content", contenttt);
                        return_intent.putExtra("FROM_ACTIVITY", "MainActivity");
                        setResult(RESULT_OK, return_intent);
                        finish();

  } else {

                    Log.e("ERROR", "ERROR");
                }

});

        return row;

}
    }

    class ViewHolder{
        MyAlarm myAlarm;
        TextView mTitle;
        TextView mDate;
        TextView mTime;
        TextView mContent;
        TextView mID;
    }
}

这是我的 TimeDateRem,这是一个警报,我试图从中获取数据 - 但未成功

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

    private void setAlarm(Calendar targetCal) {
  Intent intent_ForResult = new Intent(TimeDateRem.this, MainActivity.class);
           intent_ForResult.putExtra("title", title.getText().toString().trim());
    intent_ForResult.putExtra("day", pickerDate.getDayOfMonth());
    intent_ForResult.putExtra("month", pickerDate.getMonth());
    intent_ForResult.putExtra("year", pickerDate.getYear());
    intent_ForResult.putExtra("hour", pickerTime.getCurrentHour());
    intent_ForResult.putExtra("minute", pickerTime.getCurrentMinute());
    intent_ForResult.putExtra("type", "TimeDateRem");
   startActivityForResult(intent_ForResult, REQUEST);
  }

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == REQUEST) {
        if (resultCode == RESULT_OK) {
            Bundle extras = getIntent().getExtras();
            String previousActivity = extras.getString("FROM_ACTIVITY");

            if (previousActivity.equals("MainActivity")) {

                String titles = extras.getString("alarm_title");
                int year_intent = extras.getInt("alarm_year");
                int month_intent = extras.getInt("alarm_month");
                int day_intent = extras.getInt("alarm_day");
                int minute_intent = extras.getInt("alarm_minute");
                int hour_intent = extras.getInt("alarm_hour");

                String year = String.valueOf(year_intent);
                String month = String.valueOf(month_intent);
                String day = String.valueOf(day_intent);
                String minute = String.valueOf(minute_intent);
                String hour = String.valueOf(hour_intent);

                title.setText(titles);
                pickerTime.setCurrentHour(Integer.parseInt(hour));
                pickerTime.setCurrentMinute(Integer.parseInt(minute));
                pickerDate.init(
                        Integer.parseInt(year),
                        Integer.parseInt(month),
                        Integer.parseInt(day),
                        null
                );

                String result = data.getStringExtra("returnData");
                Log.e("RESULTRESULRESULT ", result);

            }
        }
    }
}

我正在做的是从 TimeDateRem 获取数据,将其发送到主 Activity,然后将其发送回 TimeDateRem。
如果我想编辑已保存的提醒,但 bundle somthing = getIntent().getExtras() 总是 returns null...

您将数据直接放入 intent 并使用 bundle 访问,这是错误的。直接使用data.getString。如果你想在 onActivityResult

中使用 data.getExtras() 获取它,你必须将 bundle 设置为像 intent.setExtras(bundle) 这样的意图

您使用的是 getIntent() 而不是 Intent data... 所以,这不是您通过 startActivityForResult

传递的意图

例如

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == REQUEST) {
        if (resultCode == RESULT_OK) {
            Bundle extras = data.getExtras();
            String previousActivity = extras.getString("FROM_ACTIVITY");

此外,如果 getIntent().getExtras() 在单击事件之外工作,则将其设为字段。

private Bundle extras;

public void onCreate(Bundle savedInstanceState) {
    .... 
    this.extras = getIntent().getExtras();
} 

并在 onClick 中使用它们

不过,理想情况下,我建议不要在 Activity 代码中定义整个适配器,而是创建一个单独的 class 文件。在这种情况下,您不能在适配器代码

中使用 Activity Intent