0
我是想編寫一個無線掃描器,它確實20次掃描,但它顯示了在錯誤顯示爲空指針異常
if(bssid[j].equals(null)){
NullPointerException異常我的代碼稍微巨大
package com.example.scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
WifiManager wifi;
WifiScanReceiver wifireciever;
WifiInfo info;
Button scan, save;
List<ScanResult> wifilist;
ListView list;
String wifis[];
String name;
String[] ssid = new String[100];
String[] bssid = new String[100];
int[] lvl = new int[100];
int[] count = new int[100];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
list=(ListView)findViewById(R.id.listView1);
scan=(Button)findViewById(R.id.button1);
save=(Button)findViewById(R.id.button2);
scan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
wifi=(WifiManager)getSystemService(Context.WIFI_SERVICE);
if (wifi.isWifiEnabled()==false){
wifi.setWifiEnabled(true);
}
wifireciever = new WifiScanReceiver();
for (int i=0;i<20;i++){
registerReceiver(wifireciever, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
wifi.startScan();
if (i==19){
Toast.makeText(getBaseContext(), "Scan Finish", Toast.LENGTH_LONG).show();
}
}
}
});
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
savedata();
}
});
}
protected void savedata() {
// TODO Auto-generated method stub
try {
File sdcard = Environment.getExternalStorageDirectory();
File directory = new File(sdcard.getAbsolutePath() + "/WIFI_RESULT");
directory.mkdirs();
name = new SimpleDateFormat("yyyy-MM-dd HH mm ss").format(new Date());
File file = new File(directory,name + "wifi_data.txt");
FileOutputStream fou = new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(fou);
try {
for (int i =0; i < list.getCount(); i++){
osw.append(list.getItemAtPosition(i).toString());
}
osw.flush();
osw.close();
Toast.makeText(getBaseContext(), "Saved", Toast.LENGTH_LONG).show();
} catch (IOException e){
e.printStackTrace();
}
} catch (FileNotFoundException e){
e.printStackTrace();
}
}
class WifiScanReceiver extends BroadcastReceiver {
@SuppressLint("UseValueOf")
public void onReceive(Context c, Intent intent) {
int a =0;
wifi.startScan();
List<ScanResult> wifilist = wifi.getScanResults();
if (a<wifilist.size()){
a=wifilist.size();
}
for(int j=0;j<wifilist.size();j++){
if(bssid[j].equals(null)){
ssid[j] = wifilist.get(j).SSID.toString();
bssid[j] = wifilist.get(j).BSSID.toString();
lvl[j] = wifilist.get(j).level;
count[j]++;
}
else if (bssid[j].equals(wifilist.get(j).BSSID.toString())){
lvl[j] = lvl[j] + wifilist.get(j).level;
count[j]++;
}
}
wifis = new String[a];
for (int i =0; i<a; i++){
wifis[i] = ("\n" + ssid[i] + "\n AP Address" + bssid[i] + "\n Signal Strength:" + lvl[i]/count[i]).toString();
}
list.setAdapter(new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_1,wifis));
}
}
protected void onDestroy() {
unregisterReceiver(wifireciever);
super.onPause();
}
protected void onResume() {
registerReceiver(wifireciever, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
super.onResume();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
的NullPointerException在這一點的意思是我的數組bssid沒有初始化。所以我只想知道如何在主要活動中初始化它,以便我可以在任何地方使用該字符串bssid。