简体   繁体   English

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

[英]Problems with multiple myIntent.putExtra/myIntent.getExtra in Android

Ok, so I am trying to get two different String arrays from one activity, through an alarmManager to a BroadcastReceiver (called AlarmReceiver), and so far have been using myIntentName.putExtra() and the relevant .getExtra() on the broadcastReceiver side of things. 好的,因此,我试图从一个活动中,通过alarmManager到BroadcastReceiver(称为AlarmReceiver)获得两个不同的String数组,到目前为止,我一直在使用myIntentName.putExtra()和相关的.getExtra()在东西。 Here's my relevant code: 这是我的相关代码:

In the first activity (this is called and the code gets to the AlarmReceiver): 在第一个活动中(这被调用,代码到达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); 
}

At the top of the AlarmReceiver(which extends BroadcastReceiver): 在AlarmReceiver(扩展了BroadcastReceiver)的顶部:

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

In the broadcastReceiver, in the onReceive(): 在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]);
}

you need to get array from intent using following code 您需要使用以下代码从intent获取数组

askArray = intent.getStringArrayExtra("askArray");
answerArray = intent.getStringArrayExtra("answerArray");

You're presumably getting a NullPointerException when accessing askArray.length , which means it's a good idea to check if askArray == null before attempting to iterate through the array. 大概在访问askArray.length时会收到NullPointerException,这意味着在尝试遍历数组之前检查askArray == null是否是一个好主意。 This will prevent your app from crashing if an error occurs passing these values to the BroadcastReceiver. 如果将这些值传递给BroadcastReceiver时发生错误,这将防止您的应用崩溃。

It's also common practice to check if getExtras() returned anything useful, like so: 检查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
}

If this logs the failure message every time, then the problem might be related to how you're setting the intent variable. 如果这每次都记录失败消息,则问题可能与您设置intent变量的方式有关。 It appears you're setting it correctly before "putting" the arrays, but it's not apparent how you're setting it when "getting" the arrays. 看来您在“放入”数组之前已正确设置了它,但是“获取”数组时如何设置它尚不清楚。 This should be your code in the BroadcastReceiver: 这应该是您在BroadcastReceiver中的代码:

Intent intent = getIntent();

use this 用这个

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

for details 详情

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

Ok, sorry but it turns out that the problem wasn't with the part of the code I posted. 好的,很抱歉,但事实证明问题出在我发布的部分代码中。 I was using an alarm manager that had a quirk I wasn't aware of, but thanks for all the answers, here is the link: Android cannot pass intent extras though AlarmManager 我正在使用一个警报管理器,该警报管理器我没有意识到,但是感谢所有的答案,这里是链接: Android无法通过AlarmManager传递意图附加功能

And here is the relevant information(copied from the 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 !! Haresh的代码不是完整的答案...我使用了Bundle,但我尝试了不使用Bundle的方法,但是当我尝试从多余的字符串中获取字符串时,我得到了空值!

The exact problem, in your code, is with the PendingIntent ! 在您的代码中,确切的问题在于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 因为标志的0会导致您头疼

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. 这可能是一个非常普遍的问题,因为Google的示例代码被忽略了在警报中包括Extra。

Maybe this solve your problem: 也许这可以解决您的问题:

At the top of the broadcast-receiver change fields to this: 在广播接收器变化的顶部场到这一点:

String[] askArray;
String[] answerArray;

That's all. 就这样。 Good luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM