2013-11-02 94 views
1

我對我的ListView代碼有點問題,它應該顯示尚未與設備配對的可用設備的列表。Android ArrayAdapter NullPointerException getID

我已經包含下面的錯誤:

FATAL EXCEPTION: main 
at com.zephyr.bolt.UnpairedDeviceViewer$StableArrayAdapter.getItemId(UnpairedDeviceViewer.java:110) 
at android.widget.AbsListView.obtainView(AbsListView.java:2198) 

(我沒有包括整個錯誤可悲的是,該錯誤是在我的平板電腦,而我似乎無法把它複製出來的助手)

下面是引用的類文件,它的XML文件

UnpairedDeviceViewer.java

package com.zephyr.bolt; 

import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 

import android.app.Activity; 
import android.bluetooth.BluetoothAdapter; 
import android.bluetooth.BluetoothDevice; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.ListView; 
import android.widget.Toast; 

public class UnpairedDeviceViewer extends Activity{ 
    Activity a = this; 
    private ListView listview; 
    private Button b1; 
    public StableArrayAdapter adapter; 
    private ArrayList strings; 
    private BroadcastReceiver recv; 

protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.udv); 

    b1 = (Button) findViewById(R.id.b5); 

    listview = (ListView) findViewById(R.id.l2); 

    strings = new ArrayList<String>(); 

    adapter = new StableArrayAdapter(this, android.R.layout.simple_list_item_1, strings); 

    recv = new BroadcastReceiver() { 

     @Override 
     public void onReceive(Context arg0, Intent arg1) { 
      String action = arg1.getAction(); 

      if(BluetoothDevice.ACTION_FOUND.equals(action)) 
      { 
       BluetoothDevice device = arg1.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 

       adapter.add(device.getName() + "\n" + device.getAddress()); 
       adapter.notifyDataSetChanged(); 

      } 
      else if(BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) 
      { 
       Toast.makeText(a, "Device Discovery Started", Toast.LENGTH_LONG).show(); 
      } 
      else if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) 
      { 
       Toast.makeText(a, "Device Discovery Completed", Toast.LENGTH_LONG).show(); 
      } 

     } 

    }; 

    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); 
    filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED); 
    filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); 
    registerReceiver(recv, filter); 

    listview.setAdapter(adapter); 

    b1.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      MainActivity.bTAdapter.startDiscovery(); 

     } 
    }); 


} 

protected void onDestroy() 
{ 
    MainActivity.bTAdapter.cancelDiscovery(); 
    unregisterReceiver(recv); 
} 


private class StableArrayAdapter extends ArrayAdapter<String> { 

    HashMap<String, Integer> mIdMap = new HashMap<String, Integer>(); 

    public StableArrayAdapter(Context context, int textViewResourceId, 
           List<String> objects) { 
     super(context, textViewResourceId, objects); 
     for (int i = 0; i < objects.size(); ++i) { 
      mIdMap.put(objects.get(i), i); 
     } 
    } 

    @Override 
    public long getItemId(int position) { 
     String item = getItem(position); 
     return mIdMap.get(item); 
    } 

    @Override 
    public boolean hasStableIds() { 
     return true; 
    } 

} 

}

udv.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android" > 
    <Button 
     android:id = "@+id/b5" 
     android:layout_centerHorizontal="true" 
     android:layout_width = "match_parent" 
     android:layout_height = "wrap_content" 
     android:text = "Begin Discovery" 
     /> 
    <ListView 
     android:id = "@+id/l2" 
     android:layout_below="@+id/b5" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 
    </ListView> 
</RelativeLayout> 

回答

1

mIdMap是做什麼ArrayAdapter已經做的過於複雜的方式。其實施getItemId(pos)返回pos

注意你只構建mIdMap一次,你添加的構建後的任何項目 - 在您的接收器回調喜歡 - 不會有ID,並會導致您getItemId實施NPE試圖拆箱null

此外,調用notifyDataSetChanged()是不必要的,ArrayAdapter默認會在每個add()後自動調用它。

+0

非常感謝您的快速回答,我切換到了常規的ArrayAdapter,一切似乎都按照它的方式工作。猜猜我應該更瞭解我從在線教程中使用的代碼。 – brad95411

相關問題