我在项目中使用Kotlin
和Java together
。
我创建了一个Bridge
类,以便在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
类,以便从类似下面的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,但没有用。
答案 0 :(得分:0)
您的Bridge类是不可注入的,您可以将其添加到图形中并使其易于注入,将public function user() {
$crud = new grocery_CRUD();
$crud->set_table('cms_user');
$crud->set_subject('User List');
$crud->required_fields('user_name');
$crud->columns('user_name','email','real_name','active');
$crud->change_field_type('active', 'true_false');
$crud->callback_delete(array($this,'delete_user'));
$output = $crud->render();
$this->_example_output($output);
}
public function delete_user($primary_key) {
return $this->db->update(
'cms_user',
array('deleted' => '1'),
array('id' => $primary_key)
);
}
添加到其构造函数中
@Inject