我有一个简单的想法,那就是制作一个自定义键盘,而不是使用内置键盘来避免自动校正,符号,数字等,这些在我的应用程序中是我所不希望的。出于某种原因,当我将事件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" />
目的是能够键入文字并显示为textview。我的想法是将一个事件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
在此先感谢您的帮助。
最佳答案
三个问题:
您试图在呼叫answerbox
之前搜索您的setContentView(R.layout.activity_main)
视图。这将始终导致answerbox
为空,因为还没有可用的视图(尚未),当您尝试访问NullPointerException
时会抛出answerbox
。致电answerbox
后,请确保切换顺序并搜索setContentView(R.layout.activity_main)
例如
setContentView(R.layout.activity_main);
answerbox = (TextView) findViewById(R.id.answerbox);
其次,更改您的click方法,以使用
View
作为参数(而不是Object
),就像onClick方法期望的那样。尽管我认为View在技术上是一个对象,并且您正在投射,但您认为应该没问题将其设置为适当的类型。我不确定尝试调用Click侦听器时有多挑剔,因此为了安全起见,我将其更改为View
。例如
public void typeKey(View sender)
{
Button pressed = (Button) sender;
answer.add(ansLength, (String) pressed.getText());
ansLength++;
answerbox.setText((CharSequence) answer);
}
最后,
您不能简单地将向量转换为CharSequence。如果要使用它来制作一个字符串,则必须遍历每个值并将其添加到更大的字符串中。
public void typeKey(View sender)
{
Button pressed = (Button) sender;
answer.add(ansLength, (String) pressed.getText());
ansLength++;
StringBuilder stringBuilder = new StringBuilder();
for (String string : answer) {
stringBuilder.append(string);
}
answerbox.setText(stringBuilder.toString());
}
您还应该将ansLength从0开始,因为向量是零索引的。
int ansLength = 1;