0
你好 我的目標是讓使用模擬器SDK 2.2的本地地址。 我寫了這段代碼,但是當我運行它時告訴我關閉應用程序。 這是代碼如何使用仿真器獲取IP地址的Android
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>
我希望你能幫助我 預先感謝您
(我沒有設備,我只是使用安裝在我的PC上的模擬器。這可能嗎?)
錯誤:
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'
在哪裏,什麼是你的類`exercice1.identificateur.ex.nomprenom`? – WarrenFaith 2010-12-01 15:40:15