我在第 29 行 ( ) 得到 NPE private ListView lv = (ListView) findViewById(R.id.ScanList);
,但我想这是因为以下行:
this.adapter = new SimpleAdapter(ScannerActivity.this, arraylist, R.layout.row, new String[] { ITEM_KEY }, new int[] { R.id.list_value });
属于“R.id.list_value”和“R.layout.row”的是row.xml吗?我没有得到我必须在那里写的东西!
要知道代码是如何工作的,我粘贴了相关部分:
public class ScannerActivity extends Activity implements OnClickListener {
private WifiManager wifi;
private ListView lv = (ListView) findViewById(R.id.ScanList);
private Button buttonScan = (Button) findViewById(R.id.scannow_button);
private Switch enable;
private int size = 0;
private List<ScanResult> results;
private final String ITEM_KEY = "key";
private final ArrayList<HashMap<String, String>> arraylist = new ArrayList<HashMap<String, String>>();
private SimpleAdapter adapter;
/* Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scan);
buttonScan.setOnClickListener(this);
wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
enable.setChecked(wifi.isWifiEnabled());
enable.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundbutton, boolean flag) {
wifi.setWifiEnabled(flag);
if (flag) {
arraylist.clear();
lv.setVisibility(View.VISIBLE);
buttonScan.setEnabled(true);
wifi.startScan();
} else {
lv.setVisibility(View.INVISIBLE);
buttonScan.setEnabled(false);
}
}
});
this.adapter = new SimpleAdapter(ScannerActivity.this, arraylist, R.layout.activity_scan, new String[]{ITEM_KEY}, new int[]{R.id.ScanList});
lv.setAdapter(this.adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long id) {
lv.getSelectedItemPosition();
Intent myIntent = new Intent(ScannerActivity.this, MyActivity.class);
String mac = results.get(size).BSSID;
myIntent.putExtra("mac", mac);
ScannerActivity.this.startActivity(myIntent);
}
});
这是我的第一个 ListView,所以请帮助我 :)