我是android开发的新手。这是我的代码
boolean isReachable = false;
try{
isReachable = InetAddress.getByName(MyIPAddress).isReachable(reachableTimeout);
} catch (Exception e){
Log.e("InetAddress", e.getMessage());
}finally {
if (!onlyReachables || isReachable) {
// Do something
}
}
当我AndroidManifest.xml
没有这些行时它工作正常
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
当它出现时,我的应用程序“不幸的是,myApp 已停止”
任何人都可以帮助修复它吗?
我的日志猫:
E/AndroidRuntime(9310): FATAL EXCEPTION: main
E/AndroidRuntime(9310): java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.thesis.wifihotspot/com.thesis.wifihotspot.MainActivity}:
java.lang.NullPointerException: println needs a message
E/AndroidRuntime(9310): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
E/AndroidRuntime(9310): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
E/AndroidRuntime(9310): at
android.app.ActivityThread.access$700(ActivityThread.java:140)
E/AndroidRuntime(9310): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
E/AndroidRuntime(9310): at
android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(9310): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(9310): at
android.app.ActivityThread.main(ActivityThread.java:4921)
E/AndroidRuntime(9310): at
java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(9310): at
java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(9310): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
E/AndroidRuntime(9310): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
E/AndroidRuntime(9310): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(9310): Caused by: java.lang.NullPointerException:
println needs a message
E/AndroidRuntime(9310): at android.util.Log.println_native(Native Method)
E/AndroidRuntime(9310): at android.util.Log.e(Log.java:297)
E/AndroidRuntime(9310): at
com.thesis.wifihotspot.MainActivity.setClientsList(MainActivity.java:226)
E/AndroidRuntime(9310): at
com.thesis.wifihotspot.MainActivity.onCreate(MainActivity.java:58)
E/AndroidRuntime(9310): at
android.app.Activity.performCreate(Activity.java:5206)
E/AndroidRuntime(9310): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
E/AndroidRuntime(9310): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
E/AndroidRuntime(9310): ... 11 more
更新: 根据此链接,从 Android 3.0 (Honeycomb) 开始,如果在用户界面线程中访问网络,则 StrictMode 配置为因 NetworkOnMainThreadException 异常而崩溃。所以我需要添加这些代码才能使应用程序正常工作。
StrictMode.ThreadPolicy 策略 = 新的 StrictMode。ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy);
感谢您的支持。