我这里有我点击按钮点击事件的代码:
public void register(View view) {
EditText user = (EditText) findViewById(R.id.registerEditText);
if("USER01".equals(user.getText().toString()) || "USER02".equals(user.getText().toString())) {
Toast.makeText(RegisterActivity.this,"Correct username!", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(RegisterActivity.this, "Only USER01 and USER02 is allowed for username!", Toast.LENGTH_SHORT).show();
}
}
这是我的XML布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="@drawable/green_bg"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/first"
android:textSize="32sp"
android:shadowColor="#F70000"
android:shadowDx="2.5"
android:shadowDy="2.5"
android:textColor="#FFFDFD"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/registerEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:textColor="#DDFED1"
android:hint="@string/user"
android:inputType="text|textCapCharacters" >
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="register"
android:text="@string/register" />
点击按钮后,我的LogCat显示了什么:
02-07 11:10:47.155: E/AndroidRuntime(556): FATAL EXCEPTION: main
02-07 11:10:47.155: E/AndroidRuntime(556): java.lang.IllegalStateException: Could not execute method of the activity
02-07 11:10:47.155: E/AndroidRuntime(556): at android.view.View$1.onClick(View.java:3039)
02-07 11:10:47.155: E/AndroidRuntime(556): at android.view.View.performClick(View.java:3480)
02-07 11:10:47.155: E/AndroidRuntime(556): at android.view.View$PerformClick.run(View.java:13983)
02-07 11:10:47.155: E/AndroidRuntime(556): at android.os.Handler.handleCallback(Handler.java:605)
02-07 11:10:47.155: E/AndroidRuntime(556): at android.os.Handler.dispatchMessage(Handler.java:92)
02-07 11:10:47.155: E/AndroidRuntime(556): at android.os.Looper.loop(Looper.java:137)
02-07 11:10:47.155: E/AndroidRuntime(556): at android.app.ActivityThread.main(ActivityThread.java:4340)
02-07 11:10:47.155: E/AndroidRuntime(556): at java.lang.reflect.Method.invokeNative(Native Method)
02-07 11:10:47.155: E/AndroidRuntime(556): at java.lang.reflect.Method.invoke(Method.java:511)
02-07 11:10:47.155: E/AndroidRuntime(556): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-07 11:10:47.155: E/AndroidRuntime(556): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-07 11:10:47.155: E/AndroidRuntime(556): at dalvik.system.NativeStart.main(Native Method)
02-07 11:10:47.155: E/AndroidRuntime(556): Caused by: java.lang.reflect.InvocationTargetException
02-07 11:10:47.155: E/AndroidRuntime(556): at java.lang.reflect.Method.invokeNative(Native Method)
02-07 11:10:47.155: E/AndroidRuntime(556): at java.lang.reflect.Method.invoke(Method.java:511)
02-07 11:10:47.155: E/AndroidRuntime(556): at android.view.View$1.onClick(View.java:3034)
02-07 11:10:47.155: E/AndroidRuntime(556): ... 11 more
02-07 11:10:47.155: E/AndroidRuntime(556): Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
02-07 11:10:47.155: E/AndroidRuntime(556): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:434)
02-07 11:10:47.155: E/AndroidRuntime(556): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
02-07 11:10:47.155: E/AndroidRuntime(556): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
02-07 11:10:47.155: E/AndroidRuntime(556): at tsu.ccs.capstone.RegisterActivity.retrieveNumber(RegisterActivity.java:38)
02-07 11:10:47.155: E/AndroidRuntime(556): at tsu.ccs.capstone.RegisterActivity.register(RegisterActivity.java:46)
02-07 11:10:47.155: E/AndroidRuntime(556): ... 14 more
这是一个简单的测试人员,我想出来检查我的IF statement
是否正常工作。但不幸的是,每当我点击register
按钮时,我的应用程序总是强行关闭。
有解决方案吗?
答案 0 :(得分:0)
试试这个
if(user.getText().toString().equals ("USER01")|| user.getText().toString().equals ("USER02")) {
//do your action
}else{
//do your action
}