片段未显示任何内容

时间:2019-04-06 09:17:03

标签: java android-fragments

因此,我试图在我的个人资料片段中显示此Facebook登录界面。当我切换到片段时,它显示为空白。我不明白我犯了什么错误。请帮助我,这里还是新手。谢谢。

这是我的片段代码:

public class ProfileFragment extends Fragment {

private LoginButton loginButton;
private CircleImageView circleImageView;
private TextView usernameTextView, emailTextView;

private CallbackManager callbackManager;

Context context;


public ProfileFragment() {
    // Required empty public constructor
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
}

@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View getView = inflater.inflate(R.layout.fragment_profile, container, false);

    return getView();
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    loginButton = view.findViewById(R.id.login_button);
    circleImageView = view.findViewById(R.id.profile_pic);
    usernameTextView = view.findViewById(R.id.profile_name);
    emailTextView = view.findViewById(R.id.profile_email);

    callbackManager = CallbackManager.Factory.create();

    loginButton.setReadPermissions(Arrays.asList("email","public_profile"));

    checkLoginStatus();

    loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {

        }

        @Override
        public void onCancel() {

        }

        @Override
        public void onError(FacebookException error) {

        }
    });

    AccessTokenTracker tokenTracker = new AccessTokenTracker() {
        @Override
        protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken currentAccessToken) {

            if(currentAccessToken == null){
                usernameTextView.setText("");
                emailTextView.setText("");
                circleImageView.setImageResource(0);
                Toast.makeText(getContext(), "User logged out", Toast.LENGTH_SHORT).show();
            }
            else{
                loadUserProfile(currentAccessToken);
            }

        }
    };

}

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

private void loadUserProfile(AccessToken newAccessToken){

    GraphRequest request = GraphRequest.newMeRequest(newAccessToken, new GraphRequest.GraphJSONObjectCallback() {
        @SuppressLint("CheckResult")
        @Override
        public void onCompleted(JSONObject object, GraphResponse response) {

            try {
                String first_name = object.getString("first_name");
                String last_name = object.getString("last_name");
                String email = object.getString("email");
                String id = object.getString("id");

                String image_url = "https://graph.facebook.com/"+id+"/picture?type=normal";

                usernameTextView.setText(first_name+" "+last_name);
                emailTextView.setText(email);

                RequestOptions requestOptions = new RequestOptions();
                requestOptions.dontAnimate();

                Glide.with(getContext()).load(image_url).into(circleImageView);



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

        }
    });

    Bundle parameter = new Bundle();
    parameter.putString("fields","first_name,last_name,email,id");
    request.setParameters(parameter);
    request.executeAsync();

}

private void checkLoginStatus(){
    if(AccessToken.getCurrentAccessToken()!=null){
        loadUserProfile(AccessToken.getCurrentAccessToken());
    }
}

}

这是XML代码:

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

    <de.hdodenhof.circleimageview.CircleImageView
        android:layout_width="175dp"
        android:layout_height="175dp"
        android:layout_gravity="center_horizontal"
        android:id="@+id/profile_pic"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:gravity="center"
        android:textSize="20sp"
        android:layout_marginTop="20dp"
        android:id="@+id/profile_name"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:gravity="center"
        android:textSize="18sp"
        android:layout_marginTop="10dp"
        android:id="@+id/profile_email"
        />
    <com.facebook.login.widget.LoginButton
        android:id="@+id/login_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="25dp"
        />
</LinearLayout>

在我的XML或代码中可能是错误的,但我仍然没有弄清楚。这是我的学校项目

1 个答案:

答案 0 :(得分:0)

在您的onCreateView方法中,您有一个错误 替换:

@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View getView = inflater.inflate(R.layout.fragment_profile, container, false);

    return getView();
}

至:

@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View getView = inflater.inflate(R.layout.fragment_profile, container, false);

    return getView;
}

错误地,您调用了名为getView()的Fragment类的函数,而不是返回自己的膨胀视图对象。