如何使用android模拟器获取ip地址
你好 我的目标是使用模拟器 sdk 2.2 获取本地地址。 我编写了这段代码,但是当我运行它时告诉我关闭应用程序。 这是代码
package exercice1.identificateur.ex;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import exercice1.identificateur.R;
public class wifi1 extends ListActivity {
private static final String LOG_TAG = null;
public String getLocalIpAddress() {
try {
for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
return inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException ex) {
Log.e(LOG_TAG, ex.toString());
}
return null;
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = new TextView(this);
tv.setText(getLocalIpAddress());
setContentView(tv);
}
}
这是manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="exercice1.identificateur"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".ex.wifi1"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="8" />
<uses-configuration></uses-configuration>
<uses-feature></uses-feature>
<uses-permission android:name="android.permission.INTERNET"></uses-permission><uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission><uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission><uses-sdk></uses-sdk>
</manifest>
我希望你能帮助我 提前谢谢您
(我没有设备,我只使用安装在我的电脑上的模拟器。这可能吗?)
[编辑] 错误:
12-01 17:13:23.154: ERROR/ AndroidRuntime(511): FATAL EXCEPTION: main
12-01 17:13:23.154: ERROR/ AndroidRuntime(511): java.lang.RuntimeException: Unable to start activity ComponentInfo{exercice1.identificateur/ exercice1.identificateur.ex.wifi1}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
Hello
My aim is to get the local address using the emulator sdk 2.2.
I wrote this code but when I run it tell me to close the application.
this is the code
package exercice1.identificateur.ex;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import exercice1.identificateur.R;
public class wifi1 extends ListActivity {
private static final String LOG_TAG = null;
public String getLocalIpAddress() {
try {
for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
return inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException ex) {
Log.e(LOG_TAG, ex.toString());
}
return null;
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = new TextView(this);
tv.setText(getLocalIpAddress());
setContentView(tv);
}
}
this is the manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="exercice1.identificateur"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".ex.wifi1"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="8" />
<uses-configuration></uses-configuration>
<uses-feature></uses-feature>
<uses-permission android:name="android.permission.INTERNET"></uses-permission><uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission><uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission><uses-sdk></uses-sdk>
</manifest>
I hope that you can help me
Thank you in advance
(I don't have a device, I use just the emulator installing in my PC. It is possible?)
[edit]
The error:
12-01 17:13:23.154: ERROR/ AndroidRuntime(511): FATAL EXCEPTION: main
12-01 17:13:23.154: ERROR/ AndroidRuntime(511): java.lang.RuntimeException: Unable to start activity ComponentInfo{exercice1.identificateur/ exercice1.identificateur.ex.wifi1}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
沃伦菲斯的问题问得很好。我看起来 DalvikVM 正在寻找预编译或生成的类(ex.nomprenom)。你的项目结构是什么?这些都是项目中的文件吗?
我认为它是您的包的 R 文件之一中的链接。
Very good question WarrenFaith. I looks like the DalvikVM is looking for a precompiled or generated class (ex.nomprenom). What's your project structure? Are these all files in the project?
I think it is a link in one of the R files of your package.