通常我可以通过查看错误日志来了解问题,看看发生了什么。大多数时候,它给我一个行号,我可以从那里弄明白。
但现在,我迷路了。我不明白我的代码在哪里有问题。
我不确定在哪里开始排除此错误。我不是要求任何讲义或代码更正,所以我还没有显示我的代码,我只是想了解一下这个错误:
12-10 17:12:20.359: E/AndroidRuntime(14072): FATAL EXCEPTION: main
12-10 17:12:20.359: E/AndroidRuntime(14072): android.os.NetworkOnMainThreadException
12-10 17:12:20.359: E/AndroidRuntime(14072): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
12-10 17:12:20.359: E/AndroidRuntime(14072): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
12-10 17:12:20.359: E/AndroidRuntime(14072): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
12-10 17:12:20.359: E/AndroidRuntime(14072): at java.net.InetAddress.getAllByName(InetAddress.java:214)
12-10 17:12:20.359: E/AndroidRuntime(14072): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
12-10 17:12:20.359: E/AndroidRuntime(14072): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
12-10 17:12:20.359: E/AndroidRuntime(14072): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
12-10 17:12:20.359: E/AndroidRuntime(14072): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
12-10 17:12:20.359: E/AndroidRuntime(14072): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
12-10 17:12:20.359: E/AndroidRuntime(14072): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
12-10 17:12:20.359: E/AndroidRuntime(14072): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
12-10 17:12:20.359: E/AndroidRuntime(14072): at com.sencide.AndroidLogin.postLoginData(AndroidLogin.java:101)
12-10 17:12:20.359: E/AndroidRuntime(14072): at com.sencide.AndroidLogin.onClick(AndroidLogin.java:153)
12-10 17:12:20.359: E/AndroidRuntime(14072): at android.view.View.performClick(View.java:4211)
12-10 17:12:20.359: E/AndroidRuntime(14072): at android.view.View$PerformClick.run(View.java:17362)
12-10 17:12:20.359: E/AndroidRuntime(14072): at android.os.Handler.handleCallback(Handler.java:725)
12-10 17:12:20.359: E/AndroidRuntime(14072): at android.os.Handler.dispatchMessage(Handler.java:92)
12-10 17:12:20.359: E/AndroidRuntime(14072): at android.os.Looper.loop(Looper.java:137)
12-10 17:12:20.359: E/AndroidRuntime(14072): at android.app.ActivityThread.main(ActivityThread.java:5227)
12-10 17:12:20.359: E/AndroidRuntime(14072): at java.lang.reflect.Method.invokeNative(Native Method)
12-10 17:12:20.359: E/AndroidRuntime(14072): at java.lang.reflect.Method.invoke(Method.java:511)
12-10 17:12:20.359: E/AndroidRuntime(14072): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
12-10 17:12:20.359: E/AndroidRuntime(14072): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
12-10 17:12:20.359: E/AndroidRuntime(14072): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
假设您正在主线程上运行网络操作,请更改ThreadPolicy或在AsyncTask中运行它
AsyncTask是更好的解决方案这里有基本的轮廓外观here以获取更多信息:
private class LoginTask extends AsyncTask {
public LoginTask(Context context) {
super(context);
}
@Override
protected Object doInBackground(String... urls) {
try{
}catch{
}
return.....;
}
}
线程策略是一种糟糕的方式,但它的快速脏和Android开发指南建议反对,但它确实有效,但我建议反对它:
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
答案 1 :(得分:0)
查看AndroidLogin.java
的第101行。不应从UI线程调用方法postLoginData
。您需要启动一个新线程并将方法调用移到其中。