BluetoothChat例如:NullPointerException异常异常、BluetoothChat、NullPointerException

2023-09-05 03:23:49 作者:时间将终结所有

可能重复:    getActionBar返回null

完整的Andr​​oid小白。我试图运行从银河S3的BluetoothChat例子,它似乎崩溃的应用程序运行的时刻。

Complete Android noob. I am trying to run the BluetoothChat example from a Galaxy S3, and it appears to crash the moment the application is run.

下面是LogCat中输出:

Here is the LogCat output:

09-14 15:47:23.400: E/AndroidRuntime(6098): FATAL EXCEPTION: main
09-14 15:47:23.400: E/AndroidRuntime(6098): java.lang.NullPointerException
09-14 15:47:23.400: E/AndroidRuntime(6098):     at com.example.android.BluetoothChat.BluetoothChat.setStatus(BluetoothChat.java:235)
09-14 15:47:23.400: E/AndroidRuntime(6098):     at com.example.android.BluetoothChat.BluetoothChat.access$4(BluetoothChat.java:233)
09-14 15:47:23.400: E/AndroidRuntime(6098):     at com.example.android.BluetoothChat.BluetoothChat$2.handleMessage(BluetoothChat.java:260)
09-14 15:47:23.400: E/AndroidRuntime(6098):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-14 15:47:23.400: E/AndroidRuntime(6098):     at android.os.Looper.loop(Looper.java:137)
09-14 15:47:23.400: E/AndroidRuntime(6098):     at android.app.ActivityThread.main(ActivityThread.java:4514)
09-14 15:47:23.400: E/AndroidRuntime(6098):     at java.lang.reflect.Method.invokeNative(Native Method)
09-14 15:47:23.400: E/AndroidRuntime(6098):     at java.lang.reflect.Method.invoke(Method.java:511)
09-14 15:47:23.400: E/AndroidRuntime(6098):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
09-14 15:47:23.400: E/AndroidRuntime(6098):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
09-14 15:47:23.400: E/AndroidRuntime(6098):     at dalvik.system.NativeStart.main(Native Method)

code为每例中的SDK正是:

Code is exactly per the example in the SDK:

private final void setStatus(int resId) {
    final ActionBar actionBar = getActionBar();
    actionBar.setSubtitle(resId);
}

这似乎getActionBar返回null。什么可能我会做正确?

It appears getActionBar returns null. What might I be doing incorrectly?

这是唯一的第二Android应用程序我已经运行,这样下去容易对我说:)

This is only the 2nd Android app I have run, so go easy on me :)

干杯

推荐答案

解决方案在这里找到: getActionBar返回null

Solution found here: getActionBar returns null

这需要转到的onCreate()方法 bluetoothChat.java

getWindow()requestFeature(Window.FEATURE_ACTION_BAR);

getWindow().requestFeature(Window.FEATURE_ACTION_BAR);