验证后的facebook集成页面不会进入android中的下一个活动

时间:2016-03-16 09:59:24

标签: android facebook facebook-graph-api

我正在使用delaroy/AndroidFacebook中的示例尝试在android中进行facebook集成。当我运行代码示例时,它运行完美,因此想到在我的项目中集成,在项目中我使用片段中的facebook登录按钮并给出facebook也在其中初始化。 Facebook页面的网页打开完美,登录,登录后显示消息Image for authorize app。单击确定后,Webview将关闭,而不会进入下一个活动。

main.xml中

  `<?xml version="1.0" encoding="utf-8"?>
  <android.support.design.widget.CoordinatorLayout    
  xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
  android:fitsSystemWindows="true"
  tools:context="com.example.test.framecamera.MainActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">
</android.support.design.widget.AppBarLayout>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />
    <ImageView
        android:id="@+id/preview"
        android:layout_below="@+id/toolbar"
        android:layout_width="100dp"
        android:layout_height="100dp"
      android:layout_centerHorizontal="true"
        android:layout_margin="10dp"/>      

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_menu_share" />

</android.support.design.widget.CoordinatorLayout>`

fragment_sharelist.xml

 `<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="@+id/frame1"
android:layout_height="match_parent"
tools:context="com.example.test.framecamera.sharelist">

<!-- TODO: Update blank fragment layout -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linear_1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#AAffffff">
    <com.facebook.login.widget.LoginButton
    android:id="@+id/btn_fblogin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="250dp"
        />        
 </RelativeLayout>
 </FrameLayout>`

Imageview.java

   public class Imageview  extends AppCompatActivity {
private FloatingActionButton fab;
Bitmap mbitmap;
CallbackManager callbackManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    Bundle extras = getIntent().getExtras();
    byte[] photo = extras.getByteArray("photo");
    Button but1=(Button)findViewById(R.id.sent);
    but1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Uri uri = Uri.parse("mailto:mayurasekar@gmail.com");
            Intent myActivity2 = new Intent(Intent.ACTION_SENDTO, uri);
            myActivity2.putExtra(Intent.EXTRA_SUBJECT,
                    "Customer comments/questions");
            startActivity(myActivity2);
        }});
    final Bitmap bitmap  = BitmapFactory.
    decodeByteArray(photo, 0,  photo.length);
    mbitmap=bitmap;
    ImageView imgView = (ImageView)findViewById(R.id.preview);
    imgView.setImageBitmap(bitmap);
    fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            new sharelist();
            Fragment f= sharelist.newInstance(mbitmap);
            getSupportFragmentManager()
                    .beginTransaction()
                    .add(android.R.id.content, f, "loginfragment")
                    .attach(f)
                    .commit();
           }
    });
    }
  @Override
      public void onActivityResult(int requestCode, int resultCode,
    Intent  data) {
     Fragment f = getSupportFragmentManager().findFragmentById(R.id.frame1);
    f.onActivityResult(requestCode, resultCode, data);
   /* here the null pointer exception occurs */
    System.out.println("data "+data.toString());
    Log.e("data", data.toString());
    super.onActivityResult(requestCode, resultCode, data);
   }
   }      

sharelist.java

package com.example.test.framecamera;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.example.test.framecamera.facebook.fbActivity;
import com.example.test.framecamera.google.goActivity;
import com.example.test.framecamera.twitter.twitActivity;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.GraphRequest;
import com.facebook.GraphResponse;
import com.facebook.appevents.AppEventsLogger;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;

import org.json.JSONObject;

public class sharelist extends Fragment {
Utils utils;
Bitmap mBitmap;
CallbackManager callbackManager;
   public sharelist() {
    // Required empty public constructor
}
public static sharelist newInstance(Bitmap bitmap) {
    sharelist f = new sharelist();

    // Supply index input as an argument.
    Bundle args = new Bundle();
    args.putParcelable("index", bitmap);
    f.setArguments(args);

    return f;
}
public Bitmap getBitmap() {
    return getArguments().getParcelable("index");
}
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    facebookSDKInitialize();
    View v =inflater.inflate(R.layout.fragment_sharelist, container, false);
   final LoginButton fb=(LoginButton)v.findViewById(R.id.btn_fblogin);
    mBitmap=getBitmap();
    utils = new Utils();   
    fb.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (utils
                    .isNetworkConnected(getActivity()) == false) {
                showAlertBox();
            } else {

                fb.setReadPermissions("email,publish_actions");
                getLoginDetails(fb);
            }
        }

    });

    return v;
}
private void showAlertBox() {

    AlertDialog malertDialog = null;
    AlertDialog.Builder mdialogBuilder = null;
    if (mdialogBuilder == null) {
        mdialogBuilder = new AlertDialog.Builder(getActivity());

        mdialogBuilder.setTitle("Alert");
        mdialogBuilder.setMessage("No Network");

        mdialogBuilder.setPositiveButton("Enable",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        // launch setting Activity
  startActivityForResult(new Intent(                                          
  android.provider.Settings.ACTION_SETTINGS),0);
                    }
                });

        mdialogBuilder.setNegativeButton(android.R.string.no,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {

                    }
                }).setIcon(android.R.drawable.ic_dialog_alert);

        if (malertDialog == null) {
            malertDialog = mdialogBuilder.create();
            malertDialog.show();
        }

    }

 }

 protected void facebookSDKInitialize() {

    FacebookSdk.sdkInitialize(getActivity());

    callbackManager = CallbackManager.Factory.create();
}

protected void getLoginDetails(LoginButton login_button){

    // Callback registration
    login_button.registerCallback(callbackManager, 
    new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult login_result) {

            getUserInfo(login_result);
        }

        @Override
        public void onCancel() {
            // code for cancellation
        }

        @Override
        public void onError(FacebookException exception) {
            //  code to handle error
        }
    });
 }

 protected void getUserInfo(LoginResult login_result){

    GraphRequest data_request =GraphRequest.newMeRequest(
 login_result.getAccessToken(), new 
 GraphRequest.GraphJSONObjectCallback() {
                @Override
                public void onCompleted(
                        JSONObject json_object,
                        GraphResponse response) {
           Intent intent = new Intent(getContext(), fbActivity.class);
                    intent.putExtra("BitmapImage", mBitmap);
                    intent.putExtra("jsondata", json_object.toString());
                    startActivity(intent);
                }
            });
    Bundle permission_param = new Bundle();
 permission_param.putString("fields","id,
 name,email,picture.width(120).height(120)");
    data_request.setParameters(permission_param);
    data_request.executeAsync();
 }
 /*
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    callbackManager.onActivityResult(requestCode, resultCode, data);
    Log.e("data", data.toString());
 }*/
@Override
public void onResume() {
    super.onResume();

    // Logs 'install' and 'app activate' App Events.
    AppEventsLogger.activateApp(this.getActivity());
}

@Override
public void onPause() {
    super.onPause();

    // Logs 'app deactivate' App Event.
    AppEventsLogger.deactivateApp(this.getActivity());
}

}

我不知道我在哪里失踪,任何人都可以指导我这个。提前谢谢你们。

2 个答案:

答案 0 :(得分:1)

可能会调用Activity的onActivityResult()。所以让你的Fragment代码保持不变,只需从活动中调用Fragment的onActivityResult() -

在您的活动onActivityResult()中,获取对您的片段的引用 -

Fragment f = getFragmentManager.findFragmentById(R.id.container);

其中R.id.container是包含您的片段的Activity的布局。然后触发片段的onActivityResult() -

((sharelist) fragment).onActivityResult(requestCode, resultCode, data);

不要在Activity中使用CallbackManager,因为它仅在片段中定义。

答案 1 :(得分:0)

onSuccess()

中开始您的活动

试试这个:

rotected void getLoginDetails(LoginButton login_button){

 // Callback registration
    login_button.registerCallback(callbackManager, 
    new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult login_result) {

            getUserInfo(login_result);
              Intent intent = new Intent(getApplicationContext(), HomeActivity.class);
              startActivity(intent);
        }