Я следил за документацией по API Facebook. О фрагменте было мало описания.

Как решить эту ошибку ...?

Я пробовал во фрагменте, [btnFacebook.setFragment (this)]

В Activity, [ProfileFragment (). onActivityResult (requestCode, resultCode, data)]

class ProfileFragment : BaseFragment() {
    private lateinit var profileViewModel: ProfileViewModel
    val RC_SIGN_IN = 9001
    val TAG = "@@@ ProfileF"
    lateinit var callbackManager: CallbackManager
    lateinit var firebaseAuth: FirebaseAuth
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        profileViewModel = ViewModelProviders.of(this).get(ProfileViewModel::class.java)
        val root = inflater.inflate(R.layout.fragment_profile, container, false)
        firebaseAuth = FirebaseAuth.getInstance()
        callbackManager = CallbackManager.Factory.create()
        var btnFacebook = root.findViewById<com.facebook.login.widget.LoginButton>(R.id.btn_profile_facebook)
        btnFacebook.setFragment(this)
        initFacebook(btnFacebook)
        return root
    }
    private fun initFacebook(btnFacebook: LoginButton) {
        btnFacebook.setPermissions("email", "public_profile")
        btnFacebook.registerCallback(callbackManager, object : FacebookCallback<LoginResult> {
            override fun onSuccess(result: LoginResult?) {
                Log.d(TAG, "facebook:onSuccess: $result")
                handleFacebookAccessToken(result!!.accessToken)
            }
            override fun onCancel() {
                Log.d(TAG, "facebook:onCancel...")
            }
            override fun onError(error: FacebookException?) {
                Log.d(TAG, "facebook:onError...${error.toString()}")
            }
        })
    }
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        callbackManager.onActivityResult(requestCode, resultCode, data)
    }
    private fun handleFacebookAccessToken(token: AccessToken) {
        Log.d(TAG, "handleFacebookAccessToken: $token")
        val credential = FacebookAuthProvider.getCredential(token.getToken());
        firebaseAuth.signInWithCredential(credential)
            .addOnCompleteListener(activity as MainActivity, object : OnCompleteListener<AuthResult> {
                override fun onComplete(task: Task<AuthResult>) {
                    if (task.isSuccessful()) {
                        // 로그인 성공
                        Log.d(TAG, "Facebook Login Success!!!")
                    } else {
                        // 로그인 실패
                        Log.d(TAG, "Facebook Login Fail...")
                    }
                }
            })
    }
}
class MainActivity : BaseActivity() {
    val TAG = "@@@ Main"
    val mContext = this
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val navView: BottomNavigationView = findViewById(R.id.nav_view)
        val navController = findNavController(R.id.nav_host_fragment)
        navView.setupWithNavController(navController)
    }
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        Log.d(TAG, "onActivityResult!!! ${requestCode} / ${resultCode} ")
        ProfileFragment().onActivityResult(requestCode, resultCode, data)
    }
}

Вызвано: kotlin.UninitializedPropertyAccessException: свойство lateinit callbackManager не было инициализировано

0
Yoonho Lee 4 Окт 2019 в 07:41

1 ответ

Лучший ответ

В методе onActivityResult вашего основного действия вы отправляете результат во вновь созданный ProfileFragment(), а не в уже прикрепленный фрагмент.

Так что сделайте ниже модификацию в методе MainActivity onActivityResult.

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
   val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment)
    val currFragment = navHostFragment?.childFragmentManager?.fragments?.get(0)
    if (currFragment is HomeFragment) {
        currFragment.onActivityResult(requestCode, resultCode, data)
    }
   super.onActivityResult(requestCode, resultCode, data)
}
2
DHAVAL ASODARIYA 4 Окт 2019 в 04:57