尝试调用界面方法' ...'在null对象引用上

时间:2016-07-14 10:34:35

标签: android callback

我正在使用具有蓝牙功能的应用程序。我使用片段扫描并列出蓝牙设备。点击后,会有一个回调主活动,提供所选的蓝牙设备。

我开始使用带有Android 6(API 23)的智能手机,然后必须调整代码以便与Android 5.0(API 21)一起使用。
我刚刚将minSDK更改为API21并重建了项目而没有任何问题。

该应用程序在智能手机上没有任何问题。使用Android 5的平板电脑运行应用程序,但在选择蓝牙设备时崩溃并出现空指针异常。

我没有找到解决这个问题的任何方法,也不知道如何继续。也许有人可以帮忙? : - )

日志是:

me: FATAL EXCEPTION: main
Process: de.tuhh.et5.tills.biocontrol, PID: 26512
java.lang.NullPointerException: Attempt to invoke interface method 'void de.tuhh.et5.tills.biocontrol.activity.BLEListFragment$OnBLEDeviceSelectedListener.OnBLEDeviceSelected(android.bluetooth.BluetoothDevice)' on a null object reference                                                                                    at de.tuhh.et5.tills.biocontrol.activity.BLEListFragment.onListItemClick(BLEListFragment.java:92)
at android.app.ListFragment$2.onItemClick(ListFragment.java:160)
at android.widget.AdapterView.performItemClick(AdapterView.java:305)
at android.widget.AbsListView.performItemClick(AbsListView.java:1185)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3222)
at android.widget.AbsListView$3.run(AbsListView.java:4138)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5568)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:955)                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:750)

由于代码很多,我会尝试总结一些重要的代码:

错误是指此方法

@Override
public void onListItemClick(ListView mBluetoothLeDeviceList, View v, int position, long id) {
    if(DEBUG)d("onListItemClick()");
    mBluetoothLeDeviceList.getChildAt(position).setBackgroundColor(Color.GREEN); // set background
    mBluetoothLeDeviceList.getChildAt(position).setFocusable(false); // not clickable again
    mCallback.OnBLEDeviceSelected(mListAdapter.getDevice(position));
}

mCallback的最后一行...生成Null Pointer Exception。蓝牙设备肯定不是空的,所以在android 5.0下出现的回调一定存在问题(听起来不对我:-))

创建了回调:

OnBLEDeviceSelectedListener mCallback;

和界面

public interface OnBLEDeviceSelectedListener {
    void OnBLEDeviceSelected(BluetoothDevice device);
}

这确保了侦听器在主要活动中实现:

try {
        mCallback = (OnBLEDeviceSelectedListener) context;
    } catch (ClassCastException e) {
        throw new ClassCastException(context.toString()
                + " must implement OnBLEDeviceeSelectedListener");
    }

主要活动实现BLEListFragment.OnBLEDeviceSelectedListener并包含

@Override
public void OnBLEDeviceSelected(BluetoothDevice device) {
.
.
.}

多数民众赞成。我发现它非常奇怪,它可以在一台设备上运行,只是在另一台设备上崩溃而没有任何编译错误。

我感谢任何想法或提示。

谢谢&问候

1 个答案:

答案 0 :(得分:3)

Make sure you implement both methods in fragment like this.

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    //Your callback initialization here
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(context);
    //Your callback initialization here
}