2016-07-14 60 views
2

我正在開發具有藍牙功能的應用程序。我使用片段掃描並列出藍牙設備。點擊後會回覆提供所選藍牙設備的主要活動。

我開始使用帶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的最後一行...生成空指針異常。藍牙設備definetly不爲空,所以必須有與剛下的是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) { 
. 
. 
.} 

那。我覺得它很奇怪,它可以在一臺設備上運行,並且在另一臺設備上崩潰而沒有任何編譯錯誤。

我欣賞任何想法或提示。

感謝&問候

+0

調試,並找出哪個值是空 – Jas

+0

我檢查,如果mListAdapter或mListAdapter.getDevice(位置)爲null這是情況並非如此。我想它與回調的東西。奇怪的是,相同的代碼適用於API23,但不適用於API21。沒有編譯錯誤,只是這個運行時崩潰 – Chuchaki

回答

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 
} 
+0

完美!我意識到活動/情境的差異,但不知道超載是可能的。謝謝,很好的回答:-) – Chuchaki

+0

現在已經棄用了。 –

相關問題