Hallo,
zugegeben ich bin nicht der perfekte programmierer in Java, ich hatte Java damals in der Schule aber das ist ja doch wieder 4 Jahre her
Ich bin eher mit anderne Programmiersprachen vertraut. Aber jetzt jetzt zum Thema.
Was meinst du mit einem seperaten Thread? - bzw. wie stell ich das an? - habs jetzt mal in einer eigenen Funktion gespielen, mit return, hier der Code:
Code
public class testActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView info = (TextView)findViewById(R.id.info_text);
info.setText(ping());
}
public String ping()
{
InetAddress in;
in=null;
try
{
in = InetAddress.getByName("127.0.0.1");
}
catch (UnknownHostException e)
{
e.printStackTrace();
}
try
{
if(in.isReachable(5000))
return "Responde OK";
else
return "No responde: Time out";
}
catch (IOException e)
{
return e.toString();
}
}
}
Alles anzeigen
Es funktioniert auch mit 127.0.0.1 nicht, und mit der SDK von 2.3.3 auf einem Android Handy mit 2.3.4 klappt es fein.
EDIT:
Log:
Code
03-24 12:31:36.355: D/dalvikvm(535): Not late-enabling CheckJNI (already on)
03-24 12:31:36.605: I/dalvikvm(535): Turning on JNI app bug workarounds for target SDK version 10...
03-24 12:31:36.975: D/AndroidRuntime(535): Shutting down VM
03-24 12:31:36.975: W/dalvikvm(535): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
03-24 12:31:36.996: E/AndroidRuntime(535): FATAL EXCEPTION: main
03-24 12:31:36.996: E/AndroidRuntime(535): java.lang.RuntimeException: Unable to start activity ComponentInfo{sarl.test.wi/sarl.test.wi.testActivity}: android.os.NetworkOnMainThreadException
03-24 12:31:36.996: E/AndroidRuntime(535): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
03-24 12:31:36.996: E/AndroidRuntime(535): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
03-24 12:31:36.996: E/AndroidRuntime(535): at android.app.ActivityThread.access$600(ActivityThread.java:123)
03-24 12:31:36.996: E/AndroidRuntime(535): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
03-24 12:31:36.996: E/AndroidRuntime(535): at android.os.Handler.dispatchMessage(Handler.java:99)
03-24 12:31:36.996: E/AndroidRuntime(535): at android.os.Looper.loop(Looper.java:137)
03-24 12:31:36.996: E/AndroidRuntime(535): at android.app.ActivityThread.main(ActivityThread.java:4424)
03-24 12:31:36.996: E/AndroidRuntime(535): at java.lang.reflect.Method.invokeNative(Native Method)
03-24 12:31:36.996: E/AndroidRuntime(535): at java.lang.reflect.Method.invoke(Method.java:511)
03-24 12:31:36.996: E/AndroidRuntime(535): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-24 12:31:36.996: E/AndroidRuntime(535): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-24 12:31:36.996: E/AndroidRuntime(535): at dalvik.system.NativeStart.main(Native Method)
03-24 12:31:36.996: E/AndroidRuntime(535): Caused by: android.os.NetworkOnMainThreadException
03-24 12:31:36.996: E/AndroidRuntime(535): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
03-24 12:31:36.996: E/AndroidRuntime(535): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
03-24 12:31:36.996: E/AndroidRuntime(535): at libcore.io.IoBridge.connectErrno(IoBridge.java:138)
03-24 12:31:36.996: E/AndroidRuntime(535): at libcore.io.IoBridge.connect(IoBridge.java:112)
03-24 12:31:36.996: E/AndroidRuntime(535): at java.net.InetAddress.isReachable(InetAddress.java:730)
03-24 12:31:36.996: E/AndroidRuntime(535): at java.net.InetAddress.isReachable(InetAddress.java:684)
03-24 12:31:36.996: E/AndroidRuntime(535): at java.net.InetAddress.isReachable(InetAddress.java:654)
03-24 12:31:36.996: E/AndroidRuntime(535): at sarl.test.wi.testActivity.ping(testActivity.java:37)
03-24 12:31:36.996: E/AndroidRuntime(535): at sarl.test.wi.testActivity.onCreate(testActivity.java:18)
03-24 12:31:36.996: E/AndroidRuntime(535): at android.app.Activity.performCreate(Activity.java:4465)
03-24 12:31:36.996: E/AndroidRuntime(535): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
03-24 12:31:36.996: E/AndroidRuntime(535): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
03-24 12:31:36.996: E/AndroidRuntime(535): ... 11 more
Alles anzeigen