这是我的第一个问题,因为我是Android世界节目的新手
我想把wifi rssi读取并在列表中显示它 我写下面的代码,当我在手机上运行时,程序停止并说“抱歉程序停止” 我不知道为什么???有人可以帮我吗???
public class MainActivity extends ActionBarActivity {
ListView list;
WifiManager wifiManager;
IntentFilter filter;
String wifi [];
WifiScanClass myClass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list=(ListView)findViewById(R.id.list1);
wifiManager=(WifiManager)getSystemService(Context.WIFI_SERVICE);
myClass = new WifiScanClass();
registerReceiver(myClass,new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
wifiManager.startScan();
}
protected void onResume()
{
filter=new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
registerReceiver(myClass,filter);
super.onResume();
}
protected void onPause()
{
unregisterReceiver(myClass);
super.onPause();
}
class WifiScanClass extends BroadcastReceiver {
@SuppressLint("UseValueOf")
public void onReceive(Context context, Intent intent) {
List<ScanResult> listResult=wifiManager.getScanResults();
wifi=new String[listResult.size()];
int i;
for (i=0;i<listResult.size();i++);
wifi[i]=((listResult.get(i)).toString());
list.setAdapter(new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1,wifi));
}
}
}
答案 0 :(得分:-1)
请为错误添加Logcat输出。 问题的一个可能来源可能是需要在提供的上下文中调用getApplicationContext()。因此,尝试使用context.getApplicationContext()替换getApplicationContext()。
答案 1 :(得分:-1)
public class MainActivity extends ActionBarActivity {
ListView list;
WifiManager wifiManager;
IntentFilter filter;
String wifi [];
WifiScanClass myClass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list=(ListView)findViewById(R.id.list1);
wifiManager=(WifiManager)getSystemService(Context.WIFI_SERVICE);
myClass = new WifiScanClass();
wifiManager.startScan();
}
protected void onResume()
{
filter=new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
registerReceiver(myClass,filter);
super.onResume();
}
protected void onPause()
{
unregisterReceiver(myClass);
super.onPause();
}
class WifiScanClass extends BroadcastReceiver {
@SuppressLint("UseValueOf")
public void onReceive(Context context, Intent intent) {
List<ScanResult> listResult=wifiManager.getScanResults();
wifi=new String[listResult.size()];
int i;
for (i=0;i<listResult.size();i++)
wifi[i]=((listResult.get(i)).toString());
list.setAdapter(new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1,wifi));
}
}
}
最后我发现了错误
“;”在for循环中。