[英]How can I hide and show views on a MainActivity from a BroadcastReceiver
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... 所以我的BroadcastREceiver遇到错误...接收器正在运行并检测网络的变化..一旦添加可见性,它就会消失...
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 我收到的错误是:进程: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
java.lang.RuntimeException:接收com.example.user.appname.ConnectionReceiver@531d646中的广播意图{act = android.net.conn.CONNECTIVITY_CHANGE flg = 0x4000010(has extras)}时出错
There is no specific error message as to why. 没有关于原因的特定错误消息。
Don't call MainActivity directly from the broadcast receiver. 不要直接从广播接收机调用MainActivity。 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.
我已经在这里回答了像您这样的问题,在这里我演示了(大体上是复制并粘贴我在greenrobot的github上阅读的内容)如何做到这一点。 Now I get that you are using Kotlin, so you can end the installation of Greerobot's event bus by reading this .
现在,我知道您正在使用Kotlin,因此您可以通过阅读this结束Greerobot事件总线的安装。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.