0

我登录并在 Edittext 中输入消息。但是当我单击发送按钮时,应用程序显示 Webview 弹出窗口大约几分钟,然后显示消息“不幸的是,AppName 已停止。”

WebView 弹出窗口,如 Facebook 上的 Webview 登录弹出窗口

MainActivity.java

public class MainActivity extends Activity {

public static Facebook facebook = new Facebook("apikey");
AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
String token;
Button login;
Button send;
EditText msg;
TextView onoff;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.postapp);
    login = (Button)findViewById(R.id.login);
    send = (Button)findViewById(R.id.send);
    msg = (EditText)findViewById(R.id.msg);
    onoff = (TextView)findViewById(R.id.onoff);
    login.setOnClickListener(new OnClickListener()
    {
        public void onClick(View v) {
            facebook.authorize(MainActivity.this, new String[]{"publish_stream"}, new DialogListener(){

                @Override
                public void onComplete(Bundle values) {
                    onoff.setText("On");
                    token =values.getString(Facebook.TOKEN);
                }

                @Override
                public void onFacebookError(FacebookError e) {
                    Log.d("FACEBOOK ERROR","FB ERROR. MSG: "+e.getMessage()+", CAUSE: "+e.getCause());
                }

                @Override
                public void onError(DialogError e) {
                    Log.e("ERROR","AUTH ERROR. MSG: "+e.getMessage()+", CAUSE: "+e.getCause());
                }

                @Override
                public void onCancel() {
                    Log.d("CANCELLED","AUTH CANCELLED");
                }
            });

        }
    });

    send.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {
            updateStatus(token);
        }
    });
}

public void updateStatus(String accessToken){
    try {
        Bundle bundle = new Bundle();
        bundle.putString("message",msg.getText().toString());
        bundle.putString(Facebook.TOKEN,accessToken);
        String response = facebook.request("me/feed",bundle,"POST");
        Log.d("UPDATE RESPONSE",""+response);
    } catch (MalformedURLException e) {
        Log.e("MALFORMED URL",""+e.getMessage());
    } catch (IOException e) {
        Log.e("IOEX",""+e.getMessage());
    }
}
}

和 postapp.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <Button
        android:id="@+id/login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Sign-in" />

    <TextView
        android:id="@+id/onoff"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="off"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <EditText
        android:id="@+id/msg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ems="10" />

    <Button
        android:id="@+id/send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Send" />
</LinearLayout>

单击发送时在 LogCat 中

12-05 05:08:57.376: E/AndroidRuntime(539): FATAL EXCEPTION: main
12-05 05:08:57.376: E/AndroidRuntime(539): android.os.NetworkOnMainThreadException
12-05 05:08:57.376: E/AndroidRuntime(539):  at  android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1084)
12-05 05:08:57.376: E/AndroidRuntime(539):  at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
12-05 05:08:57.376: E/AndroidRuntime(539):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
12-05 05:08:57.376: E/AndroidRuntime(539):  at java.net.InetAddress.getAllByName(InetAddress.java:220)
12-05 05:08:57.376: E/AndroidRuntime(539):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
12-05 05:08:57.376: E/AndroidRuntime(539):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
12-05 05:08:57.376: E/AndroidRuntime(539):  at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
12-05 05:08:57.376: E/AndroidRuntime(539):  at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
12-05 05:08:57.376: E/AndroidRuntime(539):  at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
12-05 05:08:57.376: E/AndroidRuntime(539):  at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
12-05 05:08:57.376: E/AndroidRuntime(539):  at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:460)
12-05 05:08:57.376: E/AndroidRuntime(539):  at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:432)
12-05 05:08:57.376: E/AndroidRuntime(539):  at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
12-05 05:08:57.376: E/AndroidRuntime(539):  at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
12-05 05:08:57.376: E/AndroidRuntime(539):  at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
12-05 05:08:57.376: E/AndroidRuntime(539):  at libcore.net.http.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:164)
12-05 05:08:57.376: E/AndroidRuntime(539):  at com.facebook.android.Util.openUrl(Util.java:193)
12-05 05:08:57.376: E/AndroidRuntime(539):  at com.facebook.android.Facebook.request(Facebook.java:777)
12-05 05:08:57.376: E/AndroidRuntime(539):  at facebook.appwall.MainActivity.updateStatus(MainActivity.java:84)
12-05 05:08:57.376: E/AndroidRuntime(539):  at facebook.appwall.MainActivity$2.onClick(MainActivity.java:74)
12-05 05:08:57.376: E/AndroidRuntime(539):  at android.view.View.performClick(View.java:3480)
12-05 05:08:57.376: E/AndroidRuntime(539):  at android.view.View$PerformClick.run(View.java:13983)
12-05 05:08:57.376: E/AndroidRuntime(539):  at android.os.Handler.handleCallback(Handler.java:605)
12-05 05:08:57.376: E/AndroidRuntime(539):  at android.os.Handler.dispatchMessage(Handler.java:92)
12-05 05:08:57.376: E/AndroidRuntime(539):  at android.os.Looper.loop(Looper.java:137)
12-05 05:08:57.376: E/AndroidRuntime(539):  at android.app.ActivityThread.main(ActivityThread.java:4340)
12-05 05:08:57.376: E/AndroidRuntime(539):  at java.lang.reflect.Method.invokeNative(Native Method)
12-05 05:08:57.376: E/AndroidRuntime(539):  at java.lang.reflect.Method.invoke(Method.java:511)
12-05 05:08:57.376: E/AndroidRuntime(539):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-05 05:08:57.376: E/AndroidRuntime(539):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-05 05:08:57.376: E/AndroidRuntime(539):  at dalvik.system.NativeStart.main(Native Method)
4

3 回答 3

0

您正在尝试在当前代码中对主 UI 线程进行网络操作。所以避免 UI 挂起和应用程序崩溃使用AsyncTask实现你的代码

将所有 facebook 网络操作代码doInBackground和用于更新 UI 内容的所有代码放入onPostExecute其中AsyncTask以避免“不幸的是,AppName 已停止”。信息

于 2012-12-04T20:42:40.193 回答
0

在蜂窝之外尝试您的应用程序。它不会在 3.0 以下的较低版本中显示此异常 在 2.2 或 2.3 上尝试它会起作用。 将对您有所帮助。

于 2012-12-05T05:11:11.257 回答
0

您可以在授权方法之前添加这些代码行

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
于 2012-12-04T22:41:12.563 回答