I tried to connect my app to the internet. I run the following code This is the set of code I entered in "ConnectionManager.kt" Class
import android.content.Context
import android.net.ConnectivityManager
import android.net.Network
import android.net.NetworkInfo
class ConnectionManager {
fun checkConnectivity(context: Context):Boolean{
val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val activeNetwork : NetworkInfo?=connectivityManager.activeNetworkInfo
if(activeNetwork?.isConnected!=null){
return activeNetwork.isConnected
}
else{
return false
}
}
}
The error can maybe here or not. I don't know. I entered another set of code in another file called "DashboardFragment.kt" here, in my opinion, will be an error but I cannot resolve it. The code in "DashboardFragment.kt" file is as follows-:
import android.app.AlertDialog
import android.content.Context
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.gill.bookhub.R
import com.gill.bookhub.adapter.DashboardRecyclerAdapter
import com.gill.bookhub.model.Book
import com.gill.bookhub.util.ConnectionManager
import kotlinx.android.synthetic.*
class DashboardFragment : Fragment() {
lateinit var recyclerDashboard: RecyclerView
lateinit var layoutManager:RecyclerView.LayoutManager
lateinit var checknet:Button
lateinit var recyclerAdapter: DashboardRecyclerAdapter
val bookInfoList = arrayListOf<Book>(
Book("P.S. I love You", "Cecelia Ahern", "Rs. 299", "4.5", R.drawable.ps_ily),
Book("The Great Gatsby", "F. Scott Fitzgerald", "Rs. 399", "4.1", R.drawable.great_gatsby),
Book("Anna Karenina", "Leo Tolstoy", "Rs. 199", "4.3", R.drawable.anna_kare),
Book("Madame Bovary", "Gustave Flaubert", "Rs. 500", "4.0", R.drawable.madame),
Book("War and Peace", "Leo Tolstoy", "Rs. 249", "4.8", R.drawable.war_and_peace),
Book("Lolita", "Vladimir Nabokov", "Rs. 349", "3.9", R.drawable.lolita),
Book("Middlemarch", "George Eliot", "Rs. 599", "4.2", R.drawable.middlemarch),
Book("The Adventures of Huckleberry Finn", "Mark Twain", "Rs. 699", "4.5", R.drawable.adventures_finn),
Book("Moby-Dick", "Herman Melville", "Rs. 499", "4.5", R.drawable.moby_dick),
Book("The Lord of the Rings", "J.R.R Tolkien", "Rs. 749", "5.0", R.drawable.lord_of_rings)
)
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
checknet= view!!.findViewById(R.id.checknet)
checknet.setOnClickListener {
if (ConnectionManager().checkConnectivity(activity as Context)){
val dialog=AlertDialog.Builder(activity as Context)
dialog.setTitle("Success")
dialog.setMessage("Internet Connection Found")
dialog.setPositiveButton("ok"){text,listener->}
dialog.setNegativeButton("cancel"){text,listener->}
dialog.create()
dialog.show()
}
else{
val dialog=AlertDialog.Builder(activity as Context)
dialog.setTitle("Failure")
dialog.setMessage("Internet Connection not Found")
dialog.setPositiveButton("ok"){text,listener->
//
}
dialog.setNegativeButton("cancel"){text,listener->}
dialog.create()
dialog.show()
}
}
val view=inflater.inflate(R.layout.fragment_dashboard,container,false)
recyclerDashboard=view.findViewById(R.id.recyclerDashboard)
layoutManager=LinearLayoutManager(activity)
recyclerAdapter= DashboardRecyclerAdapter(activity as Context,bookInfoList)
recyclerDashboard.adapter=recyclerAdapter
recyclerDashboard.layoutManager=layoutManager
recyclerDashboard.addItemDecoration(
DividerItemDecoration(
recyclerDashboard.context,
(layoutManager as LinearLayoutManager).orientation
)
)
return view
}
}
These are the two files. Please tell me where is the error. This is the error which the code shows on mobile phone enter image description here
I am adding one more Image from logcat to find the error as per demanded by one of our stack overflow friends..... enter image description here