‘void android.view.View.unFocus(android.view.View)‘ on a null object reference

错误日志

java.lang.NullPointerException: Attempt to invoke virtual method 
'void android.view.View.unFocus(android.view.View)' on a null object reference 
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4530) at 

解决:
全局搜索View.removeViewAt(1)并注释掉。

sta"{“appid”:“1110636277”,“splash”:“9011829155178214”,“banner”:“9071427185570273”,“screen”:“6061921175375235”,“market”:{“ali”:“0”,“baidu”:“1”,“huawei”:“0”,“kuan”:“0”,“oppo”:“0”,“qq”:“0”,“samsung”:“0”,“dev360”:“0”,“vivo”:“0”,“xiaomi”:“0”}}"end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
A: 要实现这个功能,我们需要用到flutter的focus_node来监听控件的焦点变化,并根据不同的状态进行样式的更新。同时,我们也需要使用flutter的RawKeyEvent来监听遥控器按键事件。 首先,我们需要引入flutter的库: ```dart import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; ``` 然后,定义一个FocusNode,并在控件上与之关联。这样,在控件获取或失去焦点时,就会触发这个FocusNode的监听事件: ```dart FocusNode _focusNode = FocusNode(); ... Widget build(BuildContext context) { return Scaffold( body: Focus( focusNode: _focusNode, child: Container( width: 200, height: 200, decoration: BoxDecoration( color: _focusNode.hasFocus ? Colors.red : Colors.blue, ) ) ) ); } ``` 在上面的代码中,我们给一个Container控件添加了焦点监听,并且在控件中添加了一个背景颜色。当该控件获取焦点时,背景颜色变为红色,失去焦点时则变为蓝色。 接下来,我们需要使用RawKeyEvent来监听遥控器按键事件。这里我们只监听了上下左右和确定返回键: ```dart RawKeyboardListener( focusNode: FocusNode(), onKey: (RawKeyEvent event) { if (event is RawKeyDownEvent) { if (event.logicalKey == LogicalKeyboardKey.arrowUp) { // up key is pressed } else if (event.logicalKey == LogicalKeyboardKey.arrowDown) { // down key is pressed } else if (event.logicalKey == LogicalKeyboardKey.arrowLeft) { // left key is pressed } else if (event.logicalKey == LogicalKeyboardKey.arrowRight) { // right key is pressed } else if (event.logicalKey == LogicalKeyboardKey.select) { // enter key is pressed } else if (event.logicalKey == LogicalKeyboardKey.backspace) { // back key is pressed } } }, child: Container() ), ``` 在监听到不同的遥控器按键事件后,我们可以对当前焦点的控件进行样式的更新,例如: ```dart RawKeyboardListener( focusNode: FocusNode(), onKey: (RawKeyEvent event) { if (event is RawKeyDownEvent) { if (event.logicalKey == LogicalKeyboardKey.arrowUp) { // up key is pressed if (_focusNode.hasFocus) { setState(() { _focusNode.nextFocus(); }); } } else if (event.logicalKey == LogicalKeyboardKey.arrowDown) { // down key is pressed if (_focusNode.hasFocus) { setState(() { _focusNode.previousFocus(); }); } } else if (event.logicalKey == LogicalKeyboardKey.arrowLeft) { // left key is pressed if (_focusNode.hasFocus) { setState(() { _focusNode.requestFocus(FocusNode().canRequestFocus ? FocusNode() : null); }); } } else if (event.logicalKey == LogicalKeyboardKey.arrowRight) { // right key is pressed if (_focusNode.hasFocus) { setState(() { _focusNode.requestFocus(FocusNode().canRequestFocus ? FocusNode() : null); }); } } else if (event.logicalKey == LogicalKeyboardKey.select) { // enter key is pressed if (_focusNode.hasFocus) { setState(() { // update the selected widget style }); } } else if (event.logicalKey == LogicalKeyboardKey.backspace) { // back key is pressed if (_focusNode.hasFocus) { setState(() { _focusNode.unfocus(); }); } } } }, child: Container() ), ``` 在上面的代码中,我们根据遥控器按键事件的不同,对控件的焦点和样式进行了更新。例如,当焦点控件为当前控件时,按下上下左右键可移动焦点;按下确定键时,更新当前控件样式。 以上就是实现这个功能的简单示例代码,我们可以在实际开发中根据需要进行修改和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值