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