ここに、ボタン クリック イベントのコードがあります。
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
ボタンをクリックするたびに、アプリは常に強制終了します。これに対する解決策はありますか?