• 카테고리

    질문 & 답변
  • 세부 분야

    모바일 앱 개발

  • 해결 여부

    해결됨

DataBinding시 오류 질문

22.03.27 18:03 작성 조회수 224

0

안녕하세요 선생님 도저히 오류를 고치지 못하겠어서 문의드립니다. DataBinding만 하면 오류가 나네요. 

구글링을 한참 해봤는데 xml파일의 오류도 아닌 것 같고, databinding 안 쓰고, <layout> 태그 안 쓰면 정상적으로 앱은 실행됩니다.. 한 번만 살펴봐주시면 감사하겠습니다. 

그냥 제 컴퓨터에서는 DataBinding을 포기하고 findViewById 만 사용해서 수업을 따라가도 될까요..?

위치 : 주사위앱 마지막 강의 3:30 

 

// MainActivitiy.kt
package com.preferkim.test01

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import androidx.databinding.DataBindingUtil
import com.preferkim.test01.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

    private lateinit var binding : ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        binding = DataBindingUtil.setContentView(this, R.layout.activity_main)

        binding.diceStartButton.setOnClickListener {
            Toast.makeText(this, "주사위 GO!", Toast.LENGTH_LONG).show()
        }
    }
}

// activity_main.xml
<?xml version="1.0" encoding="utf-8"?>

<layout>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#008000"
        android:orientation="vertical"
        tools:context=".MainActivity">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_marginTop="100dp">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="20dp"
                android:gravity="center"
                android:text="인생은 주사위 한 방에 가는 거 아니겠습니까?"
                android:textColor="@color/black"
                android:textSize="30sp"
                android:textStyle="bold" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:weightSum="2">


            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:src="@drawable/dice_1" />

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:src="@drawable/dice_2" />

        </LinearLayout>

        <android.widget.Button
            android:id="@+id/diceStartButton"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_margin="50dp"
            android:background="@color/black"
            android:gravity="center"
            android:text="인생 고고"
            android:textColor="@color/white" />

    </LinearLayout>

</layout>

// build.gradle(app)
plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-kapt'

}

android {
    compileSdkVersion 31
    buildToolsVersion "30.0.0"

    defaultConfig {
        applicationId "com.preferkim.test01"
        minSdkVersion 16
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }

    // 추가
    buildFeatures {
        dataBinding true
    }
//    dataBinding {
//        enabled true
//    }
}

dependencies {

    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.4.1'
    implementation 'com.google.android.material:material:1.5.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    kapt 'com.android.databinding:compiler:4.1.3'

}

// build.gradle(project)
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    ext.kotlin_version = "1.4.32"
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.1.3"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

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


답변 1

답변을 작성해보세요.

0

ohmyzava님의 프로필

ohmyzava

질문자

2022.03.27

와 오늘 6시간 만에 databinding 에러 해결했습니다.. 

일단 dependencies 에서 아래처럼 바꾸니까 실행은 됩니다;; 

    implementation 'com.google.android.material:material:1.5.0-alpha02'

 

앗 방금 질문을 확인하고 돌려보려고 하니 이미 찾으셨군요

다행입니다. :) 
추가 질문 있으시면 또 문의주세요~