the_dani
the_dani

Reputation: 2524

Already initialised object is set to null in BroadcastListener Method: onReceive

My Class NetworkListener should check, if Internet Connection is available or not. Then, It should show a Snackbar with the InternetState. For this, I'm using an interface.

My whole code:

[MainActivity.java]:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    setTitle(R.string.Inbox);

    Initialize_NavigationDrawer();

    Initialize_Objects();

    //1. Here, I initialise my NetworkListener and register my Receiver
    NetworkListener mNetworkListener = new NetworkListener();
    mNetworkListener.registerReceiver(this);
}

@Override
public void ToggleSnackbar(boolean ShowHide) {
    if (ShowHide) snbNoInternet.show();
    else snbNoInternet.dismiss();
}

[NetworkListener.java]:

public class NetworkListener extends BroadcastReceiver {

    private SnackbarInterface snbInterface;

    public NetworkListener() {}

    @Override
    public void onReceive(Context context, Intent intent) {
        //4. After toggling the Network-Connection, a NullReferenceException occurs
        //5. System.out.println(snbInterface) shows: null
        ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo ni = cm.getActiveNetworkInfo();
        if (ni == null || !ni.isConnected()) {
            snbInterface.ToggleSnackbar(true);
        }
        else if (ni != null && ni.isConnected()) {
            snbInterface.ToggleSnackbar(false);
        }
    }

    public void registerReceiver(SnackbarInterface receiver) {
        snbInterface = receiver; //2. Is called from onCreate

        //3. System.out.println(snbInterface) shows: at.guger.email.activities.MainActivity@27472a5e
    }

    public interface SnackbarInterface {
        public void ToggleSnackbar(boolean ShowHide);
    }
}

[AndroidManifest.xml]:

<activity
    android:name=".activities.MainActivity" >

    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>

</activity>

<activity
    android:name=".activities.WriteActivity" />
<activity
    android:name=".activities.ReadActivity" />

<receiver
    android:name=".NetworkListener">
    <intent-filter>
        <action
            android:name="android.net.conn.CONNECTIVITY_CHANGE" />
    </intent-filter>
</receiver>

And the permissions:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />

The code is called like the numbers are ordered! I really can't explain, why snbInterface is set to null!

I hope you can help me!

Upvotes: 1

Views: 689

Answers (1)

You registered the BroadcastReceiver using the receiver element in the AndroidManifest. Android system creates the BroadcastReceiver instance and calls the onReceive method with the appropriate parameters.

If you want to use a BroadcastReceiver to manipulate UI of your application (like show/hide Snackbar ), you will have to register the receiver within your activity using the registerReceiver(BroadcastReceiver receiver,IntentFilter intentFilter) in the onCreate method and unRegister in the onDestroy method of your activity.

Remove the receiver element from the AndroidManifest.xml file and create the BroadcastReceiver through code.

This question here has a clear explanation on how BroadcastReceivers can be created and registered : Broadcast Receiver class and registerReceiver method

In the onReceive method of the BroadcastReceiver, call the toggleSnackbar method in your Activity to toggle the Snackbar.

There is an other method for achieving what you want. It is discussed here: Inform Activity from a BroadcastReceiver ONLY if it is in the foreground

Read more on BroadcastReceivers here

You can write the Receiver as a separate class. Instantiate the Receiver and make a call to registerReceiver with the first parameter as the Receiver and the second parameter as the IntentFilter.

Upvotes: 1

Related Questions