英文:
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
去到ProfessionalDetailsFragment
的import
语句并导入正确的合成布局。它应该是由ProfessionalDetailsFragment
充气的同一个xml。
我最好的猜测是uploadAdhar_Button
的id
也被另一个.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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论