活动不会在onCreate()中刷新布局

时间:2015-04-21 20:33:38

标签: java android android-layout android-activity picasso

我正在创建一个登录Facebook的应用程序基于两个活动,一旦登录到第二步活动并加载信息。问题是,当我用Picasso加载图像时,我只需在图像出现之前登录两次。 我将Picasso的代码放在onCreate,onStart,onResume上,但是在第一次登录时没有加载图像。 请帮帮我们。 这是代码:

Picasso.with(getApplicationContext()).load(personPhotoUrl).into(avatar);

这是第二项活动

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if(Start.SignWithFacebook == true)
    {
        name.setText(FacebookLogin.NameFace);
        desc.setText(FacebookLogin.EmailFace);
        try
        {
            Picasso.with(getApplicationContext()).load("https://graph.facebook.com/" + FacebookLogin.idFacebook + "/picture?type=large").into(avatar);
        }catch(Exception e){}
    }
}

这是第二个活动的呼叫开始时的主要活动:

        public void onClick(View v) {
        if(v.getId() == R.id.authButton) {
            if(SignWithFacebook == true)
            {
            Intent second = new Intent(this, SecondActivity.class);
            startActivity(second);
            this.finish();
            }
            else
            {
            SignWithFacebook = true;
            Intent face = new Intent(this, FacebookLogin.class);
            startActivity(face);
            }
        }

这是FacebookActivity:

    public class FacebookLogin extends Activity {
    private String TAG = "Start";
    public static String NameFace;
    public static String idFacebook;
    public static String EmailFace;
    public static CallbackManager callbackManager;
    private String id;
    private String firstName;
    private String lastName;
    private String email;
    ProfileTracker profileTracker;
    public void onCreate(Bundle savedInstanceState){
          super.onCreate(savedInstanceState);
        FacebookSdk.sdkInitialize(getApplicationContext());
        Start.mBut.setReadPermissions(Arrays.asList("public_profile", "email", "user_friends"));
        callbackManager = CallbackManager.Factory.create();
        Start.mBut.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
                @Override
                public void onSuccess(LoginResult loginResult) {
                    GraphRequest.newMeRequest(
                            loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
                                @Override
                                public void onCompleted(JSONObject user, GraphResponse response) {
                                    if (response.getError() != null) {
                                        // handle error
                                    } else {
                                        Start.SignWithFacebook = true;
                                        id = user.optString("id");
                                        firstName = user.optString("first_name");
                                        lastName = user.optString("last_name");
                                        email = user.optString("email");
                                        Log.i(TAG,"User ID "+ id);
                                        EmailFace = "" + email;
                                        Log.i(TAG,"Email "+ email);
                                        Start.text.setText("" + email);
                                        idFacebook = id;
                                    }
                                    NameFace = firstName + " " + lastName;
                                    Toast.makeText(getApplicationContext(), "Log in with " + NameFace, Toast.LENGTH_LONG).show();
                                }
                            }).executeAsync();
            }

                @Override
                public void onCancel() {
                    Start.SignWithFacebook = false;
            }

                @Override
                public void onError(FacebookException exception) {

            }
        });
}

    public void onStart() {
        super.onStart();

    }

     @Override
     public void onActivityResult(int requestCode, int resultCode, Intent data) {
         super.onActivityResult(requestCode, resultCode, data);
     }

     public void onResume()
     {
         super.onResume();
        finish();

     }
}

2 个答案:

答案 0 :(得分:1)

您必须下载图片Synchronously

Bitmap imageBitmap =  Picasso.with(getApplicationContext()).load("https://graph.facebook.com/" + FacebookLogin.idFacebook + "/picture?type=large").get()

之后,您可以设置图像:

ImageView myImageView = (ImageView) findViewById(R.id.myImageView);
myImageView.setImageBitmap(imageBitmap);

注意:您必须使用AsyncTask来执行此操作。

答案 1 :(得分:0)

根据我们在评论中与OP进行的讨论,图片网址错误,因为"graph.facebook.com/null/picture?type=large"的{​​{1}}值为FacebookLogin.idFacebook,因此您第一次将null设为网址/ p>