我有一个足够简单的想法,那就是制作一个自定义键盘,而不是使用内置键盘来避免我的应用程序中不需要的自动更正、符号、数字等。出于某种原因,当我调用事件 typeKey 来“键入”一个字母时,调用自身的事件会引发异常。
这是java代码:
public class MainActivity extends Activity {
Vector<String> answer = new Vector<String>(1,1);
int ansLength = 1;
private TextView answerbox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
answerbox = (TextView) findViewById(R.id.answerbox);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void typeKey(Object sender)
{
Button pressed = (Button) sender;
answer.add(ansLength, (String) pressed.getText());
ansLength++;
answerbox.setText((CharSequence) answer);
}
}
以下是相关的 XML:
<Button
android:id="@+id/q"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="Q"
android:onClick="typeKey"/>
<TextView
android:id="@+id/answerbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="43dp"
android:layout_y="40dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
目标是能够键入并将键入的文本显示为文本视图。我这样做的想法是为每个键添加一个事件 typeKey,它将从调用该事件的任何按钮获取文本,将其添加到向量“answer”,然后设置 textview 以显示“answer”的文本.
这是整个堆栈跟踪:
08-06 13:04:49.497: E/AndroidRuntime(15224): FATAL EXCEPTION: main
08-06 13:04:49.497: E/AndroidRuntime(15224): java.lang.IllegalStateException: Could not execute method of the activity
08-06 13:04:49.497: E/AndroidRuntime(15224): at android.view.View$1.onClick(View.java:3698)
08-06 13:04:49.497: E/AndroidRuntime(15224): at android.view.View.performClick(View.java:4222)
08-06 13:04:49.497: E/AndroidRuntime(15224): at android.view.View$PerformClick.run(View.java:17273)
08-06 13:04:49.497: E/AndroidRuntime(15224): at android.os.Handler.handleCallback(Handler.java:615)
08-06 13:04:49.497: E/AndroidRuntime(15224): at android.os.Handler.dispatchMessage(Handler.java:92)
08-06 13:04:49.497: E/AndroidRuntime(15224): at android.os.Looper.loop(Looper.java:137)
08-06 13:04:49.497: E/AndroidRuntime(15224): at android.app.ActivityThread.main(ActivityThread.java:4895)
08-06 13:04:49.497: E/AndroidRuntime(15224): at java.lang.reflect.Method.invokeNative(Native Method)
08-06 13:04:49.497: E/AndroidRuntime(15224): at java.lang.reflect.Method.invoke(Method.java:511)
08-06 13:04:49.497: E/AndroidRuntime(15224): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
08-06 13:04:49.497: E/AndroidRuntime(15224): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
08-06 13:04:49.497: E/AndroidRuntime(15224): at dalvik.system.NativeStart.main(Native Method)
08-06 13:04:49.497: E/AndroidRuntime(15224): Caused by: java.lang.reflect.InvocationTargetException
08-06 13:04:49.497: E/AndroidRuntime(15224): at java.lang.reflect.Method.invokeNative(Native Method)
08-06 13:04:49.497: E/AndroidRuntime(15224): at java.lang.reflect.Method.invoke(Method.java:511)
08-06 13:04:49.497: E/AndroidRuntime(15224): at android.view.View$1.onClick(View.java:3693)
08-06 13:04:49.497: E/AndroidRuntime(15224): ... 11 more
08-06 13:04:49.497: E/AndroidRuntime(15224): Caused by: java.lang.ClassCastException: java.util.Vector cannot be cast to java.lang.CharSequence
08-06 13:04:49.497: E/AndroidRuntime(15224): at gguday.lexiconicmkii.MainActivity.typeKey(MainActivity.java:42)
08-06 13:04:49.497: E/AndroidRuntime(15224): ... 14 more
提前感谢您的帮助。