我正在为Android构建一个IME (输入法编辑器)。我知道我必须创建一个扩展InputMethodService的类,才能访问getCurrentInputConnection。我的理解是,这会将我返回到当前聚焦的文本字段,如果没有,则返回null。
我知道我必须做这样的事:
val focusedTextField = currentInputConnection ?: return问题是,结果我总是得到null。我的理论是,文本编辑器(当前关注的文本字段)不承认我的应用程序是一个IME,或者可能“没有意识到”它正在被关注。所以也许我得提供更多的信息。我已经检查了声明服务和提供元数据的清单,一切似乎都是正确的。res/xml/method.xml文件是正确的。
这是清单文件。我被告知,从android 11开始,我们必须获得使用服务的位置许可。
<service
android:name=".IMEService"
android:label="Amazing Keyboard"
android:foregroundServiceType="location"
android:permission="android.permission.BIND_INPUT_METHOD"
android:exported="true">
<intent-filter>
<action android:name="android.view.InputMethod" />
</intent-filter>
<meta-data
android:name="android.view.im"
android:resource="@xml/method" />
</service>这是方法文件
<?xml version="1.0" encoding="utf-8"?>
<input-method xmlns:android="http://schemas.android.com/apk/res/android"
android:settingsActivity="com.example.amazingkeyboard.MainActivity">
<subtype
android:name = "my app"
android:label="English (U.S)"
android:imeSubtypeLocale="en_US"
android:imeSubtypeMode="keyboard"/>
</input-method>这就是我所做的,我正在使用jetpack撰写,但这不是问题所在,因为我已经尝试返回一个xml视图,而且我总是有错误的。
class IMEService : InputMethodService(), LifecycleOwner,
ViewModelStoreOwner, SavedStateRegistryOwner {
fun sendText(text : CharSequence, newCursorPosition : Int) {
val focusedTextField = currentInputConnection ?: return //always returns null
focusedTextField.commitText(text, newCursorPosition)
}
...
}这就是我称之为方法的地方。
val connection = IMEService()
@Composable fun TestKey(modifier: Modifier = Modifier) {
Key(
modifier = modifier .clickable {
connection.sendText(unicodeToString(0x1F436), 1)
}...当我删除验证时。正如我前面所说的,问题是我总是得到null。显然,如果我进行验证,就不会出现错误,但是我也不能发送(因为我总是有空)。
// val focusedTextField = currentInputConnection ?: return
val focusedTextField = currentInputConnection我有这个错误。
java.lang.NullPointerException:
Attempt to invoke interface method 'boolean android.view.inputmethod.InputConnection.commitText(java.lang.CharSequence, int)' on a null object reference
at com.chrrissoft.amazingkeyboard.IMEService.sendText(IMEService.kt:21)
at com.chrrissoft.amazingkeyboard.composables.GeneralKeysKt$TestKey$1.invoke(generalKeys.kt:32)
at com.chrrissoft.amazingkeyboard.composables.GeneralKeysKt$TestKey$1.invoke(generalKeys.kt:31)这里是一个完整的项目,如果您想查看它的话。
发布于 2022-04-07 13:58:55
您将在以下代码的第二行获得NullPointerException:
val focusedTextField = currentInputConnection
focusedTextField.commitText(text, newCursorPosition) 因为当前活动的InputConnection没有绑定到输入方法,所以currentInputConnection是空的。
在onBindInput中有一个InputConnection方法,当新客户机绑定到输入方法时调用该方法。在这个调用中,您知道currentInputConnection返回有效的对象。因此,在使用currentInputConnection之前,客户端应该绑定到输入方法。
要在类范围之外使用IMEService的公共方法,需要有绑定服务的实例。深入了解GitHub上的源代码,似乎很容易通过将IMEService传递给TestKey()函数来解决问题。整个代码如下所示:
@Composable
fun TestKey(modifier: Modifier = Modifier, connection: IMEService) {
Key(
modifier = modifier
//...
.clickable {
connection.sendText(unicodeToString(0x1F383), 1)
}
) {
Icon(/*...*/)
}
}
@Composable
fun EmojiLayout(navController: NavHostController, connection: IMEService) {
val (currentPage, onPageChange) = remember {
mutableStateOf(EmoticonsAndEmotionsPage)
}
Column(modifier = Modifier.fillMaxSize()) {
EmojiPage(
//...
)
Row(
//...
) {
//...
TestKey(Modifier.weight(1f), connection)
}
}
}
@Composable
fun KeyboardScreen(connection: IMEService) {
AmazingKeyboardTheme() {
Column(
//...
) {
val navController = rememberNavController()
NavHost(navController = navController, startDestination = "qwertyLayout") {
//...
composable("emojiLayout") { EmojiLayout(navController, connection) }
}
}
}
}
class AndroidKeyboardView(context: Context) : FrameLayout(context) {
constructor(service: IMEService) : this(service as Context) {
inflate(service, R.layout.keyboard_view, this)
findViewById<ComposeView>(R.id.compose_view).setContent {
KeyboardScreen(connection = service)
}
}
}IMEService类保持不变。
发布于 2022-04-02 09:41:20
https://stackoverflow.com/questions/71713785
复制相似问题