我這裏有我的按鈕點擊事件代碼:if語句做不對的事在我的代碼
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
按鈕,我的應用程序總是強制關閉。 有沒有解決方案?
如果您的應用程序強制關閉,有可能寫入日誌異常。看看日誌,找出發生了什麼問題。 –
任何logcat痕跡? – dumbfingers
我點擊我的按鈕後添加了我的logcat。謝謝! –