2017-11-18 1 views
1

Ich versuche, die Android-Bibliothek "Data Binding Validator by Ilhasoft" mit Kotlin zu verwenden, aber ich bekomme diesen Fehler: Nicht aufgelöste Referenz: validate.Fehler im Datenbindungs-Validator von Ilhasoft + Kotlin

, dass meine Wurzel build.gradle ist:

buildscript { 
    ext.kotlin_version = '1.1.60' 
    ext.android_plugin_version = '3.0.0' 
    ext.data_binding_validator_verson = '1.0.0' 
    repositories { 
     jcenter() 
     google() 
    } 
    dependencies { 
     classpath 'com.android.tools.build:gradle:3.0.0' 
     classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 

    } 
} 

allprojects { 
    repositories { 
     jcenter() 
     maven { url 'https://jitpack.io' } 
    } 
} 

task clean(type: Delete) { 
    delete rootProject.buildDir 
} 

Das ist meine APP build.gradle ist:

<?xml version="1.0" encoding="utf-8"?> 
<layout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto"> 

    <data> 
     <variable 
      name="user" 
      type="com.main.janderson.meuapp.model.User" /> 
    </data> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     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"> 

     <ProgressBar 
      android:id="@+id/cadastrar_user_progress" 
      style="?android:attr/progressBarStyleLarge" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:layout_marginBottom="@dimen/activity_vertical_margin" 
      android:visibility="gone" /> 

     <ScrollView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

      <LinearLayout 
       android:id="@+id/cadastrar_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"> 

        <EditText 
         android:id="@+id/first_name_user" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:hint="@string/first_name" 
         android:inputType="textCapSentences" 
         android:lines="1" 
         android:maxLength="30" 
         android:maxLines="1" 
         android:text="@{user.firstName}" 
         app:validateEmpty="@{false}" /> 

       </android.support.design.widget.TextInputLayout> 

       <android.support.design.widget.TextInputLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"> 

        <EditText 
         android:id="@+id/last_name_user" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:hint="@string/last_name" 
         android:inputType="textCapSentences" 
         android:lines="1" 
         android:maxLength="30" 
         android:maxLines="1" 
         android:text="@{user.lastName}" 
         app:validateEmpty="@{false}" /> 

       </android.support.design.widget.TextInputLayout> 

       <android.support.design.widget.TextInputLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"> 

        <EditText 
         android:id="@+id/user_email" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:hint="@string/prompt_email" 
         android:inputType="textEmailAddress" 
         android:lines="1" 
         android:maxLength="100" 
         android:maxLines="1" 
         android:text="@{user.email}" 
         app:validateType='@{"email"}' /> 

       </android.support.design.widget.TextInputLayout> 

       <android.support.design.widget.TextInputLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"> 

        <EditText 
         android:id="@+id/user_phone" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:hint="@string/phone" 
         android:inputType="phone" 
         android:lines="1" 
         android:maxLength="20" 
         android:maxLines="1" 
         android:text="@{user.phone}" /> 

       </android.support.design.widget.TextInputLayout> 

       <android.support.design.widget.TextInputLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"> 

        <EditText 
         android:id="@+id/user_pass" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:hint="@string/prompt_password" 
         android:inputType="textPassword" 
         android:lines="1" 
         android:maxLength="20" 
         android:maxLines="1" 
         android:text="@{user.password}" 
         app:validateMinLength="@{4}" /> 

       </android.support.design.widget.TextInputLayout> 

       <android.support.design.widget.TextInputLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"> 

        <EditText 
         android:id="@+id/user_confir_pass" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:hint="@string/prompt_repeat_password" 
         android:imeActionLabel="@string/cadastrar" 
         android:imeOptions="actionUnspecified" 
         android:inputType="textPassword" 
         android:lines="1" 
         android:maxLength="20" 
         android:maxLines="1" /> 

       </android.support.design.widget.TextInputLayout> 

       <Button 
        android:id="@+id/new_user_button" 
        style="?android:textAppearanceSmall" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_marginTop="16dp" 
        android:text="@string/cadastrar" 
        android:textStyle="bold" /> 
      </LinearLayout> 
     </ScrollView> 
    </LinearLayout> 
</layout> 

Und zum Schluss, dass:

apply plugin: 'com.android.application' 
apply plugin: 'kotlin-android' 
apply plugin: 'kotlin-android-extensions' 
apply plugin: 'kotlin-kapt' 

android { 
    compileSdkVersion 27 
    buildToolsVersion "27.0.0" 
    defaultConfig { 
     applicationId "com.main.janderson.meuapp" 
     minSdkVersion 16 
     targetSdkVersion 27 
     versionCode 1 
     versionName "1.0" 
     testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 

     vectorDrawables.useSupportLibrary = true 

     multiDexEnabled true 

    } 
    buildTypes { 
     release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 

    dataBinding { 
     enabled = true 
    } 
} 

dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 
     exclude group: 'com.android.support', module: 'support-annotations' 
    }) 

    compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" 
    kapt "com.android.databinding:compiler:$android_plugin_version" 
    compile "com.github.Ilhasoft:data-binding-validator:$data_binding_validator_verson" 
} 
repositories { 
    mavenCentral() 
    google() 
} 

kapt { 
    generateStubs = true 
} 

Das ist mein fragment_cadastrar_user ist ist mein onCreateView in meinem Fragment:

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { 

    val view = inflater.inflate(R.layout.fragment_cadastrar_user, null) 

    //Binding Api 
    val binding: FragmentCadastrarUserBinding = DataBindingUtil.setContentView(activity as Activity, R.layout.fragment_cadastrar_user) 
    val validator = Validator(binding); 

    binding.validate.setOnClickListener(View.OnClickListener { 
     if (validator.validate()) { 
      registerUser() 
     } 
    }) 

    binding.setUser(user); 

    view.user_phone.addTextChangedListener(PhoneNumberFormattingTextWatcher()) 

    return view 
} 

And to complement this is a picture with my error.

Könnte mir jemand dabei helfen. Danke!

Antwort

0

validate ist die ID der Schaltfläche im Beispiel der Bibliothek.

Sie sollten die ID der Schaltfläche verwenden, die Sie validieren möchten. Ich denke es ist new_user_button.

Also mit Bindung sollte es wie newUserButton sein.

So Ihr volles Beispiel wird wie:

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { 

    val view = inflater.inflate(R.layout.fragment_cadastrar_user, null) 

    //Binding Api 
    val binding: FragmentCadastrarUserBinding = DataBindingUtil.setContentView(activity as Activity, R.layout.fragment_cadastrar_user) 
    val validator = Validator(binding); 

    // CHANGE IN BELOW LINE 
    binding.newUserButton.setOnClickListener(View.OnClickListener { 
     if (validator.validate()) { 
      registerUser() 
     } 
    }) 

    binding.setUser(user); 

    view.user_phone.addTextChangedListener(PhoneNumberFormattingTextWatcher()) 

    return view 
} 
+0

Ist es funktioniert ?? – kirtan403

+0

Es funktioniert !!! Vielen Dank! –

Verwandte Themen