Android Wifi 应用程序崩溃

发布于 2024-09-28 21:42:28 字数 2848 浏览 4 评论 0原文

我写了一个 Android 应用程序来扫描 wifi 连接。

我使用了以下代码:

public class WifiTester extends Activity {

  TextView mainText;
  WifiManager mainWifi;
  WifiReceiver receiverWifi;
  List<ScanResult> wifiList;
  StringBuilder sb = new StringBuilder();

  public void onCreate(Bundle savedInstanceState) {

     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);

     mainText = (TextView) findViewById(R.id.mainText);
     mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);

     receiverWifi = new WifiReceiver();
     registerReceiver(receiverWifi, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
     mainWifi.startScan();
     mainText.setText("\nStarting Scan...\n");
  }

  public boolean onCreateOptionsMenu(Menu menu) {
     menu.add(0, 0, 0, "Refresh");
     return super.onCreateOptionsMenu(menu);
  }

  public boolean onMenuItemSelected(int featureId, MenuItem item) {
     mainWifi.startScan();
     mainText.setText("Starting Scan");
     return super.onMenuItemSelected(featureId, item);
  }

  protected void onPause() {
     unregisterReceiver(receiverWifi);
     super.onPause();
  }

  protected void onResume() {
     registerReceiver(receiverWifi, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
     super.onResume();
  }

  class WifiReceiver extends BroadcastReceiver {
     public void onReceive(Context c, Intent intent) {
        sb = new StringBuilder();
        wifiList = mainWifi.getScanResults();
        for(int i = 0; i < wifiList.size(); i++){
            sb.append(new Integer(i+1).toString() + ".");
            sb.append((wifiList.get(i)).toString());
            sb.append("\n");
        }
        mainText.setText(sb);
     }
  }

}

当我在模拟器上运行应用程序时,它崩溃了

清单文件是:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.android.wifitester"
   android:versionCode="1"
   android:versionName="1.0">
   <application android:icon="@drawable/icon" 
       android:label="@string/app_name">
       <activity android:name=".WifiTester"
              android:label="@string/app_name">
              <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
                 <category android:name="android.intent.category.LAUNCHER" />
              </intent-filter>
       </activity> 
   </application>
   <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
   <uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
   <uses-sdk android:minSdkVersion="8" />

</manifest> 

我的问题是为什么它崩溃,尽管我添加了权限

I wrote an android application that scans for wifi connection.

I used the following code:

public class WifiTester extends Activity {

  TextView mainText;
  WifiManager mainWifi;
  WifiReceiver receiverWifi;
  List<ScanResult> wifiList;
  StringBuilder sb = new StringBuilder();

  public void onCreate(Bundle savedInstanceState) {

     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);

     mainText = (TextView) findViewById(R.id.mainText);
     mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);

     receiverWifi = new WifiReceiver();
     registerReceiver(receiverWifi, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
     mainWifi.startScan();
     mainText.setText("\nStarting Scan...\n");
  }

  public boolean onCreateOptionsMenu(Menu menu) {
     menu.add(0, 0, 0, "Refresh");
     return super.onCreateOptionsMenu(menu);
  }

  public boolean onMenuItemSelected(int featureId, MenuItem item) {
     mainWifi.startScan();
     mainText.setText("Starting Scan");
     return super.onMenuItemSelected(featureId, item);
  }

  protected void onPause() {
     unregisterReceiver(receiverWifi);
     super.onPause();
  }

  protected void onResume() {
     registerReceiver(receiverWifi, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
     super.onResume();
  }

  class WifiReceiver extends BroadcastReceiver {
     public void onReceive(Context c, Intent intent) {
        sb = new StringBuilder();
        wifiList = mainWifi.getScanResults();
        for(int i = 0; i < wifiList.size(); i++){
            sb.append(new Integer(i+1).toString() + ".");
            sb.append((wifiList.get(i)).toString());
            sb.append("\n");
        }
        mainText.setText(sb);
     }
  }

}

when I run the application on the emulator, it crashes

The manifest file is:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.android.wifitester"
   android:versionCode="1"
   android:versionName="1.0">
   <application android:icon="@drawable/icon" 
       android:label="@string/app_name">
       <activity android:name=".WifiTester"
              android:label="@string/app_name">
              <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
                 <category android:name="android.intent.category.LAUNCHER" />
              </intent-filter>
       </activity> 
   </application>
   <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
   <uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
   <uses-sdk android:minSdkVersion="8" />

</manifest> 

My question is why it crashes although I added the permissions

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

昨迟人 2024-10-05 21:42:28

简短的回答是:模拟器不支持 wifi。

在模拟器和其他没有 wifi 的设备上,该行将

wifiList = mainWifi.getScanResults();

导致 wifiList 为空。您应该检查这一点,例如用 try/catch 块包围它,如下所示:

try{ 
    wifiList = mainWifi.getScanResults();
    for(int i = 0; i < wifiList.size(); i++){
        sb.append(new Integer(i+1).toString() + ".");
        sb.append((wifiList.get(i)).toString());
        sb.append("\n");
    }
catch (NullPointerException e) {
    //Add dummy testdata to sb
}

The short answer is: The emulator doesn't do wifi.

On the emulator, and other devices without wifi, the line

wifiList = mainWifi.getScanResults();

will result in wifiList being null. You should check on this, for instance by surrounding this with a try/catch block like this:

try{ 
    wifiList = mainWifi.getScanResults();
    for(int i = 0; i < wifiList.size(); i++){
        sb.append(new Integer(i+1).toString() + ".");
        sb.append((wifiList.get(i)).toString());
        sb.append("\n");
    }
catch (NullPointerException e) {
    //Add dummy testdata to sb
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文