gpt4 book ai didi

android - 无法让绑定(bind)自动生成的类工作

转载 作者:太空狗 更新时间:2023-10-29 13:50:17 29 4
gpt4 key购买 nike

所以我开始了一个新项目,主要是从 https://github.com/MindorksOpenSource/android-mvvm-architecture 复制架构

我想我几乎都在编译,但由于某种原因,我的 LoginActivityBinding 类在编译时找不到,即使 IDE 似乎找到了它,它也没有显示红色文本,我可以执行 control+b,它会将我带到布局文件。

这是我的课:

public class LoginActivity extends BaseActivity<LoginActivityBinding, LoginViewModel> implements LoginNavigator {

@Inject
LoginViewModel loginViewModel;

LoginActivityBinding loginActivityBinding;

public static Intent getStartIntent(Context context) {
Intent intent = new Intent(context, LoginActivity.class);
return intent;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
loginActivityBinding = getViewDataBinding();
loginViewModel.setNavigator(this);

}

@Override
protected void onDestroy() {
super.onDestroy();
}

@Override
public void openMainActivity() {
/*
Intent intent = MainActivity.getStartIntent(LoginActivity.this);
startActivity(intent);
finish();*/
}

@Override
public void handleError(Throwable throwable) {
// handle error
}

@Override
public void login() {
/* String email = activityLoginBinding.etEmail.getText().toString();
String password = activityLoginBinding.etPassword.getText().toString();
if (loginViewModel.isEmailAndPasswordValid(email, password)) {
hideKeyboard();
loginViewModel.login(email, password);
} else {
Toast.makeText(this, getString(R.string.invalid_email_password), Toast.LENGTH_SHORT).show();
}*/
}

@Override
public LoginViewModel getViewModel() {
return loginViewModel;
}

@Override
public int getBindingVariable() {
return BR.viewModel;
}

@Override
public int getLayoutId() {
return R.layout.login_activity;
}

}

这是它扩展的 BaseActivity:

public abstract class BaseActivity<T extends ViewDataBinding, V extends BaseViewModel> extends AppCompatActivity implements BaseFragment.Callback {

// TODO
// this can probably depend on isLoading variable of BaseViewModel,
// since its going to be common for all the activities
private ProgressDialog progressDialog;

private T viewDataBinding;
private V viewModel;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
performDependencyInjection();
super.onCreate(savedInstanceState);
performDataBinding();
}

private void performDataBinding() {
viewDataBinding = DataBindingUtil.setContentView(this, getLayoutId());
this.viewModel = viewModel == null ? getViewModel() : viewModel;
viewDataBinding.setVariable(getBindingVariable(), viewModel);
viewDataBinding.executePendingBindings();
}



@TargetApi(Build.VERSION_CODES.M)
public void requestPermissionsSafely(String[] permissions, int requestCode) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(permissions, requestCode);
}
}

@TargetApi(Build.VERSION_CODES.M)
public boolean hasPermission(String permission) {
return Build.VERSION.SDK_INT < Build.VERSION_CODES.M ||
checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED;
}

@Override
public void onFragmentAttached() {

}

@Override
public void onFragmentDetached(String tag) {

}

public void hideKeyboard() {
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}

public void openActivityOnTokenExpire() {
startActivity(LoginActivity.getStartIntent(this));
finish();
}

public boolean isNetworkConnected() {
return NetworkUtils.isNetworkConnected(getApplicationContext());
}

public void showLoading() {
hideLoading();
progressDialog = CommonUtils.showLoadingDialog(this);
}

public void hideLoading() {
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.cancel();
}
}

public T getViewDataBinding() {
return viewDataBinding;
}

/**
* Override for set view model
*
* @return view model instance
*/
public abstract V getViewModel();

/**
* Override for set binding variable
*
* @return variable id
*/
public abstract int getBindingVariable();

/**
* @return layout resource id
*/
public abstract
@LayoutRes
int getLayoutId();

public void performDependencyInjection() {
AndroidInjection.inject(this);
}

}

这是login_activity.xml:

<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="mypackage.LoginActivity">
<data>
<import type="android.view.View"/>
<variable
name="viewModel"
type="mypackage.LoginViewModel"/>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
>
<!-- Login progress -->
<ProgressBar
android:id="@+id/login_progress"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:visibility="gone"/>
<ScrollView
android:id="@+id/login_form"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/email_login_form"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<AutoCompleteTextView
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/prompt_email"
android:inputType="textEmailAddress"
android:maxLines="1"
android:singleLine="true"/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/prompt_password"
android:imeActionId="6"
android:imeActionLabel="@string/action_sign_in_short"
android:imeOptions="actionUnspecified"
android:inputType="textPassword"
android:maxLines="1"
android:singleLine="true"/>
</android.support.design.widget.TextInputLayout>
<Button
android:id="@+id/email_sign_in_button"
style="?android:textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="@string/action_sign_in"
android:onClick="@{() -> viewModel.onServerLoginClick()}"

android:textStyle="bold"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
</layout>

这是我的 gradle 文件:

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'


apply plugin: 'kotlin-android-extensions'

android {
compileSdkVersion rootProject.ext.compileSdkVersion

defaultConfig {
applicationId "mypackage"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
dataBinding {
enabled = true
}
}
kapt {
generateStubs = true
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation "com.android.support:appcompat-v7:$supportLibraryVersion"
implementation "com.android.support:design:$supportLibraryVersion"
implementation "com.android.support:recyclerview-v7:$rootProject.supportLibraryVersion"
implementation "com.android.support:cardview-v7:$rootProject.supportLibraryVersion"

//firebase and gps
implementation "com.google.firebase:firebase-core:$googlePlayServicesVersion"

//Viewmodel and LiveData
implementation "android.arch.lifecycle:extensions:$rootProject.lifecycle"
annotationProcessor "android.arch.lifecycle:compiler:$rootProject.lifecycle"

// database
implementation "android.arch.persistence.room:rxjava2:$rootProject.roomDatabase"
annotationProcessor "android.arch.persistence.room:compiler:$rootProject.roomDatabase"

// parser
implementation "com.google.code.gson:gson:$rootProject.gsonVersion"

// dependency injection
implementation "com.google.dagger:dagger:$rootProject.dagger2Version"
annotationProcessor "com.google.dagger:dagger-compiler:$rootProject.dagger2Version"
annotationProcessor "com.google.dagger:dagger-android-processor:$rootProject.dagger2Version"
implementation "com.google.dagger:dagger-android-support:$rootProject.dagger2Version"
kapt "com.google.dagger:dagger-compiler:$rootProject.dagger2Version"
kapt "com.google.dagger:dagger-android-processor:$rootProject.dagger2Version"

// reactive
implementation "io.reactivex.rxjava2:rxjava:$rootProject.rxjava2Version"
implementation "io.reactivex.rxjava2:rxandroid:$rootProject.rxandroidVersion"


//databinding
annotationProcessor 'com.android.databinding:compiler:3.0.1'


testImplementation "junit:junit:$junitVersion"
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation "com.android.support.test.espresso:espresso-core:$espressoVersion"
}
apply plugin: 'com.google.gms.google-services'

我错过了什么?

谢谢。

最佳答案

因为这是一个 Kotlin 项目,但我刚刚复制了一些 Java 文件,所以我决定用 Kotlin 搜索这个问题,结果发现我需要的是这一行 kapt "com.android.databinding: compiler:2.3.3" 在我的依赖项上。

关于android - 无法让绑定(bind)自动生成的类工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48819171/

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