简体   繁体   中英

Error receiving broadcast Intent { act=android.net.wifi.SCAN_RESULTS }

okay, the full error is

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 is my package WiFiScanReceiver is the class its crashing in.

the class is here:

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 is the main class thing.

the really strange thing about the error is, it only happens on the second time this function is called. whether thats because of the different data i dont know, i can't test.

can someone please help me?

edit1: as requested, 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>

here's the main class. where all the fun happens.

//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");
    }

}

and last but not least, here's the entire output, plus all error messages.

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

ANSWER UPDATED

If you look into your stack trace, you will find another exception that points out the real problem:

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

Note the part that says

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

The method you are attempting to access ( calculateSignalLevel ) has a known bug in it. You can find a post relating to the use calculateSignalLevel (that includes the code implementation of calculateSignalLevel ) at this question (also note Ridcully's comment on Lars' answer).

The problem is most likely your use of 100 here:

WifiManager.calculateSignalLevel(level, 100);

When this number is greater than 45, you will get a divide by zero exception. Try stepping through the method with a value of 100 for partitionLevel and observe the outcome.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM