FragmentTransaction.add():错误的片段类型

时间:2016-04-04 23:01:09

标签: java android android-fragments

我制作了一个片段通过Facebook连接,其代码在这里:

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.facebook.AccessToken;
import com.facebook.AccessTokenTracker;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.Profile;
import com.facebook.ProfileTracker;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;

public class LoginFragment extends Fragment
{
    private TextView mTextDetails;
    private CallbackManager fbCallbackManager;
    private AccessTokenTracker tTracker;
    private ProfileTracker pTracker;


    private FacebookCallback<LoginResult> fbCallback = new FacebookCallback<LoginResult>()
    {
        @Override
        public void onSuccess(LoginResult loginResult)
        {
            AccessToken accessToken = loginResult.getAccessToken();
            Profile profile = Profile.getCurrentProfile();
        }

        @Override
        public void onCancel()
        {

        }

        @Override
        public void onError(FacebookException error)
        {

        }
    };

    public LoginFragment()
    {

    }

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

        tTracker.startTracking();
        pTracker.startTracking();

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        return inflater.inflate(R.layout.fragment_main, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState)
    {
        super.onViewCreated(view, savedInstanceState);
        setTextDetails(view);
        setupFBLoginButton(view);
    }

    @Override
    public void onResume()
    {
        super.onResume();
        Profile profile = Profile.getCurrentProfile();
    }

    @Override
    public void onStop()
    {
        super.onStop();
        tTracker.stopTracking();
        pTracker.stopTracking();
    }

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



    private void setupFBLoginButton(View view)
    {
        LoginButton loginButton = (LoginButton) view.findViewById(R.id.login_button);
        loginButton.setReadPermissions("user_friends");
        loginButton.setFragment(this);
        loginButton.registerCallback(fbCallbackManager, fbCallback);
    }

    private void setTextDetails(View view)
    {
        mTextDetails = (TextView) view.findViewById(R.id.text_details);
    }

    private void setupTokenTracker()
    {
        tTracker= new AccessTokenTracker()
        {
            @Override
            protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken newAccessToken)
            {
                //TODO
            }
        };
    }

    private void setupProfileTracker()
    {
        pTracker = new ProfileTracker()
        {
            @Override
            protected void onCurrentProfileChanged(Profile oldProfile, Profile newProfile)
            {
                //TODO
            }
        };
    }


}

我正在努力&#34;打电话&#34;我的主要活动中的这个片段使用以下代码:

import android.app.FragmentManager;
import android.app.FragmentTransaction;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity
{

    private boolean logged = false;
    private LoginFragment loginFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        FragmentManager fManager = getFragmentManager();
        FragmentTransaction fTransaction = fManager.beginTransaction();

        LoginFragment loginFragment = new LoginFragment();

        fTransaction.add(R.id.fragment_container, loginFragment, "fragment_container");
        fTransaction.commit();

        setContentView(R.layout.activity_main);
    }


}

然而,我在这一行fTransaction.add(R.id.fragment_container, loginFragment, "fragment_container");的第二个参数上有错误。

我的IDE告诉我它期待android.app.Fragment个对象,而我的是my.package.LoginFragment

我没有得到它,LoginFragment延伸Fragment ......

我还尝试传递android.support.v4.*中的所有内容,但后来我无法loginButton.setFragment(this);,然后我不知道如何将片段设置为注册按钮。

1 个答案:

答案 0 :(得分:2)

在使用支持片段而不是getSupportFragmentManager()时使用getFragmentManager()

此外,在setContentView()之后添加片段应该可以找到fragment_container

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    loginFragment = new LoginFragment();
    getSupportFragmentManager()
            .beginTransaction()
            .add(R.id.fragment_container, loginFragment, "fragment_container")
            .commit();
}

完成此操作后,您还必须修复几个进口。请确保在MainActivity

中使用以下内容
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;