0

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. Here's my relevant code:

In the first activity (this is called and the code gets to the 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):

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

In the broadcastReceiver, in the 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]);
}
pRaNaY
  • 24,642
  • 24
  • 96
  • 146
Beyarkay
  • 25
  • 1
  • 10

5 Answers5

0

you need to get array from intent using following code

askArray = intent.getStringArrayExtra("askArray");
answerArray = intent.getStringArrayExtra("answerArray");
pRaNaY
  • 24,642
  • 24
  • 96
  • 146
  • I see no differences. Take a look at [android.content.Intent](http://grepcode.com/file/repo1.maven.org/maven2/org.robolectric/android-all/5.0.0_r2-robolectric-1/android/content/Intent.java) source code. – frogatto Oct 11 '15 at 10:33
  • I'm afraid there was no change when replaced the first two lines of the onReceive with your two lines of code; I'm still getting NullPointerException on the first line of the for loop in broadcastReveicer. – Beyarkay Oct 11 '15 at 10:39
0

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. This will prevent your app from crashing if an error occurs passing these values to the BroadcastReceiver.

It's also common practice to check if getExtras() returned anything useful, like so:

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. 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:

Intent intent = getIntent();
solarbabies
  • 302
  • 1
  • 12
0

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());
Firefog
  • 3,094
  • 7
  • 48
  • 86
0

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

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 !!

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.

Community
  • 1
  • 1
Beyarkay
  • 25
  • 1
  • 10
-1

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.

frogatto
  • 28,539
  • 11
  • 83
  • 129
Fabulist
  • 31
  • 3