自定义键盘抛出异常

时间:2013-08-06 19:16:36

标签: java android exception

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

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

三个问题:

您在致电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);

其次,更改点击方法以View作为参数(而不是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;