尝试在空对象引用上调用接口方法“ ...” [英] Attempt to invoke interface method '...' on a null object reference

查看:177
本文介绍了尝试在空对象引用上调用接口方法“ ...”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发具有蓝牙功能的应用程序。我使用一个片段来扫描并列出蓝牙设备。单击时,将回调到提供所选蓝牙设备的主要活动。

我从使用Android 6(API 23)的智能手机开始,然后不得不修改代码以与Android 5.0(API 21)结合使用。

我刚刚将minSDK更改为API21,并重建了项目,没有任何问题。


该应用程序可以在智能手机上正常运行。装有Android 5的平板电脑可以运行该应用程序,但是当我选择蓝牙设备时会崩溃,并出现空指针异常。

I'm working on an App with Bluetooth functionalities. I use a fragment to scan for and list bluetooth device. On click there is a callback to the main activity providing the selected bluetooth device.

I started with a Smartphone with Android 6 (API 23) and then had to adapt the code for the use with Android 5.0 (API 21).
I just changed the minSDK to API21 and rebuilt the project without any problems.

The App works without any problems on the smartphone. The Tablet with Android 5 runs the app but crashes with a null pointer exception when I select a bluetooth device.

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

I have not found any solution to this problem and dont know how to proceed. Maybe someone can help? :-)

日志为:

The Log is:

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)

由于有很多代码,我将尝试总结重要的代码段:

Since it is a lot of code I'll try to summarize the important pieces of code:

此方法涉及错误

@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指针异常。蓝牙设备绝对不是null,因此在android 5.0下出现的回调肯定有问题(对我来说声音不正确:-))

The last line with the mCallback... generates the Null Pointer Exception. The Bluetooth device is definetly not null, so there must be a problem with the callback that just appears under android 5.0 (doesnt sound right to me :-) )

回调创建:

OnBLEDeviceSelectedListener mCallback;

和界面

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

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

and this makes sure the listener is implemented in the main activity:

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

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

The main activity implements BLEListFragment.OnBLEDeviceSelectedListener and contains

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

就是这样。我觉得这很奇怪,它可以在一台设备上运行,而在另一台设备上崩溃却没有任何编译错误。

Thats about it. I find it very weird, that it works on one device and just crashes on the other one without any compiling errors.

我感谢任何想法或提示。

I appreciate any idea or hints.

谢谢问候

推荐答案

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
}

这篇关于尝试在空对象引用上调用接口方法“ ...”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆