[英]java.lang.NullPointerException: Attempt to invoke virtual method on a null object reference, while its not null
[英]java.lang.RuntimeException: Unable to start activity and java.lang.NullPointerException: Attempt to invoke virtual method on a null object reference
我正在做一個項目,其中包括:啟動屏幕 >> 入職片段 1 >> 入職片段 2 >> 入職片段 3 >> 發送 OTP 屏幕 >> 驗證 OTP 屏幕 >> 儀表板
因此,在入職片段 3 屏幕中,我添加了一個按鈕“開始使用”,在發送 OTP 屏幕中,我添加了一個按鈕“獲取 OTP”。
因此,當我單擊“開始”按鈕時,它導航到發送 OTP 屏幕,但是當我單擊“獲取 OTP”按鈕時,它返回到載入片段 3 屏幕,而不是轉到驗證 OTP 屏幕。
這是logcat 中的錯誤:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dsproject/com.example.dsproject.verificationOTPtwo}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
這是我的onboardingfragment3.java
代碼:
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_on_boarding3, container,false);
btn = root.findViewById(R.id.get_started_button);
btn.setOnClickListener(v -> {
Intent intent = new Intent(requireActivity().getApplicationContext(), sendOTPone.class);
startActivity(intent);
});
return root;
}
這是我的sendotp.java
代碼:
getotpbutton.setOnClickListener(v -> {
if (!enternumber.getText().toString().trim().isEmpty()){
if ((enternumber.getText().toString().trim()).length()==10){
Intent intent = new Intent(getApplicationContext(),verificationOTPtwo.class);
intent.putExtra("mobile",enternumber.getText().toString());
startActivity(intent);
}else{
Toast.makeText(sendOTPone.this,"Please enter correct number", Toast.LENGTH_SHORT).show();
}
}else{
Toast.makeText(sendOTPone.this, "Enter mobile number", Toast.LENGTH_SHORT).show();
}
});
這是我的驗證OTPtwo.java 代碼:
package com.example.dsproject;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class verificationOTPtwo extends AppCompatActivity {
EditText inputnumber1, inputnumber2, inputnumber3, inputnumber4, inputnumber5, inputnumber6;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_verification_otptwo);
Button verifybuttonclick = findViewById(R.id.buttongetotp);
inputnumber1 = findViewById(R.id.inputotp1);
inputnumber2 = findViewById(R.id.inputotp2);
inputnumber3 = findViewById(R.id.inputotp3);
inputnumber4 = findViewById(R.id.inputotp4);
inputnumber5 = findViewById(R.id.inputotp5);
inputnumber6 = findViewById(R.id.inputotp6);
TextView textView = findViewById(R.id.textmobileshownumber);
textView.setText(String.format(
"+91-%s", getIntent().getStringExtra("mobile")
));
verifybuttonclick.setOnClickListener(v -> {
if (!inputnumber1.getText().toString().trim().isEmpty() && !inputnumber2.getText().toString().trim().isEmpty() && !inputnumber3.getText().toString().trim().isEmpty() && !inputnumber4.getText().toString().trim().isEmpty() && !inputnumber5.getText().toString().trim().isEmpty() && !inputnumber6.getText().toString().trim().isEmpty()){
Toast.makeText(verificationOTPtwo.this, "otp verify", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(verificationOTPtwo.this, "Please enter all number", Toast.LENGTH_SHORT).show();
}
});
numberotpmove();
}
private void numberotpmove() {
inputnumber1.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (!s.toString().trim().isEmpty()){
inputnumber2.requestFocus();
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
inputnumber2.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (!s.toString().trim().isEmpty()){
inputnumber3.requestFocus();
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
inputnumber3.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (!s.toString().trim().isEmpty()){
inputnumber4.requestFocus();
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
inputnumber4.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (!s.toString().trim().isEmpty()){
inputnumber5.requestFocus();
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
inputnumber5.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (!s.toString().trim().isEmpty()){
inputnumber6.requestFocus();
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
}
這就是我能夠查看您的代碼的全部內容。 如果您認為您的問題仍未解決,那么還要添加驗證OTPtwo.class 代碼。
很可能您在verifyOTPtwo中使用了錯誤的按鈕 ID。 這就是它拋出 null 指針異常的原因。
Button verifybuttonclick = findViewById(R.id.buttongetotp);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.