Invoking method on null reference kotlin extensions

huangapple go评论73阅读模式
英文:

Invoking method on null reference kotlin extensions

问题

我在我的碎片中遇到了一个非常奇怪的错误:

java.lang.NullPointerException: 尝试在空对象引用上调用虚拟方法 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' 
        at com.halalrishtey.ProfessionalDetailsFragment.onViewCreated(ProfessionalDetailsFragment.kt:47)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:892)
        at androidx.fragment.app.FragmentManagerImpl.addAddedFragments(FragmentManagerImpl.java:2100)
        at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1874)
        at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1830)
        at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727)
        at androidx.fragment.app.FragmentManagerImpl$2.run(FragmentManagerImpl.java:150)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6077)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

我在onViewCreated函数中通过kotlin-extensions访问按钮时出现了空引用的错误,代码如下:

uploadAdhar_Button.setOnClickListener {
    findNavController().navigate(R.id.action_professionalDetails_to_uploadImageFragment)
}

之前是正常工作的,我不知道出了什么问题,请帮忙解决!

英文:

So, I'm getting a really weird error in my fragment:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
        at com.halalrishtey.ProfessionalDetailsFragment.onViewCreated(ProfessionalDetailsFragment.kt:47)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:892)
        at androidx.fragment.app.FragmentManagerImpl.addAddedFragments(FragmentManagerImpl.java:2100)
        at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1874)
        at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1830)
        at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727)
        at androidx.fragment.app.FragmentManagerImpl$2.run(FragmentManagerImpl.java:150)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6077)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

I'm getting null reference on a button which I access inside onViewCreated function through kotlin-extensions like so:

uploadAdhar_Button.setOnClickListener {
    findNavController().navigate(R.id.action_professionalDetails_to_uploadImageFragment)
}

It was working before, I don't know what went wrong please help!

答案1

得分: 1

去到ProfessionalDetailsFragmentimport语句并导入正确的合成布局。它应该是由ProfessionalDetailsFragment充气的同一个xml。

我最好的猜测是uploadAdhar_Buttonid也被另一个.xml文件中的某些其他视图使用!

import kotlinx.android.synthetic.main.fragment_professional_details.*

同时删除未使用的.xml的另一个import语句。

英文:

Go to the import statements of ProfessionalDetailsFragment and import the correct synthetic. It should be the same xml which is inflated by ProfessionalDetailsFragment.

My best guess is that uploadAdhar_Button id is used by some other views in another .xml file too !

import kotlinx.android.synthetic.main.fragment_professional_details.*

Also delete the other import statement of the unused .xml

huangapple
  • 本文由 发表于 2020年1月7日 01:42:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/59616604.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定