当我通过命令wifiManager.getIpAddress()
获取IP地址时,应用程序将关闭。有人能告诉我原因吗?
当我使用底部的方法时,我获得了所需的IP地址,但在相同的代码中,当我尝试直接显示IP地址时,它给了我一个错误
编辑 -
package com.example.socketserverclient;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity{
final Context context = this;
WifiManager wifi;
TextView ipAddress, BSSID;
Button getIP;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
// String IpAddress = getLocalIpAddress();
ipAddress = (TextView) findViewById(R.id.textIpAddress);
BSSID = (TextView) findViewById(R.id.textBSSID);
getIP = (Button) findViewById (R.id.button);
// ipAddress.setText(IpAddress);
getIP.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
String IpAddress = getLocalIpAddress();
// ipAddress.setText(IpAddress);
}
});
}
private String getLocalIpAddress() {
WifiManager myWifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
WifiInfo myWifiInfo = myWifiManager.getConnectionInfo();
//gets the Ip address in hex form, We need to convert it to integer and then finally to string to display it
int myIp = myWifiInfo.getIpAddress();
BSSID.setText(myIp);
int intMyIp3 = myIp/0x1000000;
int intMyIp3mod = myIp%0x1000000;
int intMyIp2 = intMyIp3mod/0x10000;
int intMyIp2mod = intMyIp3mod%0x10000;
int intMyIp1 = intMyIp2mod/0x100;
int intMyIp0 = intMyIp2mod%0x100;
ipAddress.setText(String.valueOf(intMyIp0) + "." + String.valueOf(intMyIp1)
+ "." + String.valueOf(intMyIp2)
+ "." + String.valueOf(intMyIp3)
);
return null;
}
}
LOG CAT:
07-01 14:22:44.918: E/AndroidRuntime(21063): FATAL EXCEPTION: main
07-01 14:22:44.918: E/AndroidRuntime(21063): Process: com.example.socketserverclient, PID: 21063
07-01 14:22:44.918: E/AndroidRuntime(21063): android.content.res.Resources$NotFoundException: String resource ID #0xd01a8c0
07-01 14:22:44.918: E/AndroidRuntime(21063): at android.content.res.Resources.getText(Resources.java:244)
07-01 14:22:44.918: E/AndroidRuntime(21063): at android.widget.TextView.setText(TextView.java:3888)
07-01 14:22:44.918: E/AndroidRuntime(21063): at com.example.socketserverclient.MainActivity.getLocalIpAddress(MainActivity.java:56)
07-01 14:22:44.918: E/AndroidRuntime(21063): at com.example.socketserverclient.MainActivity.access$0(MainActivity.java:48)
07-01 14:22:44.918: E/AndroidRuntime(21063): at com.example.socketserverclient.MainActivity$1.onClick(MainActivity.java:42)
07-01 14:22:44.918: E/AndroidRuntime(21063): at android.view.View.performClick(View.java:4438)
07-01 14:22:44.918: E/AndroidRuntime(21063): at android.view.View$PerformClick.run(View.java:18422)
07-01 14:22:44.918: E/AndroidRuntime(21063): at android.os.Handler.handleCallback(Handler.java:733)
07-01 14:22:44.918: E/AndroidRuntime(21063): at android.os.Handler.dispatchMessage(Handler.java:95)
07-01 14:22:44.918: E/AndroidRuntime(21063): at android.os.Looper.loop(Looper.java:136)
07-01 14:22:44.918: E/AndroidRuntime(21063): at android.app.ActivityThread.main(ActivityThread.java:5017)
07-01 14:22:44.918: E/AndroidRuntime(21063): at java.lang.reflect.Method.invokeNative(Native Method)
07-01 14:22:44.918: E/AndroidRuntime(21063): at java.lang.reflect.Method.invoke(Method.java:515)
07-01 14:22:44.918: E/AndroidRuntime(21063): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-01 14:22:44.918: E/AndroidRuntime(21063): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-01 14:22:44.918: E/AndroidRuntime(21063): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
这应该有效:
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ipAddr = wifiInfo.getIpAddress();
在清单文件中具有给定权限:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
答案 1 :(得分:0)
试试这个
protected String wifiIpAddress(Context context) {
WifiManager wifiManager = (WifiManager) context.getSystemService(WIFI_SERVICE);
int ipAddress = wifiManager.getConnectionInfo().getIpAddress();
// Convert little-endian to big-endianif needed
if (ByteOrder.nativeOrder().equals(ByteOrder.LITTLE_ENDIAN)) {
ipAddress = Integer.reverseBytes(ipAddress);
}
byte[] ipByteArray = BigInteger.valueOf(ipAddress).toByteArray();
String ipAddressString;
try {
ipAddressString = InetAddress.getByAddress(ipByteArray).getHostAddress();
} catch (UnknownHostException ex) {
Log.e("WIFIIP", "Unable to get host address.");
ipAddressString = null;
}
return ipAddressString;
}
并授予此权限
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
像on this这样
从onCreate调用此方法String str = wifiIpAddress(getApplicationContext());
your_Textview.setText(str);
修改强>
更改BSSID.setText(myIp);
到
BSSID.setText(myIp + "");
答案 2 :(得分:0)
问题与IP地址功能无关。在内存地址找到资源时遇到问题:#0xd01a8c0
检查资源是否已设置。与LogCat错误一样,它应该是strings.xml中的一个字符串
编辑
将BSSID.setText(myIp);
替换为BSSID.setText(myIp + "");
答案 3 :(得分:0)
问题出在这一行
BSSID.setText(myIp);
问题是您尝试在int
的{{1}}方法中传递setText
。
简而言之,您正在调用方法TextView
而不是setText(int resid)
因此,Android会尝试查找您在setText(CharSequence text)
中传递的资源,即不存在的setText
,您将获得myIp
从
更改代码ResourcesNotFoundException
到
BSSID.setText(myIp);
答案 4 :(得分:0)
第56行: BSSID.setText(MYIP);
myIp是一个整数,对于TextView的setText方法,根据SDK文档:
final void setText(int resid)
因此,android会认为myIp是一个资源ID,它无法通过myIp的值找到此资源。纠正此问题的最简单方法是:
BSSID.setText(""+myIp);
你实际上调用了TextView的这个方法:
final void setText(CharSequence text)