开发者

program closed unexpected when i try getConnectionInfo

I'm in the process of learning the android api. However, when I try to get info about wifi my program is closing unexpectedly:

here is the code:

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());

    }
}

The code above runs, but as soon as I uncomment the line: WifiInfo info = wifi.getConnectionInfo();

It says that my program has closed unexpectedly. I'm not sure what I'm doing wrong. Any help would be appreciated.

I am using the simulator.

Thanks

UPDATE: if I write out the wifi obj:

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

the app run and tv says:

android.net.wifi.
WifiManager@4051c960

UPDATE: 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


Not certain if it's supported on emmulator but on a real device I think you would need to have the following permission in your manifest:

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

If you were to look at the logcat output for errors when it closes you should see something to the effect of not having a permission...or maybe some other tidbit of info that would help others provide you some guidance. See: http://developer.android.com/guide/developing/tools/adb.html#logcat


I bet it's because you haven't set the permissions in the AndroidManifest.xml. See here. However, remember that it helps a ton if you post the actual exception message you are getting.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜