Выражение 'recyclerView' не должно быть нулевым в edgar.yodgorbek.yangiliklar.sportactivities.BBCSportFragment.onCreateView (BBCSportFragment.kt: 62) - PullRequest
0 голосов
/ 17 июня 2019

Я занимаюсь разработкой новостного приложения. В моем фрагменте появляется следующее исключение

Процесс: edgar.yodgorbek.yangiliklar, PID: 6949 java.lang.NullPointerException: выражение 'recyclerView' не должно быть нулевым at edgar.yodgorbek.yangiliklar.sportactivities.BBCSportFragment.onCreateView (BBCSportFragment.kt: 62)

ниже моего BBCSportFragment.kt

класс BBCSportFragment: Fragment (), ArticleAdapter.ClickListener {

var articleList: MutableList<Article> = ArrayList()


@ActivityContext
var activityContext: Context? = null
@ApplicationContext
var mContext: Context? = null
@BindView(R.id.recycler_view)
internal var recyclerView: RecyclerView? = null
internal var bbcSportFragmentComponent: BBCSportFragmentComponent? = null
internal var bbcFragmentContextModule: BBCFragmentContextModule? = null
private var search: Search? = null
private var sportNews: SportNews? = null
private var articleAdapter: ArticleAdapter? = null
private var apiInterface: SportInterface? = null


override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                          savedInstanceState: Bundle?): View? {
    val view = inflater.inflate(R.layout.fragment_bbcsport, container, false)
    ButterKnife.bind(this, view)

    articleAdapter = ArticleAdapter(articleList)
    val layoutManager = LinearLayoutManager(context)
    recyclerView!!.layoutManager = layoutManager
    recyclerView!!.adapter = articleAdapter

    apiInterface = SportClient.apiService

    fetchInitialArticles()

    return view
}

private fun fetchInitialArticles() {
    val progress = ProgressDialog(context)
    progress.setMessage("Loading... ")
    progress.isIndeterminate = true
    progress.show()
    val call = apiInterface!!.articles
    call.enqueue(object : Callback<SportNews> {
        override fun onResponse(call: Call<SportNews>, response: Response<SportNews>) {
            if (response.body() != null) {
                sportNews = response.body()
                if (sportNews!!.articles != null) {
                    articleList.clear()
                    sportNews!!.articles?.let { articleList.addAll(it) }
                }
                articleAdapter!!.notifyDataSetChanged()
            }
            progress.dismiss()
        }

        override fun onFailure(call: Call<SportNews>, t: Throwable) {
            progress.dismiss()
            Toast.makeText(context, "" + t.message, Toast.LENGTH_SHORT).show()
        }
    })
}

private fun getContext(applicationComponent: ApplicationComponent): Context? {
    return null
}

fun doFilter(searchQuery: String) {
    searchAPICall(searchQuery)

}

private fun searchAPICall(searchQuery: String) {
    val progress = ProgressDialog(context)
    progress.setMessage("Searching... ")
    progress.isIndeterminate = true
    progress.show()

    val searchCall = apiInterface!!.getSearchViewArticles(searchQuery)
    searchCall.enqueue(object : Callback<Search> {
        override fun onResponse(call: Call<Search>, response: Response<Search>) {
            try {
                search = response.body()

                if (search != null && search!!.articles != null) {
                    articleList.clear()
                    search!!.articles?.let { articleList.addAll(it) }
                }
                articleAdapter!!.notifyDataSetChanged()

                Toast.makeText(context, "Searched.", Toast.LENGTH_SHORT).show()
            } catch (e: Exception) {
                Toast.makeText(context, "" + e.message, Toast.LENGTH_SHORT).show()
                e.printStackTrace()
            }

            progress.dismiss()
        }

        override fun onFailure(call: Call<Search>, t: Throwable) {
            progress.dismiss()
            Toast.makeText(context, "" + t.message, Toast.LENGTH_SHORT).show()
        }
    })
}

}

1 Ответ

0 голосов
/ 17 июня 2019

Я не думаю, что ButterKnife хорошо работает с kotlin - вы можете использовать синтетику для своего фрагмента.

https://kotlinlang.org/docs/tutorials/android-plugin.html

...