2017-04-05 71 views
0

我讀了更多關於這個問題的鏈接,但我不知道我的項目到底是什麼問題。我設置dataBinding爲真上build.gradle,下面的代碼是我的應用程序build.gradle文件內容android包* .databinding不存在

apply plugin: 'com.android.application' 
apply plugin: 'com.neenbedankt.android-apt' 

android { 
    compileSdkVersion 25 
    buildToolsVersion "25.0.2" 
    defaultConfig { 
     applicationId "com.text.myapp" 
     minSdkVersion 15 
     targetSdkVersion 25 
     versionCode 1 
     versionName "1.0" 
    } 
    buildTypes { 
     release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 
    dataBinding { 
     enabled = true 
    } 
} 

repositories { 
    jcenter() 
    maven { url "https://jitpack.io" } 
    maven { url "https://clojars.org/repo/" } 
} 

def support_library = "25.2.0" 

dependencies { 
    compile fileTree(include: ['*.jar'], dir: 'libs') 
    compile "com.android.support:appcompat-v7:${support_library}" 
    compile "com.android.support:support-v13:${support_library}" 
    compile "com.android.support:cardview-v7:${support_library}" 
    compile "com.android.support:recyclerview-v7:${support_library}" 
} 

我的活動:

public class ActivityRegister extends AppCompatActivity { 

    private ActivityRegisterBinding binding; 

    @Override 
    public void onCreate(@Nullable Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     binding = DataBindingUtil.setContentView(this, R.layout.activity_register); 
    } 
} 

然後,活動佈局上的logcat

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

    <data> 
     <variable 
      name="presenter" 
      type="ir.pishguy.cafealachiqpro.Ui.Register.ActivityMain.Model.ActivityRegisterViewModel"/> 
    </data> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_margin="-5dp" 
     android:orientation="vertical"> 

     <TextView 
      android:id="@+id/default_port" 
      android:layout_width="match_parent" 
      android:visibility="gone" 
      android:layout_height="@dimen/default_textview_height"/> 
    </LinearLayout> 
</layout> 

錯誤:

/Users/mahdi/Desktop/Home/Projects/Android/CafeAlachiqPro/app/src/main/java/com/text/myapp/Ui/Register/ActivityMain/Presenter/ActivityRegister.java 
Error:(11, 45) error: package com.text.myapp.databinding does not exist 
Error:(19, 13) error: cannot find symbol class ActivityRegisterBinding 
Warning:The following options were not recognized by any processor: 
'[android.databinding.artifactType, android.databinding.printEncodedErrors, android.databinding.minApi, android.databinding.isTestVariant, android.databinding.enableDebugLogs, android.databinding.sdkDir, android.databinding.bindingBuildFolder, android.databinding.enableForTests, android.databinding.modulePackage, android.databinding.generationalFileOutDir, android.databinding.xmlOutDir]' 
Error:Execution failed for task ':app:compileDebugJavaWithJavac'. 
> Compilation failed; see the compiler error output for details. 

我的視圖模型:

public class ActivityRegisterViewModel extends BaseObservable { 
    private String readContactPermission; 
    private String getMessages; 

    public ActivityRegisterViewModel() { 
    } 

    @Bindable 
    public String getReadContactPermission() { 
     return readContactPermission; 
    } 

    public void setReadContactPermission(String readContactPermission) { 
     this.readContactPermission = readContactPermission; 
     notifyChange(); 
    } 

    public String getGetMessages() { 
     return getMessages; 
    } 

    public void setGetMessages(String getMessages) { 
     this.getMessages = getMessages; 
    } 
} 
+0

請發表您的錯誤 –

+0

@SurajMakhija公佈更新後的解決,謝謝 –

回答

0

請註明變量標籤定義您的視圖模型如下:

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

     <data> 

      <variable 
       name="<variablename>" 
       type="<packagename.YourViewModel>" /> 
     </data> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_margin="-5dp" 
      android:orientation="vertical"> 

      <TextView 
       android:id="@+id/default_port" 
       android:layout_width="match_parent" 
       android:visibility="gone" 
       android:layout_height="@dimen/default_textview_height"/> 
     </LinearLayout> 
    </layout> 
+0

此解決方案無法解決我的問題先生 –

+0

你是否得到相同的錯誤? –

+0

您是否創建了您的視圖模型,並且能夠在xml中定義它? –

1

刪除

應用插件: 'com.neenbedankt.android-容易'

and

apt「com.github.Raizlabs.DBFlow:dbflow-processor:$ {dbflow_version}」

from gradle。這有助於我擺脫這個錯誤。

雖然我可以運行你的項目的原因ir.pishguy.cafealachiqpro.Ui.Register.Robot.Model.RobotViewModel不能從robot_user_action.xml

<data class="UserMessagesDataBinding"> 

     <variable 
      name="viewModel" 
      type="ir.pishguy.cafealachiqpro.Ui.Register.Robot.Model.RobotViewModel"> 
     </variable> 
    </data> 
+0

哦,在創建新的項目我沒有這個包,你可以找到它的先生?我的意思是'type =「ir.pishguy.cafealachiqpro.Ui.Register.Robot.Model.RobotViewModel」' –

+0

我把佈局內容粘貼到我自己的文章中,點擊這一行的佈局文件'binding = DataBindingUtil.setContentView(this, R.layout.activity_register);'可以在沒有任何問題的情況下通過android studio打開,並在這行中點擊'ActivityRegisterViewModel'' type =「ir.pishguy.cafealachiqpro.Ui.Register.ActivityMain.Model.ActivityRegisterViewModel」/>'can太開放 –

+0

您是否從Gradle中刪除了這兩行後嘗試運行您的項目? – CodeCameo

相關問題