Java 尝试忽略phonenumber开头的零时出错? //获取数据 字符串_phoneNumber=number.getEditText().getText().toString().trim(); 字符串_password=password.getEditText().getText().toString().trim(); 如果(_phoneNumber.charAt(0)='0'){ _phoneNumber=_phoneNumber.substring(1); }
确保您的字符串不是空字符串。您可能有兴趣使用Java 尝试忽略phonenumber开头的零时出错? //获取数据 字符串_phoneNumber=number.getEditText().getText().toString().trim(); 字符串_password=password.getEditText().getText().toString().trim(); 如果(_phoneNumber.charAt(0)='0'){ _phoneNumber=_phoneNumber.substring(1); },java,android,database,firebase,authentication,Java,Android,Database,Firebase,Authentication,确保您的字符串不是空字符串。您可能有兴趣使用startsWith()方法而不是charAt(): 如果您感兴趣,这里有一个指向startsWith()文档的链接:确保您的字符串不是空字符串。您可能有兴趣使用startsWith()方法而不是charAt(): 如果您感兴趣,这里有一个指向startsWith()文档的链接:可能是因为您传递的字符串是空的,所以您可以检查字符“0”,如charAt(0)=“0”不带数字0,因为没有字符的ASCII值为0i运行应用程序并尝试登录而不传递任何数据以检查验
startsWith()
方法而不是charAt()
:
如果您感兴趣,这里有一个指向
startsWith()
文档的链接:确保您的字符串不是空字符串。您可能有兴趣使用startsWith()
方法而不是charAt()
:
如果您感兴趣,这里有一个指向
startsWith()
文档的链接:可能是因为您传递的字符串是空的,所以您可以检查字符“0”,如charAt(0)=“0”不带数字0,因为没有字符的ASCII值为0i运行应用程序并尝试登录而不传递任何数据以检查验证,当我点击登录时,它会给我这个error@Syncit我试过了,但错误仍然是相同的。如果电话号码是空的,就不会有StringIndexOutOfBoundsException(而是NullPointerException)。这是@a_local_nobody从一开始就建议的。可能是因为您传递的字符串为空,所以您需要检查字符“0”,如字符(0)=“0”,而不是数字0,因为没有字符的ASCII值为0i运行应用程序并尝试登录,而不传递任何数据以检查验证,当我点击登录时,它会给我这个error@Syncit我试过了,但错误仍然是相同的。如果电话号码是空的,就不会有StringIndexOutOfBoundsException(而是NullPointerException)。这是从一开始就没有人建议的。
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409)
at android.view.View.performClick(View.java:7125)
at android.view.View.performClickInternal(View.java:7102)
at android.view.View.access$3500(View.java:801)
at android.view.View$PerformClick.run(View.java:27336)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.lang.StringIndexOutOfBoundsException: length=0; index=0
at java.lang.String.charAt(Native Method)
String _phoneNumber = number.getEditText().getText().toString().trim();
String _password = password.getEditText().getText().toString().trim();
if (_phoneNumber.length > 0 && _phoneNumber.startsWith('0')) {
_phoneNumber = _phoneNumber.substring(1);
}