不知道 NullPoniter 的确切问题
Posted
技术标签:
【中文标题】不知道 NullPoniter 的确切问题【英文标题】:Don't know the exact problem of NullPoniter 【发布时间】:2021-05-01 04:26:39 【问题描述】:我正在创建一个 FireBase 电话身份验证应用程序。 我有 2 个片段: 1. 用于获取用户的电话号码 2. 用于 OTP 验证。 我想在 EdiText 提交 6 位数字时自动验证 OTP。
我做了与教程中所示完全相同的操作。 请帮助我。
我已经阅读/试图理解这一点: What is a NullPointerException, and how do I fix it?
第 23 和 43 行是:
private val binding get() = _binding!!
和
binding.etOtp.addTextChangedListener(textWatcher)
这是我的日志:
--------- beginning of crash
2021-01-27 15:49:44.430 11355-11355/com.techfan.firebasetest E/androidRuntime: FATAL EXCEPTION: main
Process: com.techfan.firebasetest, PID: 11355
java.lang.NullPointerException
at com.techfan.firebasetest.fragments.OTPFragment.getBinding(OTPFragment.kt:23)
at com.techfan.firebasetest.fragments.OTPFragment.onCreate(OTPFragment.kt:43)
at androidx.fragment.app.Fragment.performCreate(Fragment.java:2684)
at androidx.fragment.app.FragmentStateManager.create(FragmentStateManager.java:280)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1175)
at androidx.fragment.app.FragmentManager.addAddedFragments(FragmentManager.java:2224)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1997)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1953)
at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1849)
at androidx.fragment.app.FragmentManager$4.run(FragmentManager.java:413)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
到目前为止我做了什么:
class OTPFragment : Fragment()
private var _binding: FragmentOtpBinding? = null
private val binding get() = _binding!!
var storedVerificationId: String? = null
lateinit var auth: FirebaseAuth
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View
_binding = FragmentOtpBinding.inflate(inflater, container, false)
return binding.root
override fun onCreate(savedInstanceState: Bundle?)
super.onCreate(savedInstanceState)
storedVerificationId = arguments?.getString("toSend")
binding.etOtp.addTextChangedListener(textWatcher)
auth=FirebaseAuth.getInstance()
private val textWatcher = object : TextWatcher
override fun afterTextChanged(s: Editable?)
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int)
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int)
if (start == 6)
val otp=binding.etOtp.text.toString().trim()
if(otp.isNotEmpty())
val credential : PhoneAuthCredential = PhoneAuthProvider.getCredential(
storedVerificationId.toString(), otp)
signInWithPhoneAuthCredential(credential)
private fun signInWithPhoneAuthCredential(credential: PhoneAuthCredential)
auth.signInWithCredential(credential)
.addOnCompleteListener(this.requireActivity()) task ->
if (task.isSuccessful)
startActivity(Intent(activity?.applicationContext, DashboardActivity::class.java))
activity?.finish()
【问题讨论】:
@ADM val storedVerificationId = arguments?.getString("toSend") 用于从另一个片段中获取值 @ADM 我认为 var storedVerificationId: String? = null 应该可以工作 @ADM 我已经完成了这些更改,但仍然出现同样的错误...请帮助我 【参考方案1】:在片段生命周期中,onCreate()
位于 onCreateView()
之前。您正在尝试在初始化之前使用binding
。
将需要binding
的代码移至onCreateView()
或更高的生命周期阶段。
【讨论】:
我对它们感到困惑。非常感谢以上是关于不知道 NullPoniter 的确切问题的主要内容,如果未能解决你的问题,请参考以下文章