开发者

Android: Trying to start BluetoothSettings activity Force Close with NullPointerException

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 toBluetoothSettings.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 l开发者_运维百科ooked 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.


This code works for me

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

You need to test it on the phone, otherways you will get strange errors on the emulator (Bluetooth not supported).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜