接收广播Intent时出错{act = android.net.wifi.SCAN_RESULTS}

时间:2012-06-18 15:46:25

标签: java android android-intent broadcastreceiver

好的,完整的错误是

06-19 01:07:57.421: E/AndroidRuntime(4478): java.lang.RuntimeException: Error 
receiving broadcast Intent { act=android.net.wifi.SCAN_RESULTS } in 
com.blucalc.netfind.WiFiScanReceiver@40521ba0

com.blucalc.netfind是我的包裹 WiFiScanReceiver是它崩溃的类。

班级在这里:

package com.blucalc.netfind;

import java.util.List;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.ScanResult;

public class WiFiScanReceiver extends BroadcastReceiver {
    NetworkfinderActivity netfinder;

    public WiFiScanReceiver(NetworkfinderActivity netfinder) {
        super();
        this.netfinder = netfinder;
    }

    @Override
    public void onReceive(Context c, Intent intent) {
        System.out.println("onReceive(Context=" + c.toString() + "Intent="
                + intent.toString());
        List<ScanResult> results = netfinder.wifi.getScanResults();
        netfinder.processResults(results);
    }

}

NetworkfinderActivity是主要的类。

关于错误的真正奇怪之处在于,它只发生在第二次调用此函数时。是不是因为我不知道的数据不同,我无法测试。

有人可以帮帮我吗?

EDIT1: 按要求,androidmanifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.blucalc.netfind"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="7" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".NetworkfinderActivity"
            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>

</manifest>

这是主要课程。所有的乐趣发生在哪里。

//some code borrowed from http://marakana.com/forums/android/examples/40.html
package com.blucalc.netfind;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.IntentFilter;
import android.os.Bundle;
import android.net.wifi.*;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;


public class NetworkfinderActivity extends Activity {
    /** Called when the activity is first created. */
    WifiManager wifi;
    BroadcastReceiver receiver;
    List<FrameLayout> netlist;
    LinearLayout ll;
    Button b;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        System.out.println("start of main constructor");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //a couple interface elements I want to access
        ll = (LinearLayout)findViewById(R.id.networkList);
        b = (Button)findViewById(R.id.refreshButton);
        //initialisation of some memory
        netlist=new ArrayList<FrameLayout>();
        //where all network info comes from
        wifi=(WifiManager)getSystemService(WIFI_SERVICE);
        // Register Broadcast Receiver
        receiver = new WiFiScanReceiver(this);
        registerReceiver(receiver, new IntentFilter(
                WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));

        b.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {;
            System.out.println("clicked");
                ll.removeAllViews();
                netlist.clear();

                wifi.startScan();
            }
        });
        System.out.println("end of main constructor");
    }

    @Override
    public void onStop() {
        unregisterReceiver(receiver);
    }
    public void processResults(List<ScanResult> results)
    {
        System.out.println("processResults(results "+results.toString());
        ll.removeAllViews();
        netlist.clear();
        for (ScanResult result:results)
        {
            readResult(result);
        }
        System.out.println("end of processResults");

    }
    public void readResult(ScanResult result)
    {
        System.out.println("readResult(result "+result.toString());
        FrameLayout frame=new FrameLayout(this);
        TextView ssid=new TextView(this);
        TextView strength=new TextView(this);
        System.out.println("1");
        int level=result.level;
        System.out.println("level="+level);
        int signal=WifiManager.calculateSignalLevel(level, 100)+1;
        //signal will be strength of the signal as a percent (from 1 to 100)
        System.out.println("signal="+signal);

        strength.setText(new Integer(signal).toString());
        ssid.setText(result.SSID);
        TextView cheat=new TextView(this);
        cheat.setText(result.toString());

//      frame.addView(ssid);
//      frame.addView(strength);
        frame.addView(cheat);

        netlist.add(frame);
        ll.addView(netlist.get(netlist.size()-1));
        System.out.println("end of readResults");
    }

}

最后但并非最不重要的是,这是整个输出,加上所有错误消息。

06-20 20:40:06.835: I/ApplicationPackageManager(2399): cscCountry is not German : XSA
06-20 20:40:06.835: I/System.out(2399): start of main constructor
06-20 20:40:06.867: V/WifiProgressStore(2399): WifiProgressStore Created
06-20 20:40:06.867: I/System.out(2399): end of main constructor
06-20 20:40:16.765: I/System.out(2399): clicked
06-20 20:40:17.351: I/System.out(2399): onReceive(Context=com.blucalc.netfind.NetworkfinderActivity@40518050Intent=Intent { act=android.net.wifi.SCAN_RESULTS }
06-20 20:40:17.363: I/System.out(2399): processResults(results [SSID: BluCalculator, BSSID: f4:ec:38:a9:1d:56, capabilities: [WPA-PSK-TKIP+CCMP][WPA2-PSK-TKIP+CCMP][WPS], level: -51, frequency: 2412, SSID: BigAir, BSSID: 06:27:22:b3:41:7e, capabilities: , level: -72, frequency: 2437, SSID: BigAir, BSSID: 06:27:22:5f:56:d8, capabilities: , level: -87, frequency: 2437]
06-20 20:40:17.367: I/System.out(2399): readResult(result SSID: BluCalculator, BSSID: f4:ec:38:a9:1d:56, capabilities: [WPA-PSK-TKIP+CCMP][WPA2-PSK-TKIP+CCMP][WPS], level: -51, frequency: 2412
06-20 20:40:17.375: I/System.out(2399): 1
06-20 20:40:17.375: I/System.out(2399): level=-51
06-20 20:40:17.375: I/System.out(2399): signal=100
06-20 20:40:17.378: I/System.out(2399): end of readResults
06-20 20:40:17.382: I/System.out(2399): readResult(result SSID: BigAir, BSSID: 06:27:22:b3:41:7e, capabilities: , level: -72, frequency: 2437
06-20 20:40:17.390: I/System.out(2399): 1
06-20 20:40:17.390: I/System.out(2399): level=-72
06-20 20:40:17.394: D/AndroidRuntime(2399): Shutting down VM
06-20 20:40:17.394: W/dalvikvm(2399): threadid=1: thread exiting with uncaught exception (group=0x40015578)
06-20 20:40:17.402: E/AndroidRuntime(2399): FATAL EXCEPTION: main
06-20 20:40:17.402: E/AndroidRuntime(2399): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.net.wifi.SCAN_RESULTS } in com.blucalc.netfind.WiFiScanReceiver@40520ec0
06-20 20:40:17.402: E/AndroidRuntime(2399):     at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:722)
06-20 20:40:17.402: E/AndroidRuntime(2399):     at android.os.Handler.handleCallback(Handler.java:587)
06-20 20:40:17.402: E/AndroidRuntime(2399):     at android.os.Handler.dispatchMessage(Handler.java:92)
06-20 20:40:17.402: E/AndroidRuntime(2399):     at android.os.Looper.loop(Looper.java:123)
06-20 20:40:17.402: E/AndroidRuntime(2399):     at android.app.ActivityThread.main(ActivityThread.java:3687)
06-20 20:40:17.402: E/AndroidRuntime(2399):     at java.lang.reflect.Method.invokeNative(Native Method)
06-20 20:40:17.402: E/AndroidRuntime(2399):     at java.lang.reflect.Method.invoke(Method.java:507)
06-20 20:40:17.402: E/AndroidRuntime(2399):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
06-20 20:40:17.402: E/AndroidRuntime(2399):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
06-20 20:40:17.402: E/AndroidRuntime(2399):     at dalvik.system.NativeStart.main(Native Method)
06-20 20:40:17.402: E/AndroidRuntime(2399): Caused by: java.lang.ArithmeticException: divide by zero
06-20 20:40:17.402: E/AndroidRuntime(2399):     at android.net.wifi.WifiManager.calculateSignalLevel(WifiManager.java:957)
06-20 20:40:17.402: E/AndroidRuntime(2399):     at com.blucalc.netfind.NetworkfinderActivity.readResult(NetworkfinderActivity.java:79)
06-20 20:40:17.402: E/AndroidRuntime(2399):     at com.blucalc.netfind.NetworkfinderActivity.processResults(NetworkfinderActivity.java:65)
06-20 20:40:17.402: E/AndroidRuntime(2399):     at com.blucalc.netfind.WiFiScanReceiver.onReceive(WiFiScanReceiver.java:23)
06-20 20:40:17.402: E/AndroidRuntime(2399):     at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:709)
06-20 20:40:17.402: E/AndroidRuntime(2399):     ... 9 more

1 个答案:

答案 0 :(得分:7)

更新答案

如果查看堆栈跟踪,您会发现另一个指出真正问题的异常:

06-20 20:40:17.402: E/AndroidRuntime(2399): Caused by: java.lang.ArithmeticException: divide by zero
06-20 20:40:17.402: E/AndroidRuntime(2399):     at android.net.wifi.WifiManager.calculateSignalLevel(WifiManager.java:957)
06-20 20:40:17.402: E/AndroidRuntime(2399):     at com.blucalc.netfind.NetworkfinderActivity.readResult(NetworkfinderActivity.java:79)
06-20 20:40:17.402: E/AndroidRuntime(2399):     at com.blucalc.netfind.NetworkfinderActivity.processResults(NetworkfinderActivity.java:65)
06-20 20:40:17.402: E/AndroidRuntime(2399):     at com.blucalc.netfind.WiFiScanReceiver.onReceive(WiFiScanReceiver.java:23)
06-20 20:40:17.402: E/AndroidRuntime(2399):     at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:709)
06-20 20:40:17.402: E/AndroidRuntime(2399):     ... 9 more

请注意

部分
    6-20 20:40:17.402: E/AndroidRuntime(2399): Caused by: java.lang.ArithmeticException: divide by zero

您尝试访问的方法(calculateSignalLevel)中包含已知的bug。您可以在this问题中找到与使用calculateSignalLevel相关的帖子(包括calculateSignalLevel的代码实现)(另请注意Ridcully对Lars答案的评论)。

问题很可能是你在100使用了这个问题:

WifiManager.calculateSignalLevel(level, 100);

当此数字大于45时,您将获得除以零的异常。尝试使用值{100}的partitionLevel单步执行该方法并观察结果。