我正在关注此线程/帖子中的代码:Enable bluetooth tethering android programmatically(以及随后的帖子:How to check Bluetooth tethering status programmatically in Android),我遇到了障碍。我正在使用 anirudh reddy 的代码。当我调用该行时
setTetheringOn.invoke(instance,true);
我得到一个 NullPointerException。这是 LogCat 堆栈跟踪:
12-21 13:11:16.655: W/System.err(12738): java.lang.reflect.InvocationTargetException
12-21 13:11:16.655: W/System.err(12738): at java.lang.reflect.Method.invoke(Native Method)
12-21 13:11:16.655: W/System.err(12738): at java.lang.reflect.Method.invoke(Method.java:372)
12-21 13:11:16.655: W/System.err(12738): at com.myname.android.bluetoothtetheringwidget.BluetoothTetheringActivity.turnBluetoothTetheringOn(BluetoothTetheringActivity.java:50)
12-21 13:11:16.655: W/System.err(12738): at com.myname.android.bluetoothtetheringwidget.BluetoothTetheringActivity.onCreate(BluetoothTetheringActivity.java:27)
12-21 13:11:16.655: W/System.err(12738): at android.app.Activity.performCreate(Activity.java:5933)
12-21 13:11:16.655: W/System.err(12738): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
12-21 13:11:16.655: W/System.err(12738): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
12-21 13:11:16.655: W/System.err(12738): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
12-21 13:11:16.656: W/System.err(12738): at android.app.ActivityThread.access$800(ActivityThread.java:144)
12-21 13:11:16.656: W/System.err(12738): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
12-21 13:11:16.656: W/System.err(12738): at android.os.Handler.dispatchMessage(Handler.java:102)
12-21 13:11:16.656: W/System.err(12738): at android.os.Looper.loop(Looper.java:135)
12-21 13:11:16.656: W/System.err(12738): at android.app.ActivityThread.main(ActivityThread.java:5221)
12-21 13:11:16.656: W/System.err(12738): at java.lang.reflect.Method.invoke(Native Method)
12-21 13:11:16.656: W/System.err(12738): at java.lang.reflect.Method.invoke(Method.java:372)
12-21 13:11:16.656: W/System.err(12738): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
12-21 13:11:16.656: W/System.err(12738): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
12-21 13:11:16.656: W/System.err(12738): Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'void android.bluetooth.IBluetoothPan.setBluetoothTethering(boolean)' on a null object reference
12-21 13:11:16.656: W/System.err(12738): at android.bluetooth.BluetoothPan.setBluetoothTethering(BluetoothPan.java:337)
12-21 13:11:16.656: W/System.err(12738): ... 17 more
在我的BluetoothTetheringActivity
,我的onCreate
方法调用turnBluetoothTetheringOn
方法。
继续前进,我进入了调试模式(我使用的是 Eclipse Luna),当我进入android.bluetooth.BluetoothPan.setBluetoothTethering
方法时发现了一些有趣的东西:类变量mPanService
是 null。这很奇怪,因为代码明确使用了对mPanService
. 这是中的代码android.bluetooth.BluetoothPan.setBluetoothTethering
:
public void setBluetoothTethering(boolean value) {
if (DBG) log("setBluetoothTethering(" + value + ")");
try {
mPanService.setBluetoothTethering(value);
} catch (RemoteException e) {
Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
}
}
我不确定这段代码是最新的,但是一个名为 grepcode.com 的网站有一个页面BluetoothPan
的代码:
android.bluetooth.BluetoothPan。在这段代码中,mPanService
被称为mService
.
在尝试调用之前,如何确保BluetoothPan
's 类变量引用的是实际对象?mPanService
setBluetoothTethering
我可能没有问正确的问题,所以任何关于改写我的问题的建设性批评将不胜感激。