Android:尝试启动 BluetoothSettings 活动强制关闭并出现 NullPointerException

发布于 2024-10-03 01:00:16 字数 4203 浏览 0 评论 0原文

我正在尝试打开蓝牙设置的标准窗口(包含设备名称、可发现性等)。

但是,startActivity(intent) 的一般方法以指向 BluetoothSettings.java onCreate:135NullPointerException 结束。

检查 Android 代码,我发现在第 135 行,他们从意图中获得了一些额外的内容。因此,我准备了相同的额外内容(我在 android 核心 BluetoothDevicePicker 接口中找到的名称)并发出它 - 与 NullPointerException 具有相同的效果。

可能是我准备的额外的名称有误?

那么,当我像用户一样手动打开蓝牙设置时,有没有一种方法可以从系统本身提交的意图中看到这些额外内容(尤其是名称)?

非常感谢。

我的代码是:

Intent settingsIntent = new Intent();
settingsIntent.setClassName("com.android.settings", "com.android.settings.bluetooth.BluetoothSettings");
settingsIntent.putExtra("android.bluetooth.devicepicker.extra.LAUNCH_PACKAGE", "mypackage.bttoggle");
settingsIntent.putExtra("android.bluetooth.devicepicker.extra.DEVICE_PICKER_LAUNCH_CLASS", "mypackage.bttoggle.BluetoothWidget");
PendingIntent settingsPendingIntent = PendingIntent.getActivity(context, 0, settingsIntent, 0);
views.setOnClickPendingIntent(R.id.btnSettings, settingsPendingIntent);

它是一个小部件,因此我需要使用 PendingIntent

此代码在运行时出现以下异常:

错误/AndroidRuntime(4905):未捕获的处理程序:线程主线程因退出而退出 未捕获的异常错误/AndroidRuntime(4905): java.lang.RuntimeException:无法启动活动 组件信息{com.android.settings/com.android.settings.bluetooth.BluetoothSettings}: java.lang.NullPointerException 错误/AndroidRuntime(4905):位于 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2596) 错误/AndroidRuntime(4905):位于 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2621) 错误/AndroidRuntime(4905):位于 android.app.ActivityThread.access$2200(ActivityThread.java:126) 错误/AndroidRuntime(4905):位于 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1932) 错误/AndroidRuntime(4905):位于 android.os.Handler.dispatchMessage(Handler.java:99) 错误/AndroidRuntime(4905):位于 android.os.Looper.loop(Looper.java:123) 错误/AndroidRuntime(4905):
在 android.app.ActivityThread.main(ActivityThread.java:4595) 错误/AndroidRuntime(4905):位于 java.lang.reflect.Method.invokeNative(本机方法) 错误/AndroidRuntime(4905):位于 java.lang.reflect.Method.invoke(Method.java:521) 错误/AndroidRuntime(4905):位于 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 错误/AndroidRuntime(4905):位于 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 错误/AndroidRuntime(4905):位于 dalvik.system.NativeStart.main(本机方法) 错误/AndroidRuntime(4905):引起:java.lang.NullPointerException 错误/AndroidRuntime(4905):位于 com.android.settings.bluetooth.BluetoothSettings.onCreate(BluetoothSettings.java:135) 错误/AndroidRuntime(4905):位于 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 错误/AndroidRuntime(4905):位于 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2544) 错误/AndroidRuntime(4905): ... 11 更多

经过调查,我发现在 BluetoothSettings.javaline 135 处有:

public class BluetoothSettings extends PreferenceActivity {

...
 132             mNeedAuth = intent.getBooleanExtra(BluetoothDevicePicker.EXTRA_NEED_AUTH, false);
 133             mFilterType = intent.getIntExtra(BluetoothDevicePicker.EXTRA_FILTER_TYPE,
 134                     BluetoothDevicePicker.FILTER_TYPE_ALL);
 135             mLaunchPackage = intent.getStringExtra(BluetoothDevicePicker.EXTRA_LAUNCH_PACKAGE);
 136             mLaunchClass = intent.getStringExtra(BluetoothDevicePicker.EXTRA_LAUNCH_CLASS);
...

我查看了 BluetoothDevicePicker< /code> 查找附加项的名称:

public interface BluetoothDevicePicker {
    public static final String EXTRA_NEED_AUTH =
            "android.bluetooth.devicepicker.extra.NEED_AUTH";
    public static final String EXTRA_FILTER_TYPE =
            "android.bluetooth.devicepicker.extra.FILTER_TYPE";
    public static final String EXTRA_LAUNCH_PACKAGE =
            "android.bluetooth.devicepicker.extra.LAUNCH_PACKAGE";
    public static final String EXTRA_LAUNCH_CLASS =
            "android.bluetooth.devicepicker.extra.DEVICE_PICKER_LAUNCH_CLASS";

因此我在代码中使用它们来准备那些特定的附加项。

I'm trying to bring up the standard window for Bluetooth Settings (with Device name, Discoverability etc).

However, general approach with startActivity(intent) ends with NullPointerException pointing to BluetoothSettings.java onCreate:135.

Checking with the Android code, I've found that at line 135 they get some extras from the intent. So, I prepare the same extras (names I've found in android core BluetoothDevicePicker interface) and issue it -- the same effect with NullPointerException.

Might be the wrongs names of the extras I prepare?

So is there a way I can see those extras (with names especially) from the intent the system itself submits when I open Bluetooth Settings manually acting like a user?

Many thanks.

My code is:

Intent settingsIntent = new Intent();
settingsIntent.setClassName("com.android.settings", "com.android.settings.bluetooth.BluetoothSettings");
settingsIntent.putExtra("android.bluetooth.devicepicker.extra.LAUNCH_PACKAGE", "mypackage.bttoggle");
settingsIntent.putExtra("android.bluetooth.devicepicker.extra.DEVICE_PICKER_LAUNCH_CLASS", "mypackage.bttoggle.BluetoothWidget");
PendingIntent settingsPendingIntent = PendingIntent.getActivity(context, 0, settingsIntent, 0);
views.setOnClickPendingIntent(R.id.btnSettings, settingsPendingIntent);

It's a widget so I need to use PendingIntent.

This code get the following Exception at runtime:

ERROR/AndroidRuntime(4905): Uncaught handler: thread main exiting due
to uncaught exception ERROR/AndroidRuntime(4905):
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.android.settings/com.android.settings.bluetooth.BluetoothSettings}:
java.lang.NullPointerException ERROR/AndroidRuntime(4905): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2596)
ERROR/AndroidRuntime(4905): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2621)
ERROR/AndroidRuntime(4905): at
android.app.ActivityThread.access$2200(ActivityThread.java:126)
ERROR/AndroidRuntime(4905): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1932)
ERROR/AndroidRuntime(4905): at
android.os.Handler.dispatchMessage(Handler.java:99)
ERROR/AndroidRuntime(4905): at
android.os.Looper.loop(Looper.java:123) ERROR/AndroidRuntime(4905):
at android.app.ActivityThread.main(ActivityThread.java:4595)
ERROR/AndroidRuntime(4905): at
java.lang.reflect.Method.invokeNative(Native Method)
ERROR/AndroidRuntime(4905): at
java.lang.reflect.Method.invoke(Method.java:521)
ERROR/AndroidRuntime(4905): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
ERROR/AndroidRuntime(4905): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
ERROR/AndroidRuntime(4905): at
dalvik.system.NativeStart.main(Native Method)
ERROR/AndroidRuntime(4905): Caused by: java.lang.NullPointerException
ERROR/AndroidRuntime(4905): at
com.android.settings.bluetooth.BluetoothSettings.onCreate(BluetoothSettings.java:135)
ERROR/AndroidRuntime(4905): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
ERROR/AndroidRuntime(4905): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2544)
ERROR/AndroidRuntime(4905): ... 11 more

After investigating, I found that at line 135 in BluetoothSettings.java there is:

public class BluetoothSettings extends PreferenceActivity {

...
 132             mNeedAuth = intent.getBooleanExtra(BluetoothDevicePicker.EXTRA_NEED_AUTH, false);
 133             mFilterType = intent.getIntExtra(BluetoothDevicePicker.EXTRA_FILTER_TYPE,
 134                     BluetoothDevicePicker.FILTER_TYPE_ALL);
 135             mLaunchPackage = intent.getStringExtra(BluetoothDevicePicker.EXTRA_LAUNCH_PACKAGE);
 136             mLaunchClass = intent.getStringExtra(BluetoothDevicePicker.EXTRA_LAUNCH_CLASS);
...

I looked into BluetoothDevicePicker to find the names of the extras:

public interface BluetoothDevicePicker {
    public static final String EXTRA_NEED_AUTH =
            "android.bluetooth.devicepicker.extra.NEED_AUTH";
    public static final String EXTRA_FILTER_TYPE =
            "android.bluetooth.devicepicker.extra.FILTER_TYPE";
    public static final String EXTRA_LAUNCH_PACKAGE =
            "android.bluetooth.devicepicker.extra.LAUNCH_PACKAGE";
    public static final String EXTRA_LAUNCH_CLASS =
            "android.bluetooth.devicepicker.extra.DEVICE_PICKER_LAUNCH_CLASS";

so I use them in my code to prepare those particular extras.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文