java 空对象调用方法,尝试在空对象引用上调用接口方法“ ...”

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:

The error refers to this method

@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));

}

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 :-) )

The callback is created:

OnBLEDeviceSelectedListener mCallback;

and the interface

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");

}

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.

Thanks & Greetings

解决方案

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

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值