所以我开始了一个新项目,主要是从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'
我错过了什么?
感谢。
答案 0 :(得分:1)
由于这是一个Kotlin项目,但我刚刚复制了一些Java文件,我决定用Kotlin搜索这个问题,结果我需要的是我的依赖项上的这一行kapt "com.android.databinding:compiler:2.3.3"
。