Dagger - lateinit 属性 presenter 尚未初始化 android
Dagger - lateinit property presenter has not been initialized android
我在我的项目中使用 Kotlin
和 java together
。
我创建了一个 Bridge
class 用于 java 中的 kotlin
代码。
下面是我的 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
class 供 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
我用过这个 但没用。
您的 Bridge class 不可注入,您可以将其添加到图形中并使其可注入,轻松地将 @Inject
添加到其构造函数
class Bridge @Inject constructor(): HasSupportFragmentInjector
我在我的项目中使用 Kotlin
和 java together
。
我创建了一个 Bridge
class 用于 java 中的 kotlin
代码。
下面是我的 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
class 供 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
我用过这个
您的 Bridge class 不可注入,您可以将其添加到图形中并使其可注入,轻松地将 @Inject
添加到其构造函数
class Bridge @Inject constructor(): HasSupportFragmentInjector