1

我想在 DialogListener 中完成 Facebook 登录后开始一个新活动。登录成功 没有错误出现,但活动没有开始。如果您需要 logcat,我可以给您发电子邮件。以下是我的代码。

这是我的家庭活动

package com.example.faceb;

import com.facebook.android.AsyncFacebookRunner;
import com.facebook.android.Facebook;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class HomeActivity extends Activity implements OnClickListener{
    private static final String TAG = "Facebook";
    private Button mLogin, mLogout, mShare;
    private Facebook facebook;
    private AsyncFacebookRunner abRunner;

    public boolean flag;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_home);

        facebook = new Facebook("479652662068145");
        abRunner = new AsyncFacebookRunner(facebook);

        mLogin= (Button) findViewById(R.id.Login);
        mLogout= (Button) findViewById(R.id.Logout);

        mLogin.setOnClickListener(this);
        mLogout.setOnClickListener(this);     
    }

    @Override
    public void onClick(View v){
        int id = v.getId();
        switch(id){
            case R.id.Login:
                FacebookLoginDialog login = new FacebookLoginDialog(this);         
                facebook.authorize(this, login);
                if (func()==true)         
                {
                    Intent i = new Intent(this, SearchActivity.class);
                    startActivity(i);
                }
                break;
            case R.id.Logout:
                FacebookLogoutRequest logout = new FacebookLogoutRequest(this);
                abRunner.logout(this, logout);
                Toast toast = Toast.makeText(this, "You Are Logged Out", Toast.LENGTH_SHORT);
                toast.show();
                break;
            default:
                break;
        }
    }

    public static boolean func() {
        return true;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_home, menu);
        return true;
    }
}

并按照我的 FacebookLoginDialog 类

public class FacebookLoginDialog implements DialogListener{
    @Override
    public void onComplete(Bundle values) {
        HomeActivity.func();
    }

    @Override
    public void onFacebookError(FacebookError e) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onError(DialogError e) {
        // TODO Auto-generated method stub  
    }

    @Override
    public void onCancel() {
        // TODO Auto-generated method stub  
    }
}

此对话框从我的HomeActivity. 请帮助我搜索了很多并尝试但无法解决此问题。

4

4 回答 4

1

我认为您可以启动活动,而不是在单击按钮时启动对话框,而是要在登录后启动该活动。onCreate()然后在新活动的内部调用对话框。这样您就可以关闭对话框onComplete()并自动出现新屏幕。只需在活动中保留一个标志,并仅在登录成功时将其设置为 true。这样,如果在对话框关闭后标志为假,您就可以完成新活动。

于 2012-10-03T09:21:25.017 回答
1

首先,您是否正确登录?

尝试以下操作:

1) 注释onComplete()方法中的代码,然后放一条日志消息。

2)您最好使用带有3个参数的授权方法facebook.authorize(context,permission[],listener)

3)检查您是否正确登录使用facebook.isSessionValid() { // log a msg}

您必须能够在上面的块中看到味精......然后才开始新的活动

4)此外,如果您可以发布您的 MainActivity ,我们可以更好地回答....

于 2012-10-03T09:45:27.637 回答
1

在您的 onComplete 中使用 getApplicationContext 来获取上下文,您的问题将得到解决。代码会像

Intent i = new Intent(getApplicationContext(), SearchActivity.class);
    context.startActivity(i);

我已经检查过了,它工作正常......

if(access_token != null) {
                    facebook.setAccessToken(access_token);
                }
                if(expires != 0) {
                    facebook.setAccessExpires(expires);
                }

                //if(!facebook.isSessionValid()) {

                facebook.authorize((Activity) ctx, new String[] {"publish_stream"}, new DialogListener() {
                    public void onComplete(Bundle values) {
                        SharedPreferences.Editor editor = mPrefs.edit();
                        editor.putString("access_token", facebook.getAccessToken());
                        editor.putLong("access_expires", facebook.getAccessExpires());
                        editor.commit();

                        Toast.makeText(getApplicationContext(), "Posting To Your Wall !", Toast.LENGTH_SHORT).show();

                        Log.d("Graph Api", "Posting To Wall");
                        try {
                            Bitmap bitmap = BitmapFactory.decodeResource(ctx.getResources(),
                                R.drawable.icon);
                            ByteArrayOutputStream stream = new ByteArrayOutputStream();
                            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
                            byte[] bitMapData = stream.toByteArray();

                            String response = facebook.request("me");
                            Bundle parameters = new Bundle();
                            parameters.putString("message", "" + edt.getText().toString() + "...");
                            parameters.putByteArray("picture", bitMapData);
                            response = facebook.request("photos", parameters, 
                                    "POST");
                            Log.d("Tests", "got response: " + response);
                            if (response == null || response.equals("") || 
                                    response.equals("false")) {
                               Log.v("Post Error", "Error Occured While Posting in Facebook");
                               Toast.makeText(getApplicationContext(), "Error Occured While Posting in Facebook !", Toast.LENGTH_SHORT).show();

                            }
                            else {
                                Log.v("Post Success", "Posting Succesful in Facebook");
                                Toast.makeText(getApplicationContext(), "Posting Succesful in Facebook !", Toast.LENGTH_SHORT).show();
                                edt.setText("");
                            }

                        } catch(Exception e) {
                            e.printStackTrace();
                        }

                    }

                    public void onFacebookError(FacebookError error) {
                        Toast.makeText(getApplicationContext(), "Error Occured While Authenticating in Facebook !", Toast.LENGTH_SHORT).show();
                        Log.v("FaceBook Error", ""+error);                      
                    }

                    public void onError(DialogError e) {
                        Toast.makeText(getApplicationContext(), "DiaLogError Occured While Authenticating in Facebook !", Toast.LENGTH_SHORT).show();
                        Log.v("DialogError","" + e);
                    }

                    public void onCancel() {
                        Toast.makeText(getApplicationContext(), "You Have Canceled !", Toast.LENGTH_SHORT).show();
                    }
                });
                //}
            }
于 2012-10-03T10:05:12.050 回答
0

您不能在 nonUIThread 中使用 Intent 触发活动。您应该在 onComplete() 中使用回调函数,通知用户注销或登录成功。然后在回调函数的帮助下调用 Activity 中的 Intent 之后..

于 2012-10-03T12:15:00.790 回答