Я использую Kotlin
и java together
в своем проекте.
Я создал Bridge
класс для использования kotlin
кодов в Java.
ниже мой java class
:
public class ChatFragment extends Fragment {
private Bridge bridge;
private Boolean isAttached = false;
private Boolean isVisible = false;
private AppCompatActivity appCompatActivity;
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser && isAttached) {
isVisible = true;
bridge = new Bridge();
bridge.initialInjection(ChatFragment.this);
bridge.loadCredentialsMethod(ChatFragment.this, appCompatActivity);
}
}
public ChatFragment(AppCompatActivity appCompatActivity) {
this.appCompatActivity = appCompatActivity;
}
@Override
public void onStop() {
super.onStop();
if (isVisible) {
bridge.stopMe();
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (isVisible) {
bridge.onActivityResultMe(requestCode, resultCode, data, ChatFragment.this);
}
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_chat, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
isAttached = true;
}
}
И я создал класс kotlin
для использования из kotlin's
кода, подобного приведенному ниже:
class Bridge : HasSupportFragmentInjector {
override fun supportFragmentInjector(): AndroidInjector<Fragment> {
return fragmentDispatchingAndroidInjector
}
@Inject
lateinit var fragmentDispatchingAndroidInjector: DispatchingAndroidInjector<Fragment>
@Inject
lateinit var presenter: AuthenticationPresenter
val job = Job()
fun initialInjection(fragment: Fragment) {
AndroidInjection.inject(fragment.activity)
}
fun loadCredentialsMethod(context: Fragment, appCompat: AppCompatActivity) {
val deepLinkInfo = context.activity!!.intent.getLoginDeepLinkInfo()
launch(UI + job) {
val newServer = context.activity!!.intent.getBooleanExtra(INTENT_ADD_NEW_SERVER, false)
// if we got authenticateWithDeepLink information, pass true to newServer also
presenter.loadCredentials(newServer || deepLinkInfo != null) { authenticated ->
if (!authenticated) {
showServerInput(deepLinkInfo, appCompat)
}
}
}
}
fun showServerInput(deepLinkInfo: LoginDeepLinkInfo?, appCompat: AppCompatActivity) {
toServerFragment(deepLinkInfo, appCompat)
}
fun stopMe() {
job.cancel()
}
fun onActivityResultMe(requestCode: Int, resultCode: Int, data: Intent?, context: Fragment) {
val currentFragment = context.activity!!.supportFragmentManager.findFragmentById(R.id.fragment_container)
currentFragment?.onActivityResult(requestCode, resultCode, data)
}
}
const val INTENT_ADD_NEW_SERVER = "INTENT_ADD_NEW_SERVER"
fun Context.newServerIntent(): Intent {
return Intent(this, ChatFragment::class.java).apply {
putExtra(INTENT_ADD_NEW_SERVER, true)
flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_CLEAR_TASK
}
}
fun toServerFragment(deepLinkInfo: LoginDeepLinkInfo?, appCompat: AppCompatActivity) {
appCompat.addFragmentBackStack("ServerFragment", R.id.fragment_container_chat_mou) {
ServerFragment.newInstance(deepLinkInfo)
}
}
Но когда я запускаю код, выдается ошибка:
lateinit property presenter has not been initialized
Ошибка относится к этой строке:
@Inject
lateinit var presenter: AuthenticationPresenter
Я использовал из этого LINK , но не работает.