0

我试图将我的应用程序连接到互联网。我运行以下代码这是我在“ConnectionManager.kt”类中输入的一组代码


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
        }
    }
}

错误可能在这里也可能不在。我不知道。我在另一个名为“DashboardFragment.kt”的文件中输入了另一组代码,在我看来,这将是一个错误,但我无法解决它。“DashboardFragment.kt”文件中的代码如下:

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
    }

}

这是两个文件。请告诉我错误在哪里。这是代码在手机上显示的错误enter image description here

我正在从 logcat 添加一张图片,以根据我们的一位堆栈溢出朋友的要求查找错误..... 在此处输入图片描述

4

0 回答 0