gpt4 book ai didi

java - Android ArrayAdapter NullPointerException getID

转载 作者:行者123 更新时间:2023-11-30 03:17:46 27 4
gpt4 key购买 nike

我的 ListView 代码有点问题,它应该显示尚未与设备配对的可用设备列表。

我已经包含了以下错误:

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

(遗憾的是我没有包含整个错误,错误在我的平板电脑上,我似乎无法从 AIDE 中复制它)

下面是引用的class文件和它的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>

最佳答案

你的 mIdMap是一种过于复杂的方式来做 ArrayAdapter已经这样做了。其实现getItemId(pos)返回 pos .

请注意,您只是在构建您的 mIdMap曾经,您在构建后添加的任何项目 - 就像在您的接收器回调中一样 - 将没有 ID,并且会在您的 getItemId 中导致 NPE尝试拆箱的实现 null .

另外,调用 notifyDataSetChanged()是不必要的,ArrayAdapter将在每个 add() 之后自动调用它默认情况下。

关于java - Android ArrayAdapter NullPointerException getID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19738322/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com