// 文档地址 https://docs.hamibot.com/
"ui"; // 仍建议声明UI模式(如果脚本有UI需求)
// 创建悬浮窗
var floatyWin = floaty.window(
<vertical bg="#33000000" gravity="center">
<horizontal>
<button id="startBtn" text="开始" w="60" h="30" textSize="12sp"/>
<button id="configBtn" text="配置" w="60" h="30" textSize="12sp"/>
<button id="exitBtn" text="退出" w="60" h="30" textSize="12sp"/>
</horizontal>
</vertical>
);
// 设置初始位置(避开系统状态栏)
floatyWin.setPosition(device.width - 500, 50);
// 按钮事件
floatyWin.exitBtn.click(() => {
floatyWin.close();
hamibot.exit();
});
floatyWin.configBtn.click(() => {
ui.run(() => {
dialogs.build({
title: "配置",
view: ui.inflate(
<vertical padding="16">
<text textSize="16sp" textStyle="bold" marginBottom="10">基本设置</text>
<linear gravity="center_vertical" marginBottom="10">
<text text="调试模式:" width="100" />
<switch id="debugSwitch" checked={config.debugMode} />
</linear>
<linear gravity="center_vertical" marginBottom="10">
<text text="操作延迟:" width="100" />
<numberpicker id="delayPicker" minValue="500" maxValue="3000" value={config.waitTime.operation/100} layout_weight="1" />
<text text="毫秒" marginLeft="5" />
</linear>
<linear gravity="center_vertical" marginBottom="10">
<text text="循环间隔:" width="100" />
<numberpicker id="loopPicker" minValue="500" maxValue="2000" value={config.waitTime.loopInterval/100} layout_weight="1" />
<text text="毫秒" marginLeft="5" />
</linear>
<linear gravity="center_vertical" marginBottom="10">
<text text="最大重试次数:" width="100" />
<numberpicker id="retryPicker" minValue="1" maxValue="10" value={config.waitTime.maxRetry} layout_weight="1" />
</linear>
</vertical>
),
positive: "确定" }).show();
});
});
// 保持脚本运行
setInterval(() => {}, 1000); 运行后点击 配置按钮直接闪退
JavaException: android.view.InflateException: android.view.InflateException: java.lang.ClassNotFoundException: android.widget.switch 按照给出的回答代码运行报错
修改后 窗口正常显示 但是 view: ui.inflate(
<vertical padding="16">
<text textSize="16sp" textStyle="bold" marginBottom="10">基本设置</text>
<linear gravity="center_vertical" marginBottom="10">
<text text="调试模式:" width="100" />
<Switch id="debugSwitch" checked={false} />
</linear>
<linear gravity="center_vertical" marginBottom="10">
<text text="操作延迟:" width="100" />
<NumberPicker id="delayPicker" minValue="500" maxValue="3000" value={1000} layout_weight="1" />
<text text="毫秒" marginLeft="5" />
</linear>
<linear gravity="center_vertical" marginBottom="10">
<text text="循环间隔:" width="100" />
<NumberPicker id="loopPicker" minValue="500" maxValue="2000" value={1000} layout_weight="1" />
<text text="毫秒" marginLeft="5" />
</linear>
<linear gravity="center_vertical" marginBottom="10">
<text text="最大重试次数:" width="100" />
<NumberPicker id="retryPicker" minValue="1" maxValue="10" value={3} layout_weight="1" />
</linear>
</vertical>
), 这部分内容并没有显示
回答
这个问题通常是由于在UI线程外直接操作UI元素导致的。在Hamibot中,对UI的操作必须在ui.run()回调中进行。以下是修正后的代码:
// 文档地址 https://docs.hamibot.com/
"ui"; // 确保声明UI模式
// 创建悬浮窗
var floatyWin = floaty.window(
<vertical bg="#33000000" gravity="center">
<horizontal>
<button id="startBtn" text="开始" w="60" h="30" textSize="12sp"/>
<button id="configBtn" text="配置" w="60" h="30" textSize="12sp"/>
<button id="exitBtn" text="退出" w="60" h="30" textSize="12sp"/>
</horizontal>
</vertical>
);
// 设置初始位置
floatyWin.setPosition(device.width - 500, 50);
// 按钮事件
floatyWin.exitBtn.click(() => {
floatyWin.close();
hamibot.exit();
});
floatyWin.configBtn.click(() => {
ui.run(() => {
try {
dialogs.build({
title: "配置",
view: ui.inflate(
<vertical padding="16">
<text textSize="16sp" textStyle="bold" marginBottom="10">基本设置</text>
<linear gravity="center_vertical" marginBottom="10">
<text text="调试模式:" width="100" />
<switch id="debugSwitch" checked={false} />
</linear>
<linear gravity="center_vertical" marginBottom="10">
<text text="操作延迟:" width="100" />
<numberpicker id="delayPicker" minValue="500" maxValue="3000" value={1000} layout_weight="1" />
<text text="毫秒" marginLeft="5" />
</linear>
<linear gravity="center_vertical" marginBottom="10">
<text text="循环间隔:" width="100" />
<numberpicker id="loopPicker" minValue="500" maxValue="2000" value={1000} layout_weight="1" />
<text text="毫秒" marginLeft="5" />
</linear>
<linear gravity="center_vertical" marginBottom="10">
<text text="最大重试次数:" width="100" />
<numberpicker id="retryPicker" minValue="1" maxValue="10" value={3} layout_weight="1" />
</linear>
</vertical>
),
positive: "确定"
}).show();
} catch(e) {
toastLog("配置对话框出错: " + e);
}
});
});
// 保持脚本运行
setInterval(() => {}, 1000);
主要修改点:
- 确保所有UI操作都在ui.run()回调中执行
- 移除了未定义的config对象引用
- 添加了try-catch捕获可能的错误
- 给numberpicker设置了默认值
如果仍有闪退问题,请检查:
- 是否已开启悬浮窗权限
- 检查日志是否有报错信息
- 试着在更简单的UI上测试是否正常
补充回答 1
这个错误表明 android.widget.switch
类在当前的 Android 系统版本中不可用。在 Android 4.0 以上版本,正确的 Switch 控件类名应该是 android.widget.Switch
(注意大写 S)。
以下是修正后的代码版本:
// 文档地址 https://docs.hamibot.com/
"ui";
// 创建悬浮窗
var floatyWin = floaty.window(
<vertical bg="#33000000" gravity="center">
<horizontal>
<button id="startBtn" text="开始" w="60" h="30" textSize="12sp"/>
<button id="configBtn" text="配置" w="60" h="30" textSize="12sp"/>
<button id="exitBtn" text="退出" w="60" h="30" textSize="12sp"/>
</horizontal>
</vertical>
);
floatyWin.setPosition(device.width - 500, 50);
floatyWin.exitBtn.click(() => {
floatyWin.close();
hamibot.exit();
});
floatyWin.configBtn.click(() => {
ui.run(() => {
try {
dialogs.build({
title: "配置",
view: ui.inflate(
<vertical padding="16">
<text textSize="16sp" textStyle="bold" marginBottom="10">基本设置</text>
<linear gravity="center_vertical" marginBottom="10">
<text text="调试模式:" width="100" />
<Switch id="debugSwitch" checked={false} />
</linear>
<linear gravity="center_vertical" marginBottom="10">
<text text="操作延迟:" width="100" />
<NumberPicker id="delayPicker" minValue="500" maxValue="3000" value={1000} layout_weight="1" />
<text text="毫秒" marginLeft="5" />
</linear>
<linear gravity="center_vertical" marginBottom="10">
<text text="循环间隔:" width="100" />
<NumberPicker id="loopPicker" minValue="500" maxValue="2000" value={1000} layout_weight="1" />
<text text="毫秒" marginLeft="5" />
</linear>
<linear gravity="center_vertical" marginBottom="10">
<text text="最大重试次数:" width="100" />
<NumberPicker id="retryPicker" minValue="1" maxValue="10" value={3} layout_weight="1" />
</linear>
</vertical>
),
positive: "确定"
}).show();
} catch(e) {
toastLog("配置对话框出错: " + e);
}
});
});
setInterval(() => {}, 1000);
修改要点:
- 将
<switch>
改为<Switch>
(首字母大写) - 将
<numberpicker>
改为<NumberPicker>
(首字母大写) - 保持其他布局和逻辑不变
Hamibot 的 UI 系统基于 Android 原生控件,所以组件的 XML 标签名称必须与其对应的 Java 类名一致,注意区分大小写。如果仍然有问题,可以尝试使用更基础的控件如 CheckBox 替代 Switch。
补充回答 2
问题可能出在 dialogs.build()
的视图没有正确加载。以下是修正后的版本,确保对话框内容能够正常显示:
// 文档地址 https://docs.hamibot.com/
"ui";
// 创建悬浮窗
var floatyWin = floaty.window(
<vertical bg="#33000000" gravity="center">
<horizontal>
<button id="startBtn" text="开始" w="60" h="30" textSize="12sp"/>
<button id="configBtn" text="配置" w="60" h="30" textSize="12sp"/>
<button id="exitBtn" text="退出" w="60" h="30" textSize="12sp"/>
</horizontal>
</vertical>
);
floatyWin.setPosition(device.width - 500, 50);
floatyWin.exitBtn.click(() => {
floatyWin.close();
hamibot.exit();
});
floatyWin.configBtn.click(() => {
// 先在UI线程中创建View对象
var configView = ui.inflate(
<vertical padding="16">
<text textSize="16sp" textStyle="bold" marginBottom="16">基本设置</text>
<linear gravity="center_vertical" marginBottom="16">
<text text="调试模式:" width="100" />
<Switch id="debugSwitch" checked={false} />
</linear>
<linear gravity="center_vertical" marginBottom="16">
<text text="操作延迟:" width="100" />
<NumberPicker id="delayPicker" minValue="500" maxValue="3000" value={1000} layout_weight="1" />
<text text="毫秒" marginLeft="5" />
</linear>
<linear gravity="center_vertical" marginBottom="16">
<text text="循环间隔:" width="100" />
<NumberPicker id="loopPicker" minValue="500" maxValue="2000" value={1000} layout_weight="1" />
<text text="毫秒" marginLeft="5" />
</linear>
<linear gravity="center_vertical" marginBottom="16">
<text text="最大重试次数:" width="100" />
<NumberPicker id="retryPicker" minValue="1" maxValue="10" value={3} layout_weight="1" />
</linear>
</vertical>
);
// 然后在UI线程中显示对话框
ui.run(() => {
try {
dialogs.build({
title: "配置",
view: configView,
positive: "确定",
negative: "取消"
}).show();
} catch(e) {
toastLog("配置对话框出错: " + e);
}
});
});
setInterval(() => {}, 1000);
主要修改点:
-
预先inflate视图:先创建好View对象,再传递给对话框
-
调整布局参数:
- 增加了更大的间距(marginBottom="16")
- 添加了"取消"按钮
-
确保UI线程执行:所有UI操作都在ui.run()中完成
如果仍然不显示,可以尝试:
- 检查Hamibot版本是否为最新
- 单独测试简单的对话框确认是否能显示
- 替换
NumberPicker
为普通的EditText
测试控件兼容性