Skip to content
This repository has been archived by the owner on Sep 12, 2019. It is now read-only.

NPE for abstract View-class #50

Open
orcchg opened this issue Mar 10, 2017 · 3 comments
Open

NPE for abstract View-class #50

orcchg opened this issue Mar 10, 2017 · 3 comments

Comments

@orcchg
Copy link

orcchg commented Mar 10, 2017

Imagine, we have an abstract base view-class:

`

abstract class CollectionFragment {
    protected val emptyView: View by bindView(R.id.empty_view)

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    ... some base layout ...
    }
}

`

then we inherit, and we want to access some view:

`

class NewsFragment : CollectionFragment() {
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {

        val rootView: View? = super.onCreateView(inflater, container, savedInstanceState)

        emptyDataButton.visibility = View.GONE  // hide empty data button

        return rootView

    }
}

`

But we get NPE while accessing this 'emptyView':

Caused by: kotlin.KotlinNullPointerException
                                                                              at butterknife.ButterKnifeKt$viewFinder$5.invoke(ButterKnife.kt:73)
                                                                              at butterknife.ButterKnifeKt$viewFinder$5.invoke(ButterKnife.kt)
                                                                              at butterknife.ButterKnifeKt$required$1.invoke(ButterKnife.kt:82)
                                                                              at butterknife.ButterKnifeKt$required$1.invoke(ButterKnife.kt)
                                                                              at butterknife.Lazy.getValue(ButterKnife.kt:103)
                                                                              at CollectionFragment.getEmptyDataButton(CollectionFragment.kt:0)
                                                                              at NewsFragment.onCreateView(NewsFragment.kt:50)
                                                                              at android.support.v4.app.Fragment.performCreateView(Fragment.java:2189)

When we rollback to classic ButterKnife:

@BindView(R.id.btn_empty_data) lateinit var emptyDataButton: Button

everything is OK

@ModunIran
Copy link

i also have this problem,especially in fragment.

@Wavesonics
Copy link

I don't think it's related to being an abstract class, I think it's related to the view not being set on the fragment yet inside of onCreateView

The fix is to move your code into onViewCreated so the View is already set when the delegate tries to run.

@MBratkowski
Copy link

Up. I have the same issue inside in fragment

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants