gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 06:41:42 24 4
gpt4 key购买 nike

好的,完整的错误是

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 是它崩溃的类。

类(class)在这里:

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 是主要的类。

这个错误真正奇怪的是,它只在第二次调用这个函数时发生。是不是因为数据不同我不知道,没法测试。

有人可以帮帮我吗?

编辑1:根据要求,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

最佳答案

答案已更新

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

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 时,您将获得被零除异常。尝试逐步执行 partitionLevel 值为 100 的方法并观察结果。

关于java - 接收广播 Intent 时出错 { act=android.net.wifi.SCAN_RESULTS },我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11086362/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com