Android 中多个 myIntent.putExtra/myIntent.getExtra 的问题

Problems with multiple myIntent.putExtra/myIntent.getExtra in Android

好的,所以我试图从一个 activity 中获取两个不同的字符串数组,通过 alarmManager 到 BroadcastReceiver(称为 AlarmReceiver),到目前为止一直在使用 myIntentName.putExtra() 和broadcastReceiver 方面的相关 .getExtra() 。这是我的相关代码:

在第一个 activity 中(这被调用并且代码到达 AlarmReceiver):

private void setAlarmManager() {
    Intent intent = new Intent(this, AlarmReceiver.class);
//I have already defined askArray and answerArray (they aren't null, but are dynamic 
//and exactly how I define them is quite complex so not included here)
    intent.putExtra("askArray", askArray);
    intent.putExtra("answerArray", answerArray);

    PendingIntent sender = PendingIntent.getBroadcast(this, 2, intent, 0);
    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
    am.setRepeating(AlarmManager.RTC_WAKEUP, 5000, 61000, sender); 
}

在 AlarmReceiver 的顶部(它扩展了 BroadcastReceiver):

String[] askArray = new String[2];
String[] answerArray = new String[2];

在broadcastReceiver中,在onReceive()中:

askArray = intent.getExtras().getStringArray("askArray");
answerArray = intent.getExtras().getStringArray("answerArray");

for (int i = 0; i < askArray.length; i++){ //I get a NullPointerException on this line
    Log.i("L3R", "AskArray["+i+"]: " + askArray[i]);
}

也许这可以解决您的问题:

将广播接收器顶部的字段更改为:

String[] askArray;
String[] answerArray;

就是这样。祝你好运。

您需要使用以下代码

intent获取数组
askArray = intent.getStringArrayExtra("askArray");
answerArray = intent.getStringArrayExtra("answerArray");

您可能在访问 askArray.length 时遇到 NullPointerException,这意味着在尝试遍历数组之前检查是否 askArray == null 是个好主意。如果将这些值传递给 BroadcastReceiver 时发生错误,这将防止您的应用程序崩溃。

检查 getExtras() 是否返回了任何有用的信息也是常见的做法,如下所示:

Bundle extras = intent.getExtras();
if(extras != null) {
    askArray = extras.getStringArray("askArray");
    answerArray = extras.getStringArray("answerArray");

    if(askArray != null) {
        for (int i = 0; i < askArray.length; i++) {
            Log.i("L3R", "AskArray["+i+"]: " + askArray[i]);
        }
    }
} else {
    Log.e("L3R", "Failed to getExtras()");
    // deal with missing values here
}

如果每次都记录失败消息,则问题可能与您设置 intent 变量的方式有关。看起来您在 "putting" 数组之前正确设置了它,但是当 "getting" 数组时您如何设置它并不明显。这应该是您在 BroadcastReceiver 中的代码:

Intent intent = getIntent();

使用这个

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
ContactItem contactData = (ContactItem) listView.getItemAtPosition(position);
Intent intent = new Intent(getActivity(), ContactDetail.class);
               intent.putExtra("DATA_KEY", contactData);
               startActivity(intent);
            }
        });

详情

Intent i = getIntent();
Bundle b = i.getExtras();

// getting attached intent data
ContactItem contact = (ContactItem) b.getSerializable("DATA_KEY");

// displaying selected contact name
txtName.setText(contact.getName());
txtPhone.setText(contact.getPhone());
txtMobile.setText(contact.getMobile());
txtEmail.setText(contact.getEmail());

好的,抱歉,但事实证明问题不在于我发布的那部分代码。我使用的警报管理器有一个我不知道的怪癖,但感谢所有答案,这里是 link:Android cannot pass intent extras though AlarmManager

这里是相关资料​​(从link复制而来):

Haresh's code is not the complete answer... I used a Bundle and I tried without Bundle but I got null's either way when I attempting to obtain the strings from the extra's !!

The exact problem, in your code, is with the PendingIntent !

This is wrong if you're trying to pass extra's :

PendingIntent pendingIntent = PendingIntent.getBroadcast(this, uniqueRequestCode, intent, 0);

Because the 0 for the flags is what will cause you a headache

This is the right way to do it - specify a flag !

PendingIntent pendingIntent = PendingIntent.getBroadcast(this, uniqueRequestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);

This is probably such a popular problem because Google's sample code neglected to include Extra's in an Alarm.