1

我有一个应用程序,在某个时候我有一个分享到 Facebook 的选项。我正在使用 Facebook SDK 4。

我想要这个片段做的只是登录到用户的 Facebook 并分享一张图片和一些文字。如果可能的话,我宁愿在没有片段的情况下这样做,但如果没有其他方法,我准备好忽略它。

片段:

public class Facebook extends Fragment {
private CallbackManager mCallBack;
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private String mParam1;
private String mParam2;

private OnFragmentInteractionListener mListener;


public static Facebook newInstance(String param1, String param2) {
    Facebook fragment = new Facebook();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
}

public Facebook() {

}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
    mCallBack = CallbackManager.Factory.create();
}

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

    return inflater.inflate(R.layout.fragment_facebook, container, false);

}


public void onButtonPressed(Uri uri) {
    if (mListener != null) {
        mListener.onFragmentInteraction(uri);
    }
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
        mListener = (OnFragmentInteractionListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement OnFragmentInteractionListener");
    }
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    LoginButton login = (LoginButton) view.findViewById(R.id.loginb);
    login.setReadPermissions("user_friends");
    login.setFragment(this);
    login.registerCallback(mCallBack,mCallBacke);

}

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}


public interface OnFragmentInteractionListener {
    // TODO: Update argument type and name
    public void onFragmentInteraction(Uri uri);
}
 private FacebookCallback<LoginResult> mCallBacke=new FacebookCallback<LoginResult>() {
     @Override
     public void onSuccess(LoginResult loginResult) {
         AccessToken accessToken = loginResult.getAccessToken();
         Profile profile = Profile.getCurrentProfile();
         if(profile!=null){
             Toast.makeText(getActivity().getApplicationContext(), "WElbon",
                     Toast.LENGTH_SHORT).show();
         }
         Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.redsmall);

         SharePhoto photo = new SharePhoto.Builder()
                 .setBitmap(image)
                 .build();

         SharePhotoContent content = new SharePhotoContent.Builder()
                 .addPhoto(photo)
                 .build();

         ShareApi.share(content, null);
         Toast.makeText(getActivity().getApplicationContext(),"Facebook Photo Upload Complated",Toast.LENGTH_LONG).show();
     }

     @Override
     public void onCancel() {

     }

     @Override
     public void onError(FacebookException e) {

     }
 };

我调用片段的 Activity 有 fragmentActivity 扩展(我读到这应该修复它,但它没有)

 CallbackManager callbackManager;
ShareDialog shareDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FacebookSdk.sdkInitialize(getApplicationContext());
    setContentView(R.layout.activity_unlock_braggin_rights);
    DisplayMetrics dm = new DisplayMetrics();
    int width = dm.widthPixels;

    int hight = dm.heightPixels;
    //getWindow().setLayout((int) (width * .8), (int) (hight * .5));
    Bundle so = getIntent().getExtras();
    String ulocked = so.getString("unlocked");
    TextView unlocked = (TextView)findViewById(R.id.unlocked);
    unlocked.setText(ulocked);
    unlocked.setText(ulocked);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_unlock_braggin_rights, menu);

    return true;
}



@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();

    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
public void buttonClicked(View view){
    Intent intent = new Intent(this,Store.class);
    startActivity(intent);
    finish();
}
@Override
public void onBackPressed() {

    Intent intent = new Intent(this,Store.class);
    startActivity(intent);
    finish();
}

错误代码:

j
    teException: Could not execute method of the activity
                at android.view.View$1.onClick(View.java:3969)
                at android.view.View.performClick(View.java:4637)
                at android.view.View$PerformClick.run(View.java:19422)
                at android.os.Handler.handleCallback(Handler.java:733)
                at android.os.Handler.dispatchMessage(Handler.java:95)
                at android.os.Looper.loop(Looper.java:136)
                at android.app.ActivityThread.main(ActivityThread.java:5586)
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:515)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
                at dalvik.system.NativeStart.main(Native Method)
        Unable to start activity ComponentInfo{com.inc.nicky.tapit/com.inc.nicky.tapit.UnlockBragginRights}: android.view.InflateException: Binary XML file line #58: Error inflating class fragment
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2394)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2452)
                at android.app.ActivityThread.access$900(ActivityThread.java:172)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1302)
                at android.os.Handler.dispatchMessage(Handler.java:102)
                at android.os.Looper.loop(Looper.java:136)
                at android.app.ActivityThread.main(ActivityThread.java:5586)
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:515)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
                at dalvik.system.NativeStart.main(Native Method)
         Caused by: android.view.InflateException: Binary XML file line #58: Error inflating class fragment
                at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:719)
                at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
                at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
                at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
                at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
                at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:350)
                at android.app.Activity.setContentView(Activity.java:2031)
                at com.inc.nicky.tapit.UnlockBragginRights.onCreate(UnlockBragginRights.java:41)
                at android.app.Activity.performCreate(Activity.java:5451)
                at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2358)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2452)
                at android.app.ActivityThread.access$900(ActivityThread.java:172)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1302)
                at android.os.Handler.dispatchMessage(Handler.java:102)
                at android.os.Looper.loop(Looper.java:136)
                at android.app.ActivityThread.main(ActivityThread.java:5586)
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:515)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
                at dalvik.system.NativeStart.main(Native Method)
         Caused by: java.lang.ClassCastException: com.inc.nicky.tapit.UnlockBragginRights@42c6c808 must implement OnFragmentInteractionListener
                at com.inc.nicky.tapit.Facebook.onAttach(Facebook.java:132)
                at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:907)
                at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1116)
                at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1218)
                at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2170)
                at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:300)
                at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)
                at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
                at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
                at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
                at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
                at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:350)
                at android.app.Activity.setContentView(Activity.java:2031)
                at com.inc.nicky.tapit.UnlockBragginRights.onCreate(UnlockBragginRights.java:41)
                at android.app.Activity.performCreate(Activity.java:5451)
                at  android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
                at   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2358)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2452)
                at android.app.ActivityThread.access$900(ActivityThread.java:172)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1302)
                at android.os.Handler.dispatchMessage(Handler.java:102)
                at android.os.Looper.loop(Looper.java:136)
                at android.app.ActivityThread.main(ActivityThread.java:5586)
                at java.lang.reflect.M
4

0 回答 0