Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9249661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:15:12+00:00 2026-06-18T10:15:12+00:00

I have an easy question. I have declared text view in main activity, and

  • 0

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!

P.S: 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
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T10:15:13+00:00Added an answer on June 18, 2026 at 10:15 am

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have probably a quite easy question: I created a canvas on my HTML
I have an easy question, though I'm not sure if it's doable or not:
Hi guys I have an easy question, but not easy to me. I have
Hopefully an easy question. If I have an object and I want to call
This is probably an easy question... I have 4 source versions of the same
I think this could be a very easy question for you. But I have
I'm sure this is an easy question, but I don't have an answer for.
Before answering, it is not as easy question as you might have thought about
I have a question that should be quick and easy for you guys to
I have a quick question about something I imagine must be pretty easy -

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.