这个异常大概意思是在主线程访问网络时出的异常。 Android在4.0之前的版本 支持在主线程中访问网络,但是在4.0以后对这部分程序进行了优化,也就是说访问网络的代码不能写在主线程中了。
为了把域名转化为ip地址,我在主线程中调用GetInetAddress函数。
public static String GetInetAddress(String host){//域名host转化为ip地址IPAddress返回 String IPAddress = ""; InetAddress ReturnStr1 = null; try { ReturnStr1 = java.net.InetAddress.getByName(host); IPAddress = ReturnStr1.getHostAddress(); System.out.println(IPAddress); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); return IPAddress; } return IPAddress; }
结果l出错,log信息为:
01-15 09:16:50.340: E/AndroidRuntime(3927): FATAL EXCEPTION: main 01-15 09:16:50.340: E/AndroidRuntime(3927): android.os.NetworkOnMainThreadException 01-15 09:16:50.340: E/AndroidRuntime(3927): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117) 01-15 09:16:50.340: E/AndroidRuntime(3927): at java.net.InetAddress.lookupHostByName(InetAddress.java:385) 01-15 09:16:50.340: E/AndroidRuntime(3927): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236) 01-15 09:16:50.340: E/AndroidRuntime(3927): at java.net.InetAddress.getByName(InetAddress.java:289) 01-15 09:16:50.340: E/AndroidRuntime(3927): at com.csipsimple.ui.HspLaunch.GetInetAddress(HspLaunch.java:655) 01-15 09:16:50.340: E/AndroidRuntime(3927): at com.csipsimple.ui.HspLaunch$LoginBtnListener.onClick(HspLaunch.java:264) 01-15 09:16:50.340: E/AndroidRuntime(3927): at android.view.View.performClick(View.java:4084) 01-15 09:16:50.340: E/AndroidRuntime(3927): at android.view.View$PerformClick.run(View.java:16966) 01-15 09:16:50.340: E/AndroidRuntime(3927): at android.os.Handler.handleCallback(Handler.java:615) 01-15 09:16:50.340: E/AndroidRuntime(3927): at android.os.Handler.dispatchMessage(Handler.java:92) 01-15 09:16:50.340: E/AndroidRuntime(3927): at android.os.Looper.loop(Looper.java:137) 01-15 09:16:50.340: E/AndroidRuntime(3927): at android.app.ActivityThread.main(ActivityThread.java:4746) 01-15 09:16:50.340: E/AndroidRuntime(3927): at java.lang.reflect.Method.invokeNative(Native Method) 01-15 09:16:50.340: E/AndroidRuntime(3927): at java.lang.reflect.Method.invoke(Method.java:511) 01-15 09:16:50.340: E/AndroidRuntime(3927): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:917) 01-15 09:16:50.340: E/AndroidRuntime(3927): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684) 01-15 09:16:50.340: E/AndroidRuntime(3927): at dalvik.system.NativeStart.main(Native Method)
解决方法有两个:
1、调用上面函数前,在onCreate函数里面添加如下代码:
//详见StrictMode文档 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build()); StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects().detectLeakedClosableObjects().penaltyLog().penaltyDeath().build());
2、使用Thread、Runnable、Handler这三个类
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.hsp_login_dialog); new Thread(runnable).start(); } Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); Bundle data = msg.getData(); String val = data.getString("value"); Log.i("mylog","转化后地址为-->" + val); } } Runnable runnable = new Runnable(){ @Override public void run() { // // TODO:在这里调用GetInetAddress函数处理,返回IPAddress. // Message msg = new Message(); Bundle data = new Bundle(); data.putString("value",IPAddress); msg.setData(data); handler.sendMessage(msg); } }
另外附经验技巧:
1、A.java文件里声明了public static变量a,在B.java文件对A.a赋值,在C.java文件中访问A.a时,发现A.a为null。
2、A.java里的public static final String变量一开始赋值可以为这种形式:public static final String host=GetInetAddress("hsp-study-01.eicp.net");(A.java文件中也定义了public static String GetInetAddress(String host)函数)。