gpt4 book ai didi

java - FragmentTransaction.add() : wrong Fragment type

转载 作者:行者123 更新时间:2023-12-01 10:06:33 24 4
gpt4 key购买 nike

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

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
}
};
}


}

我正在尝试使用以下代码从我的主要 Activity 中“调用”此 fragment :

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);,然后我不知道如何将 fragment 设置为登录按钮。

最佳答案

使用支持 fragment 时使用 getSupportFragmentManager() 而不是 getFragmentManager()

此外,添加 fragment 应该在 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;

关于java - FragmentTransaction.add() : wrong Fragment type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36414388/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com