程序在我尝试getConnectionInfo时意外关闭

时间:2011-09-21 23:47:32

标签: java android

我正在学习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

更新: logcat的:

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

2 个答案:

答案 0 :(得分:3)

不确定它是否在emmulator上受支持,但在真实设备上我认为您需要在清单中获得以下权限:

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

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

答案 1 :(得分:0)

我敢打赌,因为你没有在AndroidManifest.xml中设置权限。见here。但是,请记住,如果您发布了实际的异常消息,它会有所帮助。