【问题标题】:Android: Trying to start BluetoothSettings activity Force Close with NullPointerExceptionAndroid:尝试使用 NullPointerException 启动 BluetoothSettings 活动强制关闭
【发布时间】:2011-05-12 14:05:03
【问题描述】:

我正在尝试调出蓝牙设置的标准窗口(带有设备名称、可发现性等)。

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

检查 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):未捕获的处理程序:线程主退出到期 未捕获的异常 ERROR/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(Native Method) 错误/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(Native 方法)错误/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.java的第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 以找到附加组件的名称:

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";

所以,我在我的代码中使用它们来准备那些特殊的附加功能。

【问题讨论】:

    标签: android bluetooth settings


    【解决方案1】:

    这段代码对我有用

        Intent intentBluetooth = new Intent();
        intentBluetooth.setAction(android.provider.Settings.ACTION_BLUETOOTH_SETTINGS);
        startActivity(intentBluetooth);     
    

    你需要在手机上测试它,否则你会在模拟器上得到奇怪的错误(不支持蓝牙)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多