开发者

How to get the ip address with an emulator android

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 mu开发者_如何学JAVAst have a ListView whose id attribute is 'android.R.id.list'


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜