When I call the method below, my application crashes. In my AndroidManifest.xml
, the pplication has permission to access wifi state.
public void show(View view) throws UnknownHostException {
WifiManager wm =
(WifiManager)getSystemService(Context.WIFI_SERVICE);
if (!wm.isWifiEnabled()) {
wm.setWifiEnabled(true);
}
int ip = wm.getConnectionInfo().getIpAddress();
String s = Integer.toString(ip);
EditText et = (EditText) findViewById(R.id.my_name);
et.setText("" + ip);
}
Here is the error:
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:5207)
at android.view.View$PerformClick.run(View.java:21177)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5441)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
... 9 more
Caused by: java.lang.SecurityException: WifiService: Neither user 10170 nor current process has android.permission.ACCESS_WIFI_STATE.
at android.os.Parcel.readException(Parcel.java:1620)
at android.os.Parcel.readException(Parcel.java:1573)
at android.net.wifi.IWifiManager$Stub$Proxy.getConnectionInfo(IWifiManager.java:1125)
at android.net.wifi.WifiManager.getConnectionInfo(WifiManager.java:1315)
at com.example.shankey.chatoz.MainActivity.show(MainActivity.java:28)
... 11 more
Please suggest a method to resolve this error. Thanks in advance.
Are you testing on above version of lollipop ?? You have to ask for permission explicitly in code in the show() method.
or check this link may be it will helpfull for you ..
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.