0

So I'm running into an error with my BroadcastREceiver...the receiver is running and detecting the change in network..as soon as I add the visiblity it cans out...

Where it is canning out is at the change of visibility of the warning_message in the

 val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        val networkInfo = connectivityManager.activeNetworkInfo
        if (networkInfo != null && networkInfo.detailedState == NetworkInfo.DetailedState.CONNECTED) {
            Log.d("Network Connected","NC is connected")
                MainActivity().warning_message.visibility = View.INVISIBLE
            MainActivity().isConnected = true
        } else if (networkInfo != null) {
            val state = networkInfo.detailedState
            Log.d("NetworkReceiver", state.name)
        } else {
            MainActivity().warning_message.visibility = View.VISIBLE
            MainActivity().isConnected = false
            Log.d("Network Connected","NC")
        }

    }

The warning message is just a:

 <FrameLayout
        android:id="@+id/warning_message"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:visibility="invisible"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/top_toolbar">

in the Main Activity layout.

The error I receive is: Process: com.example.user.appname, PID: 19972

java.lang.RuntimeException: Error receiving broadcast Intent { act=android.net.conn.CONNECTIVITY_CHANGE flg=0x4000010 (has extras) } in com.example.user.appname.ConnectionReceiver@531d646

There is no specific error message as to why.

BostonMacOSX
  • 1,369
  • 2
  • 17
  • 38

1 Answers1

0

Don't call MainActivity directly from the broadcast receiver. Use an event bus, and post and event to the activity from the broadcast receiver. I have already answered to a question like yours here, where I show (lol basically copying and pasting what I read on greenrobot's github) how to do it. Now I get that you are using Kotlin, so you can end the installation of Greerobot's event bus by reading this.

Maxime Claude
  • 965
  • 12
  • 27