Bundle = getIntent()。getExtras()始终为null

时间:2017-05-19 14:51:17

标签: android android-intent extras

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

这是我的MainActivity

   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()始终返回null ...

2 个答案:

答案 0 :(得分:1)

您正在使用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()在click事件之外工作,则将其设为字段。

private Bundle extras;

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

并使用onClick中的那些

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

答案 1 :(得分:0)

您将数据直接放入intent并使用bundle访问,这是错误的。直接使用data.getString。如果要在(*t,c)

中使用(t[0],t[1],c)来获取它,则必须将bundle设置为intent.setExtras(bundle)