0

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

SnazzyGill
  • 13
  • 5
  • add image logcat error – MexiCano May 28 '20 at 17:02
  • in logcat...in error.... what should I type in search bar. because it is continuously scrolling @MexiCano – SnazzyGill May 28 '20 at 17:04
  • I think this can be problem @MexiCano at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636) – SnazzyGill May 28 '20 at 17:05
  • Please don't post screenshots of code, XML, or logs. Please post all text as text. That said, this line in `onCreateView()` is the current issue: `checknet= view!!.findViewById(R.id.checknet)`. A `Fragment`'s `view` is created in `onCreateView()`, so until that method returns, `view` is going to be null. It looks like you intended to define your own `view` variable there, but you're doing it too late, possibly just due to inadvertently rearranging your code incorrectly. The `val view=inflater.inflate(...` line needs to be first thing inside `onCreateView()`. – Mike M. May 28 '20 at 18:55
  • @MikeM. @MexiCano I change `checknet= view!!.findViewById(R.id.checknet)` to `checknet= view.findViewById(R.id.checknet)` gives **error on . between view and findviewbyid** the error – SnazzyGill May 29 '20 at 16:59
  • Why did you do that? All I suggested is that you move that line to the top of the method. – Mike M. May 29 '20 at 22:34

0 Answers0