简体   繁体   中英

NullPointerExceptoin when passing value from activity to broadcast receiver

I have an easy question.

I have declared text view in main activity, and created it from XML ( findViewById ). I would like to pass this value to a subclass of broadcast receiver. Following is my Broadcast constructor:

public Broadcast(TextView text_dBm) {   
    this.text_dBm = text_dBm;   
}

In my main activity I create a new broadcast object and pass my textview value inside, like this:

new Broadcast(text_dBm);

But I'm still getting null pointer exception on my text_dBm . Is there anyway (besides static methods) to pass values between activites and broadcast receiver?

Oh and yes. My broadcast receiver is registered programmatically (in service), and its running perfectly.

Thank you for your time!

PS: I already checked some threads here in SO, but i didn't find an answer: How to pass value from an activity in an broadcast receiver?

Main activity class:

public class MainActivity extends Activity {
    TextView text_dBm, text_time, text_rssi;
    Intent startServiceFromActivity;

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

        text_dBm = (TextView) findViewById(R.id.textView_dBm);

        new Broadcast(text_dBm);
        startServiceFromActivity = new Intent(this, WifiService.class);
        startService(startServiceFromActivity); 
    }
}

Broadcast receiver class:

public class Broadcast extends BroadcastReceiver {
    WifiInfo wifiInfo;
    WifiManager wifiManager_service;
    TextView text_dBm;

    public Broadcast(WifiManager wifiManager_service) { 
        this.wifiManager_service = wifiManager_service; 
    }

    public Broadcast(TextView text_dBm) {       
        this.text_dBm = text_dBm;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("RECEIVER", "Receiver running"); // LOG   

        text_dBm.setText("textview"); // nullpointerexception
    }
}

Pass data Through intent

Activity -

Intent i = new Intent(Activity.this, Broadcast.class);
Bundle b = new Bundle();
b.putString("key", "value");
i.putExtras(b);
startActivity(i);

In your broadcast receiver class onReceive method

@Override
public void onReceive(Context context, Intent intent) 
{
    String result = intent.getString("key");
    // your method
}

You can't pass around views using Intents. To do what you want to do, you will need your broadcast receiver to be an inner class of your activity. The receiver should be registered when activity is started and unregistered when activity is stopped. Else you will leak memory. That means that you will only be able to receive your messages when actually on the activity screen itself.

If you need to be able to receive broadcasts outside the activity, you will need to:

  1. register your receiver in the manifest for a given action (or in a service, but don't forget to unregister it)
  2. start the activity and pass the message to show in the textview using an intent extra
  3. when the activity starts, check if the intent contains anything to show in the textview and do the necessary

From your comment:

  1. Create the receiver as an inner class of the activity (not a static one so it can access the activity's TextView instance)
  2. register the receiver in onStart
  3. unregister the receiver in onStop
  4. In the onReceive method of your receiver do: textView.setText(intent.getStringExtra("dbm"));
  5. Service sends the broadcast by passing an intent extra called "dbm" and containing the text you want to display

-

public class MainActivity extends Activity {

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

    dbmView = (TextView) findViewById(R.id.textView_dBm);
  }

  @Override
  protected void onStart() {
    super.onStart();
    IntentFilter intentFilter = new IntentFilter("com.example.broadcasts.DBM_UPDATE");
    registerReceiver(receiver, intentFilter);
  }

  @Override
  protected void onStop() {
    unregisterReceiver(receiver);
    super.onStop();
  }

  private TextView dbmView;

  private BroadcastReceiver receiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        dbmView.setText(intent.getStringExtra("dbm"));
    }
  }

}

In the service:

Intent i = new Intent("com.example.broadcasts.DBM_UPDATE");
i.putExtra("dbm", "it works!");
sendBroadcast(i);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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