use*_*471 0 android listview exception
我在第29行(private ListView lv = (ListView) findViewById(R.id.ScanList);)获得了NPE ,但我想这是因为以下行:
this.adapter = new SimpleAdapter(ScannerActivity.this, arraylist, R.layout.row, new String[] { ITEM_KEY }, new int[] { R.id.list_value });
Run Code Online (Sandbox Code Playgroud)
什么属于"R.id.list_value"和"R.layout.row"它是一个row.xml?我不知道我在那里写的东西!
要知道代码是如何工作的,我粘贴一个relvant部分:
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);
}
});
Run Code Online (Sandbox Code Playgroud)
这是我的第一个ListView,所以请帮帮我:)
这里 :
private ListView lv = (ListView) findViewById(R.id.ScanList);
private Button buttonScan = (Button) findViewById(R.id.scannow_button);
Run Code Online (Sandbox Code Playgroud)
您正在尝试初始化ListView并Button在为当前活动设置布局之前,因此您将获得NPE异常.调用后,将所有初始化在Activity的onCreate方法中移动setContentView :
//...your code here...
private ListView lv;
private Button buttonScan;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scan);
lv = (ListView) findViewById(R.id.ScanList);
buttonScan = (Button) findViewById(R.id.scannow_button);
//...your code here...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
141 次 |
| 最近记录: |