2017-09-22 13 views
0

これは初めてのデータバインディングを使用しており、カップルからの上記のエラーが発生しています。同じ問題については十分な質問がありますが、この問題を解決することはできません。エラー:(7,36)エラー:パッケージexample.com.test.databindingが存在しません

のGradleモジュールAPP

apply plugin: 'com.android.application' 

android { 
    compileSdkVersion 26 
    buildToolsVersion "26.0.1" 
    defaultConfig { 
     applicationId "example.com.test" 
     minSdkVersion 15 
     targetSdkVersion 26 
     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 
    } 

} 

dependencies { 
    implementation fileTree(dir: 'libs', include: ['*.jar']) 
    implementation 'com.android.support:appcompat-v7:26.1.0' 
    implementation 'com.android.support.constraint:constraint-layout:1.0.2' 
    testImplementation 'junit:junit:4.12' 
    androidTestImplementation 'com.android.support.test:runner:1.0.1' 
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' 
} 

Main_xml

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

    <data> 

     <variable 
      name="user" 
      type="example.com.test.MainActivity" /> 

     <import type="android.view.View" /> 

    </data> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <TextView 
      android:id="@+id/firstName" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@{user.firstName}" /> 

     <TextView 
      android:id="@+id/lastName" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@{user.lastName}" /> 
    </LinearLayout> 
</layout> 

モーダルクラス

public class User { 

    public final String firstName; 
    public final String lastName; 

    public User(String firstName, String lastName) { 
     this.firstName = firstName; 
     this.lastName = lastName; 
    } 

    public String getFirstName() { 
     return this.firstName; 
    } 

    public String getLastName() { 
     return this.lastName; 
    } 


} 

主な活動

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     String sMan = "Test"; 


     ActivityMainBinding activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main); 
     User mainActivityPresenter = new User(sMan,sMan); 
     activityMainBinding.setUser(mainActivityPresenter); 



    } 
} 

画像は、私は、この問題を解決することができ、このissue.Howを解決することはできませんよログ

enter image description here

を追加する??

+0

にMainActivityを変更して解決 あなたは[ファイル]を試してみました>入賞/再起動を無効に...? –

+0

sure.many時間ですが、エラーは解決しません。 – seon

答えて

0

これはシンプルで愚かなエラーです。ユーザー

<data> 

    <variable 
     name="user" 
     type="example.com.test.User" /> 

    <import type="android.view.View" /> 

</data> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <TextView 
     android:id="@+id/firstName" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@{user.firstName}" /> 

    <TextView 
     android:id="@+id/lastName" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@{user.lastName}" /> 
</LinearLayout> 

1

実装にはさまざまなものがあります。

  1. お客様のモデルクラスはBaseObservableです。
  2. あなたのゲッターは@Bindable
  3. あなたのセッターは、あなたのXMLでnotifyPropertyChanged(..)
  4. を呼び出す必要がありますする必要があり、私はそれがUserだと思いながら、「ユーザー」は、example.com.test.MainActivityであることを言っていますか?
  5. User mainActivityPresenter = new User(sMan,sMan);も非常に奇妙です。 User「user」とPresenter「xxxPresenter」と呼んでください。明確なコードを記述したい場合は、ユーザー「プレゼンター」に電話しないでください。

...これらは私が一目で見るものですが、おそらくもっとあります。最初にドキュメントを完全に読んでください。それはあなたの実装をもっと簡単にします: https://developer.android.com/topic/libraries/data-binding/index.html

+0

okメイト確認します – seon

関連する問題