0

我的 Android 应用程序中有一个登录按钮。当我登录时它工作正常。但是当我尝试注销时出现此错误。我无法弄清楚是什么导致了错误。

11-12 14:44:12.358: E/AndroidRuntime(2334): FATAL EXCEPTION: main
11-12 14:44:12.358: E/AndroidRuntime(2334): android.os.NetworkOnMainThreadException
11-12 14:44:12.358: E/AndroidRuntime(2334):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
11-12 14:44:12.358: E/AndroidRuntime(2334):     at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
11-12 14:44:12.358: E/AndroidRuntime(2334):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
11-12 14:44:12.358: E/AndroidRuntime(2334):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
11-12 14:44:12.358: E/AndroidRuntime(2334):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
11-12 14:44:12.358: E/AndroidRuntime(2334):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
11-12 14:44:12.358: E/AndroidRuntime(2334):     at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:341)
11-12 14:44:12.358: E/AndroidRuntime(2334):     at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
11-12 14:44:12.358: E/AndroidRuntime(2334):     at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
11-12 14:44:12.358: E/AndroidRuntime(2334):     at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
11-12 14:44:12.358: E/AndroidRuntime(2334):     at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:461)
11-12 14:44:12.358: E/AndroidRuntime(2334):     at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:433)
11-12 14:44:12.358: E/AndroidRuntime(2334):     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
11-12 14:44:12.358: E/AndroidRuntime(2334):     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
11-12 14:44:12.358: E/AndroidRuntime(2334):     at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
11-12 14:44:12.358: E/AndroidRuntime(2334):     at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
11-12 14:44:12.358: E/AndroidRuntime(2334):     at libcore.net.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:271)
11-12 14:44:12.358: E/AndroidRuntime(2334):     at com.facebook.android.Util.openUrl(Util.java:215)
11-12 14:44:12.358: E/AndroidRuntime(2334):     at com.facebook.android.Facebook.request(Facebook.java:775)
11-12 14:44:12.358: E/AndroidRuntime(2334):     at com.facebook.android.Facebook.request(Facebook.java:691)
11-12 14:44:12.358: E/AndroidRuntime(2334):     at com.facebook.android.Facebook.logout(Facebook.java:650)
11-12 14:44:12.358: E/AndroidRuntime(2334):     at com.example.facebook.MainActivity.onClick(MainActivity.java:57)
11-12 14:44:12.358: E/AndroidRuntime(2334):     at android.view.View.performClick(View.java:4084)
11-12 14:44:12.358: E/AndroidRuntime(2334):     at android.view.View$PerformClick.run(View.java:16966)
11-12 14:44:12.358: E/AndroidRuntime(2334):     at android.os.Handler.handleCallback(Handler.java:615)
11-12 14:44:12.358: E/AndroidRuntime(2334):     at android.os.Handler.dispatchMessage(Handler.java:92)
11-12 14:44:12.358: E/AndroidRuntime(2334):     at android.os.Looper.loop(Looper.java:137)
11-12 14:44:12.358: E/AndroidRuntime(2334):     at android.app.ActivityThread.main(ActivityThread.java:4745)
11-12 14:44:12.358: E/AndroidRuntime(2334):     at java.lang.reflect.Method.invokeNative(Native Method)
11-12 14:44:12.358: E/AndroidRuntime(2334):     at java.lang.reflect.Method.invoke(Method.java:511)
11-12 14:44:12.358: E/AndroidRuntime(2334):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-12 14:44:12.358: E/AndroidRuntime(2334):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-12 14:44:12.358: E/AndroidRuntime(2334):     at dalvik.system.NativeStart.main(Native Method)

这是我的注销代码

private void updateButtonImage() {
        // TODO Auto-generated method stub
        if(fb.isSessionValid()){
            button.setImageResource(R.drawable.logout_button);
        }else{
            button.setImageResource(R.drawable.login_button);
        }
    }


    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        if(fb.isSessionValid()){
            try {
                fb.logout(getApplicationContext());
                updateButtonImage();
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }else{
            fb.authorize(MainActivity.this,new String[] {"email", "publish_stream"}, new DialogListener() {

                public void onFacebookError(FacebookError e) {
                    // TODO Auto-generated method stub
                    Toast.makeText(getApplicationContext(), "Facebook error", Toast.LENGTH_SHORT).show();       
                }

                public void onError(DialogError e) {
                    // TODO Auto-generated method stub
                    Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();        
                }

                public void onComplete(Bundle values) {
                    // TODO Auto-generated method stub
                    updateButtonImage();
                }

                public void onCancel() {
                    // TODO Auto-generated method stub
                    Toast.makeText(getApplicationContext(), "Cancel", Toast.LENGTH_SHORT).show();   
                }
            });
        }
    }

谁能指出导致错误的原因?

谢谢。

4

1 回答 1

4

当应用程序尝试在其主线程上执行网络操作时,将引发此异常。在 AsyncTask 或 Thread 中运行您的代码。

onCreate()或在方法中添加以下代码。

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy); 
于 2012-11-12T06:49:02.310 回答