2017-02-09 3 views
0

私はアンドロイドデバイスのホスト名を取得するメソッドを実装しています。私はInetAddressクラスを使っています。しかし、私は致命的な例外を取得しています。方法は次のとおりです。私はtry/catchブロックを追加しましたが、まだ動作していません。どんな助けもありがたい。編集して、コメントを追加し、例外エラーアンドロイドデバイスからホスト名を取得する方法

E/AndroidRuntime: FATAL EXCEPTION: main 
        Process: com.example.george.droidnet, PID: 23439 
        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.george.droidnet/com.example.george.droidnet.TcpConfigActivity}: android.os.NetworkOnMainThreadException 
         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) 
         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
         at android.app.ActivityThread.-wrap11(ActivityThread.java) 
         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
         at android.os.Handler.dispatchMessage(Handler.java:102) 
         at android.os.Looper.loop(Looper.java:148) 
         at android.app.ActivityThread.main(ActivityThread.java:5417) 
         at java.lang.reflect.Method.invoke(Native Method) 
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
        Caused by: android.os.NetworkOnMainThreadException 
         at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273) 
         at java.net.InetAddress.lookupHostByName(InetAddress.java:431) 
         at java.net.InetAddress.getLocalHost(InetAddress.java:409) 
         at com.example.george.droidnet.TcpConfigActivity.getHostname(TcpConfigActivity.java:100) 
         at com.example.george.droidnet.TcpConfigActivity.onCreate(TcpConfigActivity.java:40) 
         at android.app.Activity.performCreate(Activity.java:6237) 
         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)  
         at android.app.ActivityThread.-wrap11(ActivityThread.java)  
         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)  
         at android.os.Handler.dispatchMessage(Handler.java:102)  
         at android.os.Looper.loop(Looper.java:148)  
         at android.app.ActivityThread.main(ActivityThread.java:5417)  
         at java.lang.reflect.Method.invoke(Native Method)  
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)  
+0

"私は致命的な例外を受けています" - あなたの質問を編集し、**この例外に関連するJavaスタックトレース**を投稿してください。 – CommonsWare

答えて

0
Caused by: android.os.NetworkOnMainThreadException 

は、ネットワーキングを行うには、別のスレッドを使用し

/** 
    * getHostname returns the hostname of android device as string 
    * 
    * @param context 
    * @return hostname 
    */ 

    public String getHostname(Context context) { 
     String hostName; 
     try { 
      InetAddress netHost = InetAddress.getLocalHost(); 
      hostName = netHost.getHostName(); 
     } catch (UnknownHostException ex) { 
      hostName = null; 
     } 

     return hostName; 
    } 

致命的な例外。 AsyncTaskがお手伝いします。

+0

私はWifiManagerクラスも使用していますが、それはメインスレッドでの作業について不平を言っていませんでした。私もそのスレッド用に別のスレッドを作成する必要がありますか?ありがとう私は別の質問を知っている – miatech

関連する問題