我有这个代码后,我的片段显示空白屏幕

时间:2014-11-24 10:09:51

标签: android

package com.example.mindwareuae;




import java.util.Arrays;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.InputFilter.LengthFilter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


import com.facebook.Request;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.UiLifecycleHelper;
import com.facebook.model.GraphUser;
import com.facebook.widget.LoginButton;
import com.facebook.widget.LoginButton.UserInfoChangedCallback;

public class FacebokkFragment extends Fragment{

    private LoginButton loginBtn;
    private Button postImageBtn;
    private Button updateStatusBtn;

    private TextView userName;

    private UiLifecycleHelper uiHelper;

    private static final List<String> PERMISSIONS = Arrays.asList("publish_actions");


    View rootView;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (rootView != null) {
            ViewGroup parent = (ViewGroup) rootView.getParent();
            if (parent != null) {
                parent.removeView(rootView);
            }

        }
        try
        {
        View rootView = inflater.inflate(R.layout.fragment_facebook,container,false);
         userName = (TextView)rootView.findViewById(R.id.user_name);
            loginBtn = (LoginButton) rootView.findViewById(R.id.fb_login_button);
            loginBtn.setUserInfoChangedCallback(new UserInfoChangedCallback() {
                @Override
                public void onUserInfoFetched(GraphUser user) {
                    if (user != null) {
                        userName.setText("Hello, " + user.getName());
                    } else {
                        userName.setText("You are not logged");
                    }
                }
            });


            postImageBtn = (Button)rootView.findViewById(R.id.fbpost_image);
            postImageBtn.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View view) {
                    postImage();
                }
            });

            updateStatusBtn = (Button)rootView.findViewById(R.id.fbupdate_status);
            updateStatusBtn.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                }
            });

            buttonsEnabled(false);

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

        return rootView;
        }


        public void buttonsEnabled(boolean isEnabled) {
            postImageBtn.setEnabled(isEnabled);
            updateStatusBtn.setEnabled(isEnabled);
        }

        public void postImage() {
            if (checkPermissions()) {
                Bitmap img = BitmapFactory.decodeResource(getResources(),
                        R.drawable.ic_launcher);
                Request uploadRequest = Request.newUploadPhotoRequest(
                        Session.getActiveSession(), img, new Request.Callback() {
                            @Override
                            public void onCompleted(Response response) {
                                Toast.makeText(getActivity(),
                                        "Photo uploaded successfully",
                                        Toast.LENGTH_LONG).show();
                            }
                        });
                uploadRequest.executeAsync();
            } else {
                requestPermissions();
            }
        }

//      public void postStatusMessage() {
//          if (checkPermissions()) {
//              Request request = Request.newStatusUpdateRequest(
//                      Session.getActiveSession(), message,
//                      new Request.Callback() {
//                          @Override
//                          public void onCompleted(Response response) {
//                              if (response.getError() == null)
//                                  Toast.makeText(getActivity(),
//                                          "Status updated successfully",
//                                          Toast.LENGTH_LONG).show();
//                          }
//                      });
//              request.executeAsync();
//          } else {
//              requestPermissions();
//          }
//      }

        public boolean checkPermissions() {
            Session s = Session.getActiveSession();
            if (s != null) {
                return s.getPermissions().contains("publish_actions");
            } else
                return false;
        }

        public void requestPermissions() {
            Session s = Session.getActiveSession();
            if (s != null)
                s.requestNewPublishPermissions(new Session.NewPermissionsRequest(
                        this, PERMISSIONS));
        }

//      @Override
//      public void onResume() {
//          super.onResume();
//          uiHelper.onResume();
//          buttonsEnabled(Session.getActiveSession().isOpened());
//      }
//
//      @Override
//      public void onPause() {
//          super.onPause();
//          uiHelper.onPause();
//      }
//
//      @Override
//      public void onDestroy() {
//          super.onDestroy();
//          if (rootView != null) {
//              ViewGroup parentViewGroup = (ViewGroup) rootView.getParent();
//              if (parentViewGroup != null) {
//                  parentViewGroup.removeAllViews();
//              }
//          }
//          uiHelper.onDestroy();
//      }

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

        @Override
        public void onSaveInstanceState(Bundle savedState) {
            super.onSaveInstanceState(savedState);
            uiHelper.onSaveInstanceState(savedState);
        }
}

我想从这个片段登录facbook ..但是我没有在我的xml文件中找到任何东西

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:facebook="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:padding="20dp" 
    android:background="#EEEEEE">

    <com.facebook.widget.LoginButton
        android:id="@+id/fb_login_button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        facebook:confirm_logout="false"
        facebook:fetch_user_info="true" />

    <TextView
        android:id="@+id/fb_user"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="10dp"
        android:textSize="18sp" />

    <Button
        android:id="@+id/fbupdate_status"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="update_status" />

    <Button
        android:id="@+id/fbpost_image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="post_image" />

</LinearLayout>

这是我的xml文件,为什么我无法使用我的facebook登录按钮获取该片段。 请任何人都可以解释我。

2 个答案:

答案 0 :(得分:2)

创建FragmentActivity并将fragment添加到FragmentActivity

public class MainActivity extends FragmentActivity{
    FacebokkFragment fbFragment;
    ArrayList<Fragment> fragemetnList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        fbFragment = new FacebokkFragment();
        fragemetnList = getFragmnetsList();
        setContentView(R.layout.activity_main);

    }

    ArrayList<Fragment> getFragmnetsList() {
        ArrayList<Fragment> fragments = new ArrayList<Fragment>();
        fragments.add(fbFragment);
        // you can also add more fragments here
        return fragments;
    }
}

现在在 res / layout 目录中创建 activity_main.xml 文件     不要忘记将MainActivity输入AndroidManifest.xml文件     愿这段代码解决您的问题

答案 1 :(得分:0)

if (rootView != null) {
            ViewGroup parent = (ViewGroup) rootView.getParent();
            if (parent != null) {
                parent.removeView(rootView);
            }

        }

这部分代码真的需要吗?尝试删除它。

试试这个

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_facebook, container, false);

        ...

        return rootView;