0
我試圖顯示附近所有Wi-Fi網絡的列表。我編寫的代碼給出了一個錯誤,一旦Wi-Fi在應用程序中打開,並且如果Wi-Fi已經打開,應用程序甚至不會啓動。 logcat低於附加的代碼。未在Android應用程序中顯示的Wi-Fi網絡
編輯 - 我寫了新的代碼,如下所示: CODE:
package com.example.wifiapplication;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import android.app.Activity;
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.WifiConfiguration;
import android.net.wifi.WifiConfiguration.AuthAlgorithm;
import android.net.wifi.WifiConfiguration.GroupCipher;
import android.net.wifi.WifiConfiguration.KeyMgmt;
import android.net.wifi.WifiConfiguration.PairwiseCipher;
import android.net.wifi.WifiConfiguration.Protocol;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener, OnCheckedChangeListener{
Button search;
Switch change;
ListView list;
int size=0;
List <ScanResult> results;
ArrayList <HashMap<String, String>> arraylist = new ArrayList<HashMap<String, String>>();
SimpleAdapter adapter;
WifiManager manage;
String ITEM_KEY = "key";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
search = (Button) findViewById(R.id.bSearch);
change = (Switch) findViewById(R.id.wifi);
list = (ListView) findViewById(R.id.lv);
change.setOnCheckedChangeListener(this);
search.setOnClickListener(this);
manage = (WifiManager) getSystemService(Context.WIFI_SERVICE);
change.setChecked(manage.isWifiEnabled());
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
WifiConfiguration config = new WifiConfiguration();
arraylist.clear();
if(manage.startScan()){
Toast.makeText(this, "Scanning...." + size, Toast.LENGTH_SHORT).show();
try
{
size = size - 1;
while (size >= 0)
{
HashMap<String, String> item = new HashMap<String, String>();
item.put(ITEM_KEY, results.get(size).SSID + " " + results.get(size).capabilities);
arraylist.add(item);
size--;
adapter.notifyDataSetChanged();
}
}
catch (Exception e)
{ }
}
}
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
if(arg1==true)
{
manage.setWifiEnabled(true);
//search.setText("Start Searching");
this.adapter = new SimpleAdapter(MainActivity.this, arraylist, R.layout.row, new String[] { ITEM_KEY }, new int[] { R.id.list_value });
list.setAdapter(adapter);
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
results = manage.getScanResults();
size = results.size();
}
}, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
//Search for available Wi-Fi networks;
}
else{
Toast.makeText(getApplicationContext(), "Wi-Fi is disabled", Toast.LENGTH_LONG).show();;
manage.setWifiEnabled(false);
search.setText("Turn on Wi-Fi");
}
}
// TODO Auto-generated method stub
}
的logcat:
06-23 10:04:18.200: E/AndroidRuntime(1173): FATAL EXCEPTION: main
06-23 10:04:18.200: E/AndroidRuntime(1173): Process: com.example.wifiapplication, PID: 1173
06-23 10:04:18.200: E/AndroidRuntime(1173): java.lang.NullPointerException
06-23 10:04:18.200: E/AndroidRuntime(1173): at com.example.wifiapplication.MainActivity.onCheckedChanged(MainActivity.java:94)
06-23 10:04:18.200: E/AndroidRuntime(1173): at android.widget.CompoundButton.setChecked(CompoundButton.java:127)
06-23 10:04:18.200: E/AndroidRuntime(1173): at android.widget.Switch.setChecked(Switch.java:666)
06-23 10:04:18.200: E/AndroidRuntime(1173): at android.widget.CompoundButton.toggle(CompoundButton.java:87)
06-23 10:04:18.200: E/AndroidRuntime(1173): at android.widget.CompoundButton.performClick(CompoundButton.java:99)
06-23 10:04:18.200: E/AndroidRuntime(1173): at android.view.View$PerformClick.run(View.java:18422)
06-23 10:04:18.200: E/AndroidRuntime(1173): at android.os.Handler.handleCallback(Handler.java:733)
06-23 10:04:18.200: E/AndroidRuntime(1173): at android.os.Handler.dispatchMessage(Handler.java:95)
06-23 10:04:18.200: E/AndroidRuntime(1173): at android.os.Looper.loop(Looper.java:136)
06-23 10:04:18.200: E/AndroidRuntime(1173): at android.app.ActivityThread.main(ActivityThread.java:5017)
06-23 10:04:18.200: E/AndroidRuntime(1173): at java.lang.reflect.Method.invokeNative(Native Method)
06-23 10:04:18.200: E/AndroidRuntime(1173): at java.lang.reflect.Method.invoke(Method.java:515)
06-23 10:04:18.200: E/AndroidRuntime(1173): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-23 10:04:18.200: E/AndroidRuntime(1173): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-23 10:04:18.200: E/AndroidRuntime(1173): at dalvik.system.NativeStart.main(Native Method)
是否有人可以幫助我,我在哪裏犯錯,可以做些什麼來解決這個問題呢?
能否請您詳細一點?用一些例子,或者一些參考? – BumbleBee
這裏有很多例子,這裏有一個例子http://stackoverflow.com/questions/5452940/how-can-i-get-android-wifi-scan-results-into-a-list – hoomi
掃描不是瞬間。因此,掃描完成後,您可以通過廣播意圖返回結果。 – RvdK