简体   繁体   English

自定义键盘抛出异常

[英]Custom Keyboard Throwing Exceptions

I have a simple enough idea, which is to make a custom keyboard instead of using the built in keyboard to avoid autocorrect, symbols, numbers, etc. which I don't want in my app. 我有一个简单的想法,那就是制作一个自定义键盘,而不是使用内置键盘来避免自动校正,符号,数字等,这些在我的应用程序中是我所不希望的。 For some reason, when I call the event typeKey to "type" a letter, the event calling itself throws an exception. 出于某种原因,当我将事件typeKey调用为“键入”字母时,调用自身的事件会引发异常。

Here is the java code: 这是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);
    }
}

Here is the relevant XML: 这是相关的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" />

The goal is to be able to type and have the typed text show up as the textview. 目的是能够键入文字并显示为textview。 My thought to do this was to add to every key an event, typeKey, which would get the text from whatever button called the event, add it to the vector "answer", and then set the textview to display the text of "answer". 我的想法是将一个事件typeKey添加到每个键,该事件将从调用该事件的任何按钮获取文本,将其添加到矢量“ answer”,然后将textview设置为显示“ answer”文本。

Here is the whole stack trace: 这是整个堆栈跟踪:

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

Thanks in advance for any help. 在此先感谢您的帮助。

Three issues: 三个问题:

You are trying to search for your answerbox view before you call setContentView(R.layout.activity_main) . 您试图在调用setContentView(R.layout.activity_main)之前搜索您的answerbox视图。 This will always result in answerbox being null since there are no views available (yet) and throw a NullPointerException when you try to access answerbox . 这将始终导致answerbox为null,因为还没有可用的视图(尚未),并且在您尝试访问answerbox时抛出NullPointerException Make sure to switch the order and search for answerbox after you call setContentView(R.layout.activity_main) 调用setContentView(R.layout.activity_main)后,请确保切换顺序并搜索answerbox

Ex. 防爆。

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

Second, change your click method to take a View as the parameter (instead of Object ) as onClick methods expect this.. Although, I think what you have should be ok since a View technically is an object and you are casting it to the appropriate type. 其次,更改您的click方法,以将View用作参数(而不是Object ),就像onClick方法期望的那样。尽管我认为您应该拥有的,因为View从技术上讲是一个对象,并且将其强制转换为适当的对象类型。 I'm not sure how picky it is when trying to call the click listener so for safety I'd just change it to View . 我不确定尝试调用Click侦听器时有多挑剔,因此为了安全起见,我将其更改为View Ex. 防爆。

public void typeKey(View sender)
{
        Button pressed = (Button) sender;
        answer.add(ansLength, (String) pressed.getText());
        ansLength++;
        answerbox.setText((CharSequence) answer);
}

Lastly, 最后,

You cannot simply cast your vector to a CharSequence. 您不能简单地将向量转换为CharSequence。 If you wish to make a string out of it you will have to loop through each value and add it to a larger string.. Ex. 如果要使用它来制作一个字符串,则必须遍历每个值并将其添加到更大的字符串中。

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());
}

You should also start your ansLength at 0 since the vectors are zero indexed. 您还应该将ansLength从0开始,因为向量是零索引的。

int ansLength = 1;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM