gpt4 book ai didi

java - 当我尝试 getConnectionInfo 时程序意外关闭

转载 作者:搜寻专家 更新时间:2023-11-01 09:12:11 25 4
gpt4 key购买 nike

我正在学习 android api。但是,当我尝试获取有关 wifi 的信息时,我的程序意外关闭:

代码如下:

package com.example.helloandroid;    

import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.content.BroadcastReceiver;
import android.content.Context;

public class HelloAndroid extends Activity {
/** Called when the activity is first created. */

WifiManager wifi;
BroadcastReceiver receiver;

TextView textStatus;
Button buttonScan;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("Hello, Android");
setContentView(tv);

wifi = (WifiManager)getSystemService(Context.WIFI_SERVICE);
// WifiInfo info = wifi.getConnectionInfo();
// tv.append("\n\nWiFi Status: " + info.toString());

}
}

上面的代码运行了,但是一旦我取消注释该行: WifiInfo info = wifi.getConnectionInfo();

它说我的程序意外关闭。我不确定我做错了什么。任何帮助将不胜感激。

我正在使用模拟器。

谢谢

更新:如果我写出 wifi 对象:

wifi = (WifiManager)getSystemService(Context.WIFI_SERVICE);
tv.append(wifi.toString());

应用程序运行并且电视显示:

android.net.wifi.
WifiManager@4051c960

更新:登录目录:

09-22 14:56:51.387: ERROR/AndroidRuntime(408): FATAL EXCEPTION: main
09-22 14:56:51.387: ERROR/AndroidRuntime(408): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.helloandroid/com.example.helloandroid.HelloAndroid}: java.lang.SecurityException: WifiService: Neither user 10034 nor current process has android.permission.ACCESS_WIFI_STATE.
09-22 14:56:51.387: ERROR/AndroidRuntime(408): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
09-22 14:56:51.387: ERROR/AndroidRuntime(408): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
09-22 14:56:51.387: ERROR/AndroidRuntime(408): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-22 14:56:51.387: ERROR/AndroidRuntime(408): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
09-22 14:56:51.387: ERROR/AndroidRuntime(408): at android.os.Handler.dispatchMessage(Handler.java:99)
09-22 14:56:51.387: ERROR/AndroidRuntime(408): at android.os.Looper.loop(Looper.java:123)
09-22 14:56:51.387: ERROR/AndroidRuntime(408): at android.app.ActivityThread.main(ActivityThread.java:3683)
09-22 14:56:51.387: ERROR/AndroidRuntime(408): at java.lang.reflect.Method.invokeNative(Native Method)
09-22 14:56:51.387: ERROR/AndroidRuntime(408): at java.lang.reflect.Method.invoke(Method.java:507)
09-22 14:56:51.387: ERROR/AndroidRuntime(408): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-22 14:56:51.387: ERROR/AndroidRuntime(408): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-22 14:56:51.387: ERROR/AndroidRuntime(408): at dalvik.system.NativeStart.main(Native Method)
09-22 14:56:51.387: ERROR/AndroidRuntime(408): Caused by: java.lang.SecurityException: WifiService: Neither user 10034 nor current process has android.permission.ACCESS_WIFI_STATE.
09-22 14:56:51.387: ERROR/AndroidRuntime(408): at android.os.Parcel.readException(Parcel.java:1322)
09-22 14:56:51.387: ERROR/AndroidRuntime(408): at android.os.Parcel.readException(Parcel.java:1276)
09-22 14:56:51.387: ERROR/AndroidRuntime(408): at android.net.wifi.IWifiManager$Stub$Proxy.getConnectionInfo(IWifiManager.java:591)
09-22 14:56:51.387: ERROR/AndroidRuntime(408): at android.net.wifi.WifiManager.getConnectionInfo(WifiManager.java:605)
09-22 14:56:51.387: ERROR/AndroidRuntime(408): at com.example.helloandroid.HelloAndroid.onCreate(HelloAndroid.java:35)
09-22 14:56:51.387: ERROR/AndroidRuntime(408): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-22 14:56:51.387: ERROR/AndroidRuntime(408): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
09-22 14:56:51.387: ERROR/AndroidRuntime(408): ... 11 more

最佳答案

不确定模拟器是否支持它,但在真实设备上我认为您需要在 list 中获得以下权限:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>

如果您在关闭时查看 logcat 输出是否有错误,您应该会看到一些关于没有权限的信息......或者可能会帮助其他人为您提供一些指导的一些其他信息。请参阅:http://developer.android.com/guide/developing/tools/adb.html#logcat

关于java - 当我尝试 getConnectionInfo 时程序意外关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7508002/

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