我为Android Studio创建了一个小的软键盘,现在想向文本添加语音,但是在logcat中启动应用程序时出现此错误:
Attempt to invoke virtual method 'android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()' on a null object reference
at android.app.Activity.startActivityForResult(Activity.java:5174)
键盘本身和文本语音转换效果很好。 源代码:
package edmt.dev.androidcustomkeyboard;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.inputmethodservice.InputMethodService;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.media.AudioManager;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputConnection;
import android.widget.ImageButton;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Locale;
public class OrthKeyboard extends InputMethodService implements KeyboardView.OnKeyboardActionListener {
private KeyboardView kv;
private Keyboard keyboard;
public static SpeechInnerClass SpeechInnerClass;
private boolean isCaps = false;
public static String auto_word ="";
//Press Ctrl+O
@Override
public View onCreateInputView() {
kv = (KeyboardView)getLayoutInflater().inflate(R.layout.keyboard,null);
keyboard = new Keyboard(this,R.xml.qwerty);
kv.setKeyboard(keyboard);
kv.setOnKeyboardActionListener(this);
SpeechInnerClass = new SpeechInnerClass();
return kv;
}
@Override
public void onPress(int i) {
}
@Override
public void onRelease(int i) {
}
@Override
public void onKey(int i, int[] ints) {
InputConnection ic = getCurrentInputConnection();
playClick(i);
// MainActivity.count ++;
if(i==-4222522|i==-4222521|i==-4222523|i==-4222525){
if(i==-4222522){
ic.commitText("Case 2",1);
}
if(i==-4222521){
ic.commitText("Case 1",1);
}
if(i==-4222523){
ic.commitText("Case 3",1);
}
if(i==-4222525){
SpeechInnerClass.startVoiceInput();
ic.commitText(SpeechInnerClass.output,1);
}
}
else {
switch (i) {
case Keyboard.KEYCODE_DELETE:
ic.deleteSurroundingText(1, 0);
break;
case Keyboard.KEYCODE_SHIFT:
isCaps = !isCaps;
keyboard.setShifted(isCaps);
kv.invalidateAllKeys();
break;
case Keyboard.KEYCODE_DONE:
ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER));
break;
default:
char code = (char) i;
if (Character.isLetter(code) && isCaps)
code = Character.toUpperCase(code);
ic.commitText(String.valueOf(code), 1);
SettingsActivity.count++;
if ((char) i == ' ') {
auto_word = "";
}
if (96 < i && i < 123) {
auto_word = auto_word + (char) i;
}
}
}
}
private void playClick(int i) {
AudioManager am = (AudioManager)getSystemService(AUDIO_SERVICE);
switch(i)
{
case 32:
am.playSoundEffect(AudioManager.FX_KEYPRESS_SPACEBAR);
break;
case Keyboard.KEYCODE_DONE:
case 10:
am.playSoundEffect(AudioManager.FX_KEYPRESS_RETURN);
break;
case Keyboard.KEYCODE_DELETE:
am.playSoundEffect(AudioManager.FX_KEYPRESS_DELETE);
break;
default: am.playSoundEffect(AudioManager.FX_KEYPRESS_STANDARD);
}
}
@Override
public void onText(CharSequence charSequence) {
}
@Override
public void swipeLeft() {
}
@Override
public void swipeRight() {
}
@Override
public void swipeDown() {
}
@Override
public void swipeUp() {
}
public static class SpeechInnerClass extends AppCompatActivity {
private static final int REQ_CODE_SPEECH_INPUT = 100;
public static CharSequence output = "Test";
private TextView mVoiceInputTv;
private ImageButton mSpeakBtn;
public boolean voice = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(voice=true){
startVoiceInput();
}
}
public void startVoiceInput() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Hello, How can I help you?");
try {
startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
} catch (ActivityNotFoundException a) {
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQ_CODE_SPEECH_INPUT: {
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
output=(result.get(0));
}
break;
}
}
}
}
}
完整Logcat:
2019-07-14 13:23:02.202 12121-12121/? D/libEGL: Emulator has host GPU support, qemu.gles is set to 1.
2019-07-14 13:23:02.211 12121-12121/? D/libEGL: loaded /vendor/lib/egl/libEGL_emulation.so
2019-07-14 13:23:02.214 12121-12121/? D/libEGL: loaded /vendor/lib/egl/libGLESv1_CM_emulation.so
2019-07-14 13:23:02.216 12121-12121/? D/libEGL: loaded /vendor/lib/egl/libGLESv2_emulation.so
2019-07-14 13:39:26.230 12121-12121/? I/dcustomkeyboar: Not late-enabling -Xcheck:jni (already on)
2019-07-14 13:39:26.247 12121-12121/? E/dcustomkeyboar: Unknown bits set in runtime_flags: 0x8000
2019-07-14 13:39:26.247 12121-12121/? W/dcustomkeyboar: Unexpected CPU variant for X86 using defaults: x86
2019-07-14 13:39:26.339 12121-12121/edmt.dev.androidcustomkeyboard I/dcustomkeyboar: The ClassLoaderContext is a special shared library.
2019-07-14 13:39:26.451 12121-12121/edmt.dev.androidcustomkeyboard W/dcustomkeyboar: JIT profile information will not be recorded: profile file does not exits.
2019-07-14 13:39:26.461 12121-12121/edmt.dev.androidcustomkeyboard I/chatty: uid=10132(edmt.dev.androidcustomkeyboard) identical 10 lines
2019-07-14 13:39:26.462 12121-12121/edmt.dev.androidcustomkeyboard W/dcustomkeyboar: JIT profile information will not be recorded: profile file does not exits.
2019-07-14 13:39:26.484 12121-12121/edmt.dev.androidcustomkeyboard I/InstantRun: starting instant run server: is main process
2019-07-14 13:39:26.725 12121-12920/edmt.dev.androidcustomkeyboard D/OpenGLRenderer: Swap behavior 1
2019-07-14 13:39:26.726 12121-12920/edmt.dev.androidcustomkeyboard W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
2019-07-14 13:39:26.727 12121-12920/edmt.dev.androidcustomkeyboard D/OpenGLRenderer: Swap behavior 0
2019-07-14 13:39:26.739 12121-12920/edmt.dev.androidcustomkeyboard D/eglCodecCommon: setVertexArrayObject: set vao to 0 (0) 0 0
2019-07-14 13:39:26.739 12121-12920/edmt.dev.androidcustomkeyboard D/EGL_emulation: eglCreateContext: 0xd85a7cc0: maj 2 min 0 rcv 2
2019-07-14 13:39:26.758 12121-12920/edmt.dev.androidcustomkeyboard D/EGL_emulation: eglMakeCurrent: 0xd85a7cc0: ver 2 0 (tinfo 0xc61374e0)
2019-07-14 13:39:26.778 12121-12920/edmt.dev.androidcustomkeyboard W/Gralloc3: mapper 3.x is not supported
2019-07-14 13:39:26.785 12121-12920/edmt.dev.androidcustomkeyboard D/OpenGLRenderer: Setting buffer count to 3, min_undequeued 1, extraBuffers 0
2019-07-14 13:39:26.814 12121-12920/edmt.dev.androidcustomkeyboard D/EGL_emulation: eglMakeCurrent: 0xd85a7cc0: ver 2 0 (tinfo 0xc61374e0)
2019-07-14 13:39:26.835 12121-12920/edmt.dev.androidcustomkeyboard D/eglCodecCommon: setVertexArrayObject: set vao to 0 (0) 1 0
2019-07-14 13:39:26.944 12121-12920/edmt.dev.androidcustomkeyboard D/EGL_emulation: eglMakeCurrent: 0xd85a7cc0: ver 2 0 (tinfo 0xc61374e0)
2019-07-14 13:39:26.964 12121-12920/edmt.dev.androidcustomkeyboard D/OpenGLRenderer: Setting buffer count to 3, min_undequeued 1, extraBuffers 0
2019-07-14 13:39:26.967 12121-12920/edmt.dev.androidcustomkeyboard D/EGL_emulation: eglMakeCurrent: 0xd85a7cc0: ver 2 0 (tinfo 0xc61374e0)
2019-07-14 13:39:34.439 12121-12920/edmt.dev.androidcustomkeyboard D/OpenGLRenderer: Setting buffer count to 3, min_undequeued 1, extraBuffers 0
2019-07-14 13:39:34.447 12121-12920/edmt.dev.androidcustomkeyboard D/EGL_emulation: eglMakeCurrent: 0xd85a7cc0: ver 2 0 (tinfo 0xc61374e0)
2019-07-14 13:39:34.484 12121-12121/edmt.dev.androidcustomkeyboard E/InputEventReceiver: Exception dispatching input event.
2019-07-14 13:39:34.484 12121-12121/edmt.dev.androidcustomkeyboard E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
2019-07-14 13:39:34.489 12121-12121/edmt.dev.androidcustomkeyboard E/MessageQueue-JNI: java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()' on a null object reference
at android.app.Activity.startActivityForResult(Activity.java:5174)
at android.support.v4.app.BaseFragmentActivityApi16.startActivityForResult(BaseFragmentActivityApi16.java:54)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:67)
at android.app.Activity.startActivityForResult(Activity.java:5131)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:720)
at edmt.dev.androidcustomkeyboard.OrthKeyboard$SpeechInnerClass.startVoiceInput(OrthKeyboard.java:201)
at edmt.dev.androidcustomkeyboard.OrthKeyboard.onKey(OrthKeyboard.java:85)
at android.inputmethodservice.KeyboardView.detectAndSendKey(KeyboardView.java:847)
at android.inputmethodservice.KeyboardView.onModifiedTouchEvent(KeyboardView.java:1354)
at android.inputmethodservice.KeyboardView.onTouchEvent(KeyboardView.java:1217)
at android.view.View.dispatchTouchEvent(View.java:13417)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3060)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2755)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3060)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2755)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3060)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2755)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3060)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2755)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3060)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2755)
at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:460)
at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1849)
at android.app.Dialog.dispatchTouchEvent(Dialog.java:861)
at android.inputmethodservice.SoftInputWindow.dispatchTouchEvent(SoftInputWindow.java:152)
at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:418)
at android.view.View.dispatchPointerEvent(View.java:13676)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:5479)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:5282)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4785)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4838)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4804)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4944)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4812)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:5001)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4785)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4838)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4804)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4812)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4785)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:7502)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:7471)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:7432)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:7627)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:188)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:336)
at android.os.Looper.loop(Looper.java:174)
at android.app.ActivityThread.main(ActivityThread.java:7319)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(Runti
2019-07-14 13:39:34.489 12121-12121/edmt.dev.androidcustomkeyboard D/AndroidRuntime: Shutting down VM
2019-07-14 13:39:34.499 12121-12121/edmt.dev.androidcustomkeyboard E/AndroidRuntime: FATAL EXCEPTION: main
Process: edmt.dev.androidcustomkeyboard, PID: 12121
java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()' on a null object reference
at android.app.Activity.startActivityForResult(Activity.java:5174)
at android.support.v4.app.BaseFragmentActivityApi16.startActivityForResult(BaseFragmentActivityApi16.java:54)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:67)
at android.app.Activity.startActivityForResult(Activity.java:5131)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:720)
at edmt.dev.androidcustomkeyboard.OrthKeyboard$SpeechInnerClass.startVoiceInput(OrthKeyboard.java:201)
at edmt.dev.androidcustomkeyboard.OrthKeyboard.onKey(OrthKeyboard.java:85)
at android.inputmethodservice.KeyboardView.detectAndSendKey(KeyboardView.java:847)
at android.inputmethodservice.KeyboardView.onModifiedTouchEvent(KeyboardView.java:1354)
at android.inputmethodservice.KeyboardView.onTouchEvent(KeyboardView.java:1217)
at android.view.View.dispatchTouchEvent(View.java:13417)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3060)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2755)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3060)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2755)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3060)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2755)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3060)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2755)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3060)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2755)
at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:460)
at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1849)
at android.app.Dialog.dispatchTouchEvent(Dialog.java:861)
at android.inputmethodservice.SoftInputWindow.dispatchTouchEvent(SoftInputWindow.java:152)
at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:418)
at android.view.View.dispatchPointerEvent(View.java:13676)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:5479)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:5282)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4785)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4838)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4804)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4944)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4812)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:5001)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4785)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4838)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4804)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4812)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4785)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:7502)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:7471)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:7432)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:7627)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:188)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:336)
at android.os.Looper.loop(Looper.java:174)
at android.app.ActivityThread.main(ActivityThread.java:7319)
2019-07-14 13:39:34.500 12121-12121/edmt.dev.androidcustomkeyboard E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:934)
答案 0 :(得分:1)
尝试
runOnUiThread(new Runnable() {
@Override
public void run() {
//Code
}
});