-1

我在第 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,所以请帮助我 :)

4

1 回答 1

5

这里 :

private ListView lv = (ListView) findViewById(R.id.ScanList);
private Button buttonScan = (Button) findViewById(R.id.scannow_button);

您正在尝试初始化ListViewButton在为当前活动设置布局之前,因此您会遇到 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...
于 2013-06-25T19:32:54.870 回答