-1

我在这里有按钮单击事件的代码:

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按钮时,我的应用程序总是强制关闭。有解决方案吗?

4

1 回答 1

0

尝试这个

if(user.getText().toString().equals ("USER01")|| user.getText().toString().equals ("USER02")) {
  //do your action
}else{
//do your action     
}
于 2013-02-07T04:35:38.007 回答