【问题标题】:How to get the ip address with an emulator android如何使用模拟器android获取IP地址
【发布时间】:2011-05-18 13:57:54
【问题描述】:

你好 我的目标是使用模拟器 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'

【问题讨论】:

  • 你的班级在哪里和什么exercice1.identificateur.ex.nomprenom

标签: android android-emulator ip-address android-wifi ipv4


【解决方案1】:

很好的问题 WarrenFaith。我看起来像 DalvikVM 正在寻找预编译或生成的类(ex.nomprenom)。你的项目结构是什么?这些都是项目中的文件吗?

我认为它是你的包的一个 R 文件中的一个链接。

【讨论】:

  • 为了更有意义,我将班级名称从 nomprenom 改为 wifi1。
  • 这就是问题所在。看起来重命名还没有完全完成。尝试清理项目,从模拟器中删除应用并重新部署。
  • 同样的问题。我将 logcat 到 martin。我不能在这里发布它
  • 我加了日志,下次自己做吧。无论如何,您从“ListActivity”扩展,但您没有列表视图。将 ListActivity 更改为 Activity,它应该可以工作...
猜你喜欢
  • 2010-12-15
  • 2012-04-25
  • 1970-01-01
  • 2013-02-27
  • 2023-03-28
  • 2012-10-20
  • 2012-03-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多