0

我有一个足够简单的想法,那就是制作一个自定义键盘,而不是使用内置键盘来避免我的应用程序中不需要的自动更正、符号、数字等。出于某种原因,当我调用事件 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

提前感谢您的帮助。

4

1 回答 1

1

三个问题:

您正在尝试在调用之前搜索您的answerbox视图setContentView(R.layout.activity_main)。这将始终导致answerbox为空,因为没有可用的视图(还)并且NullPointerException在您尝试访问时抛出 a answerboxanswerbox打电话后一定要切换顺序搜索setContentView(R.layout.activity_main)

前任。

setContentView(R.layout.activity_main);
answerbox = (TextView) findViewById(R.id.answerbox);

其次,更改您的 click 方法以将 aView作为参数(而不是Object),因为 onClick 方法期望这样做。虽然,我认为您所拥有的应该没问题,因为从技术上讲 View 是一个对象,并且您正在将其转换为适当的类型。我不确定在尝试调用点击侦听器时有多挑剔,所以为了安全起见,我将其更改为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());
}

您还应该从 0 开始您的 ansLength,因为向量是零索引的。

int ansLength = 1;
于 2013-08-06T19:35:39.147 回答