不幸的是,AppName在点击按钮后停止了

时间:2012-12-04 20:38:54

标签: java android facebook-graph-api

我在Edittext中登录并输入Message。但是当我点击发送按钮时,应用程序会显示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)

3 个答案:

答案 0 :(得分:0)

您正尝试在当前代码中对主UI线程执行网络操作。因此,请避免挂起UI并使用AsyncTask

实现代码崩溃

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

答案 1 :(得分:0)

您可以在授权方法

之前添加这些代码行
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

答案 2 :(得分:0)

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