I have a layout file called activity_suggestions. I am using databinding in it. Hence the file ActivitySuggestionsBinding got generated. The project compiles successfully. But when I try to run the project, I get this error
e: error: cannot access ActivitySuggestionsBinding
I am using android studio 3.1.2 with kotlin version 1.4.1. Any help will be appreciated
Edit
Pasting my module level build.gradle and app level build.gradle
Module Build.gradle
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
android {
dataBinding {
enabled = true
}
..
}
dependencies{
..
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation "com.google.dagger:dagger:$rootProject.daggerVersion"
implementation "com.google.dagger:dagger-android:$rootProject.daggerVersion"
implementation "com.google.dagger:dagger-android-support:$rootProject.daggerVersion"
kapt "com.google.dagger:dagger-android-processor:$rootProject.daggerVersion"
kapt "com.google.dagger:dagger-compiler:$rootProject.daggerVersion"
provided 'javax.annotation:jsr250-api:1.0'
implementation "android.arch.lifecycle:runtime:$rootProject.archVersion"
implementation "android.arch.lifecycle:extensions:$rootProject.archVersion"
annotationProcessor "android.arch.lifecycle:compiler:$rootProject.archVersion"
kapt "com.android.databinding:compiler:3.1.2"
..
}
App build.gradle
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
android{
dataBinding{
enabled = true
}
..
}
dependencies{
compile project(':module')
kapt "com.google.dagger:dagger-android-processor:$rootProject.daggerVersion"
kapt "com.google.dagger:dagger-compiler:$rootProject.daggerVersion"
kapt "com.android.databinding:compiler:3.1.2"
..
}
This is the activity where I am accessing ActivitySuggestionsBinding. This compiles without any error.
class SuggestionsActivityScreen : BaseActivity() {
var binding : ActivitySuggestionsBinding? = null
override fun onCreate(savedInstanceState: Bundle?) {
AndroidInjection.inject(this)
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this,R.layout.activity_suggestions)
binding?.model = SuggestionActivityViewModel()
}
}
On compiling the base module (app), this is the error I get
error: cannot access ActivitySuggestionsBinding
class file for com.dom.comp.databinding.ActivitySuggestionsBinding not found
Consult the following stack trace for details.
com.sun.tools.javac.code.Symbol$CompletionFailure: class file for com.dom.comp.databinding.ActivitySuggestionsBinding not found
This is my activity_suggestions.xml
<?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"
xmlns:card_view="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="model"
type="com.dom.domp.SuggestionActivityViewModel"/>
</data>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:focusableInTouchMode="true"
android:padding="#dimen/step1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="#{model.namedString}"/>
</RelativeLayout>
</layout>
And I have tried clean, invalidate cache. These don't solve the problem.
In this demo it is work and try this way..
add only databinding enable code into app level gradle file like below code..
dataBinding {
enabled = true
}
below code is my app level gradle file ..
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
android {
compileSdkVersion 27
defaultConfig {
applicationId "com.example.adruser.rafdemo"
minSdkVersion 23
targetSdkVersion 27
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:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
implementation 'com.android.support:support-v4:27.1.1'
implementation 'com.android.support:design:27.1.1'
testImplementation 'junit:junit:4.12'
implementation 'com.android.support:cardview-v7:27.1.1'
implementation 'com.google.android.gms:play-services-maps:11.6.0'
implementation 'com.google.android.gms:play-services-location:11.6.0'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.squareup.okhttp3:logging-interceptor:3.4.1'
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
implementation 'com.intuit.sdp:sdp-android:1.0.4'
implementation 'com.github.bumptech.glide:glide:4.7.1'
}
repositories {
mavenCentral()
}
then after in project level gradle.properties file add below line..
android.databinding.enableV2=true
make user_layout.xml file like below code..
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="user"
type="com.example.adruser.rafdemo.model.User"/>
</data>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text='#{user.name}'/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:id="#+id/ulTvName"
android:text="#{user.dob}"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:id="#+id/ulTvAge"
android:text="#{Integer.toString(user.age)}"
app:layout_constraintTop_toBottomOf="#+id/ulTvName"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:id="#+id/ulTvDob"
android:text="#{user.dob ?? user.expDob}"
app:layout_constraintTop_toBottomOf="#+id/ulTvAge"
/>
</android.support.constraint.ConstraintLayout>
</layout>
then after make User.java pojo class like below code.. you can make .kt class pojo also
public class User {
public String name,dob,expDob;
public int age;
public User(){}
public User(String name, String dob,String expDob, int age) {
this.name = name;
this.dob = dob;
this.age = age;
this.expDob=expDob;
}
public static String display(){
return "rajesh";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDob() {
return dob;
}
public void setDob(String dob) {
this.dob = dob;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
then finnaly make DatabindingActivity.kt class for binding ..
class DatabindingActivity :AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
var binding:UserLayoutBinding=DataBindingUtil.setContentView(this,R.layout.user_layout)
val user = User("karan", null, "25/06/1994", 24)
binding.user=user
}
}
Add following in you app build.gradle
kapt "com.android.databinding:compiler:$android_plugin_version"
apply plugin: 'kotlin-kapt' // This one at top where plugin belong to
This will do the trick.
$android_plugin_version is version of com.android.tools.build:gradle in application build.gradle
Also, add this to your module build.gradle
android {
/// Existing Code
kapt {
generateStubs = true
}
}
I had similar issue.
I was getting error
error: cannot access RepeatPaymentFragmentBinding
class file for RepeatPaymentFragmentBinding not found
Consult the following stack trace for details.
com.sun.tools.javac.code.Symbol$CompletionFailure: class file for my.package.databinding.RepeatPaymentFragmentBinding not found
Spent a couple of days to figure out that the problem is not with GDB but with Dagger2. It turned out, Dagger doesn't support using generated classes in code it generates.
So, I had such class in my :library module:
class RepeatPaymentFragment: BaseFragment<RepeatPaymentFragmentBinding>() {
...
}
And the dagger component for it were located in :app module. So the two annotation processors interfere each other.
After I removed generic from my fragments it compiles fine.
So if you have such error, try to look if you try to inject into (or provide) class which is generated by another annotation proccessor, or inherits from such class, or have generic with such class.
Related
I took the Udacity course to learn developing Android apps with Kotlin, using data binding multiple times during it and having no problems enabling it, but now, trying to set up my first project, I am having some problems with it: Android Studio doesn't find the reference to the MainActivityBinding.
C:\Dev\Projects\Android_Studio\myproject\app\src\main\java\com\guglielmoboi\myproject\MainActivity.kt: (8, 35): Unresolved reference: MainActivityBinding
These are the files I produced:
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()
}
}
build.gradle (:app)
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-kapt'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.guglielmoboi.myproject"
minSdkVersion 26
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'
}
}
**buildFeatures {
dataBinding true
viewBinding true
}**
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
/* Room dependencies */
def room_version = "2.3.0"
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"
// optional - Kotlin Extensions and Coroutines support for Room
implementation "androidx.room:room-ktx:$room_version"
// optional - Test helpers
testImplementation "androidx.room:room-testing:$room_version"
/* Navigation dependencies */
def nav_version = "2.3.5"
// Java language implementation
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
// Kotlin
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
// Feature module Support
implementation "androidx.navigation:navigation-dynamic-features-fragment:$nav_version"
// Testing Navigation
androidTestImplementation "androidx.navigation:navigation-testing:$nav_version"
// Jetpack Compose Integration
implementation "androidx.navigation:navigation-compose:1.0.0-alpha10"
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
activity_main.xml
<?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"
xmlns:tools="http://schemas.android.com/tools">
<data>
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
MainActivity.kt
package com.guglielmoboi.myproject
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
class MainActivity : AppCompatActivity()
{
private lateinit var binding: MainActivityBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
Inside MainActivity
remove setContentView(R.layout.activity_main)
and add
val binding: ActivityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main)
Use binding for all the UI stuff.
I've got following error while trying to build prject with MVVM and data binding. I've searched through everything I could find here on StackOverflow, or common mistakes on the internet, but nothing worked for my case.
The only message I receive is this error. It looks like this in buildOutput:
I've had my packages named with capital letters and I've found here that this might be the cause because compiler treat them as names of classes, so I've changed them all to start with small letters, but that haven't helped.
I've created ViewModelFactory for creating my ViewModel inside Activity so I could send additional parameters with constructor using factory, so I've tried to remove it and use no parameters and create instance without using factory for this purpose, but still I havn't got any results (same error)
I was chaning both build.gradle's in different ways but result was always the same.
Finally I've deleted data binding and variable from XML, then I was able to run the app (with other errors, but I would be probably able to deal with those by myself) but I want to leave it as it is and just deal with my error.
I am not experienced with MVVM and data binding so it can be just a stupid mistake, but it is hard to find it if I don't know where I should look for it.
Here I post most important codes, if you need more then let me know:
build.grale(app)
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
android {
compileSdkVersion 30
buildToolsVersion "29.0.3"
defaultConfig {
applicationId "com.example.rickmorty"
minSdkVersion 24
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'
}
}
buildFeatures{
dataBinding = true
viewBinding = true
}
}
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.1'
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.android.material:material:1.3.0-alpha02'
implementation "android.arch.lifecycle:extensions:1.1.1"
implementation "androidx.cardview:cardview:1.0.0"
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
implementation 'io.reactivex.rxjava2:rxjava:2.1.7'
implementation 'com.github.bumptech.glide:glide:4.6.1'
kapt 'com.github.bumptech.glide:compiler:4.4.0'
}
**build.gradle(project)
buildscript {
ext.kotlin_version = "1.3.72"
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:4.0.0"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
MainActivity
class MainActivity : AppCompatActivity() {
lateinit var mainViewModel: MainViewModel
lateinit var mAdapter: CharactersAdapter
lateinit var api: CharacterAPI
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val retrofit = RetrofitClient.instance
api = retrofit.create(CharacterAPI::class.java)
setupViewModel()
setupRecycler()
mainViewModel.getData().observe(this,
Observer<List<Results>> { t ->
mAdapter = CharactersAdapter(this#MainActivity, t!!)
rvCharacters.adapter = mAdapter
})
}
fun setupRecycler() {
val lManager = LinearLayoutManager(this#MainActivity)
rvCharacters.apply {
setHasFixedSize(true)
layoutManager = lManager
}
rvCharacters.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
val visibleItems = lManager.childCount
val totalItems = lManager.itemCount
val firstVisible = lManager.findFirstVisibleItemPosition()
if (dy > 0) {
if (!mainViewModel.isLoading.value!! && (visibleItems + firstVisible) >= totalItems) {
mainViewModel.scrolledNext()
}
} else {
if (!mainViewModel.isLoading.value!! && (totalItems - visibleItems) <= 0) {
mainViewModel.scrolledPrev()
}
}
}
})
}
fun setupViewModel() {
mainViewModel = ViewModelProviders.of(this, MainViewModelFactory(application, api))
.get(MainViewModel::class.java)
DataBindingUtil.setContentView<ActivityMainBinding>(
this, R.layout.activity_main
).apply {
lifecycleOwner = this#MainActivity
viewmodel = mainViewModel
}
}
}
activity_main.xml
<?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"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="viewmodel"
type="com.example.rickmorty.ViewModel.MainViewModel"/>
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<RelativeLayout
android:id="#+id/rlPageTitleHolder"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHeight_percent="0.1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:layout_centerInParent="true"
android:text="#{() -> viewmodel.pageNumber}"/>
</RelativeLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rvCharacters"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="#id/rlPageTitleHolder"
app:layout_constraintHeight_percent="0.9"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
As I wrote earlier, after removing variable from XML and binding from MainActivity error disappears.
I know it is a lot of code, so if something is redundant just let me know. There is factory class still missing, but I will post it if it's necessary. Also MainRepo is one I haven't attached here, but it's quite long, but I can post it all if you're gonna need it.
Here is the problem --> android:text="#{() -> viewmodel.pageNumber}"
correct syntax for assigning value using dataBinding is
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:layout_centerInParent="true"
android:text="#{viewmodel.pageNumber}"/>
I'm trying to create a list view with the data from the Firebase real time database.For some reason I am keep getting a red line under this code.Hope you can let me know what is my mistake here. Thank you :). I have attached my layout file and also my app gradle codes.
(this,String.class,android.R.layout.simple_list_item_1,databaseReference)
Below here is the complete code
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReferenceFromUrl("https://vsem-inventory.firebaseio.com/");
FirebaseListAdapter<String> firebaseListAdapter = new FirebaseListAdapter<String>(this,String.class,android.R.layout.simple_list_item_1,databaseReference) {
#Override
protected void populateView(#NonNull View v, #NonNull String model, int position) {
TextView textView = v.findViewById(android.R.id.text1);
}
};
mListView.setAdapter(firebaseListAdapter);
content_main_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainMenu">
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="0dp" />
</RelativeLayout>
App gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.user.vseminventory"
minSdkVersion 19
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.google.firebase:firebase-auth:16.0.5'
implementation 'com.google.firebase:firebase-database:16.0.5'
implementation 'com.google.firebase:firebase-storage:16.0.5'
implementation 'com.android.support:design:28.0.0'
implementation 'com.firebase:firebase-client-android:2.3.1'
implementation 'com.firebaseui:firebase-ui-database:4.3.0'
}
apply plugin: 'com.google.gms.google-services'
com.google.gms.googleservices.GoogleServicesPlugin.config.disableVersionCheck = true
It seems you are using the new Firebaseui (version 3.0+), the constructor of the FirebaseListAdapter was changed.
First, you need to configure the adapter by using the class FirebaseListOptions, then you can create an instance of FirebaseListAdapter.
Check this code from the docs:
FirebaseListOptions<Chat> options = new FirebaseListOptions.Builder<Chat>()
.setQuery(query, Chat.class)
.build();
FirebaseListAdapter<Chat> adapter = new FirebaseListAdapter<Chat>(options) {
#Override
protected void populateView(View v, Chat model, int position) {
// Bind the Chat to the view
// ...
}
};
As you can see the FirebaseListAdapter takes a parameter of type FirebaseListOptions
More info here:
https://github.com/firebase/FirebaseUI-Android/blob/master/database/README.md#using-firebaseui-to-populate-a-listview
You can see the source code here:
https://github.com/firebase/FirebaseUI-Android/blob/master/database/src/main/java/com/firebase/ui/database/FirebaseListAdapter.java
I am trying to use Room in my project. Gradle syncing files well, but I get RunitomeException when trying to get database instance.
"Caused by: java.lang.RuntimeException: cannot find implementation for com.fillooow.android.testtochka.BusinessLogic.database.GithubUserSearchDataBase. GithubUserSearchDataBase_Impl does not exist"
I searched this issue and find that solution is to add this lines into build.gradle file:
implementation "android.arch.persistence.room:runtime:1.1.1"
implementation "android.arch.persistence.room:rxjava2:1.1.1"
kapt "android.arch.persistence.room:compiler:1.1.1"
and also aply this plugin
apply plugin: 'kotlin-kapt'
But this is my build.gradle file, and I still have this issue:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.fillooow.android.testtochka"
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:support-v4:28.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'io.reactivex.rxjava2:rxandroid:2.1.0'
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.5.0'
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'com.android.support:design:28.0.0'
implementation 'com.jakewharton.rxbinding2:rxbinding-kotlin:2.0.0'
implementation 'com.google.android.gms:play-services-auth:16.0.1'
implementation 'com.facebook.android:facebook-android-sdk:[4,5)'
implementation 'com.vk:androidsdk:1.6.9'
implementation 'com.android.support:cardview-v7:28.0.0'
implementation 'com.squareup.picasso:picasso:2.71828'
implementation "android.arch.persistence.room:runtime:1.1.1"
implementation "android.arch.persistence.room:rxjava2:1.1.1"
kapt "android.arch.persistence.room:compiler:1.1.1"
}
And this is DataBase class
import android.arch.persistence.room.Room
import android.arch.persistence.room.RoomDatabase
import android.content.Context
abstract class GithubUserSearchDataBase : RoomDatabase(){
abstract fun githubUserSearchDataDao(): GithubUserSearchDataDao
companion object {
private var INSTANCE: GithubUserSearchDataBase? = null
fun getInstance(context: Context): GithubUserSearchDataBase?{
if (INSTANCE == null){
synchronized(GithubUserSearchDataBase::class){
INSTANCE = Room.databaseBuilder(context.applicationContext,
GithubUserSearchDataBase::class.java,
"github.db")
.build()
}
}
return INSTANCE
}
fun destroyInstance(){
INSTANCE = null
}
}
}
Project were cleared and rebuild a lot of times.
So, maybe I missed something?
Your gradle file looks fine. Just be sure to Sync it after you have added the proper imports.
What you are missing is the #Database annotation on top of your Database class.
#Database(entities = [Entity1::class, Entity2::class, Entity3::class, Entity4::class], version = 1)
abstract class GithubUserSearchDataBase : RoomDatabase(){
abstract fun githubUserSearchDataDao(): GithubUserSearchDataDao
companion object {
private var INSTANCE: GithubUserSearchDataBase? = null
fun getInstance(context: Context): GithubUserSearchDataBase?{
if (INSTANCE == null){
synchronized(GithubUserSearchDataBase::class){
INSTANCE = Room.databaseBuilder(context.applicationContext,
GithubUserSearchDataBase::class.java,
"github.db")
.build()
}
}
return INSTANCE
}
fun destroyInstance(){
INSTANCE = null
}
}
}
In the entities attribute of the #Database annotation you must put an array with all the classes of your model annotated with the #Entity annotation. I put there fake names, you should put the proper ones.
I'm using androidx navigation architecture along with Kotlin 1.2.71 in Android studio 3.2.1. My fragment code is:
package com.dell.andnav.fragments
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.navigation.fragment.findNavController
import com.dell.andnav.R
import kotlinx.android.synthetic.main.fragment_welcome.*
/**
* A simple [Fragment] subclass.
* Use the [WelcomeFragment.newInstance] factory method to
* create an instance of this fragment.
*
*/
class WelcomeFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_welcome, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
detailButton.setOnClickListener {
findNavController().navigate(R.id.action_welcomeFragment_to_detailsFragment)
}
}
companion object {
#JvmStatic
fun newInstance() = WelcomeFragment()
}
}
And layout code is:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragments.WelcomeFragment">
<Button
android:id="#+id/detailButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/to_detail_fragment"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
My build.gradle is:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'androidx.navigation.safeargs'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.dell.andnav"
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
androidExtensions {
experimental = true
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
def nav_version = "1.0.0-alpha07"
implementation fileTree(dir: 'libs', include: ['*.jar'])
// implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
// implementation 'com.android.support:support-v4:28.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.multidex:multidex:2.0.0'
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation "android.arch.navigation:navigation-fragment-ktx:$nav_version"
implementation "android.arch.navigation:navigation-ui-ktx:$nav_version"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}
repositories {
mavenCentral()
}
When I try to run my code, I get below error:
Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
public val Activity.detailButton: Button! defined in kotlinx.android.synthetic.main.fragment_welcome
public val Dialog.detailButton: Button! defined in kotlinx.android.synthetic.main.fragment_welcome
public val android.app.Fragment.detailButton: Button! defined in kotlinx.android.synthetic.main.fragment_welcome
public val androidx.fragment.app.Fragment.detailButton: Button! defined in kotlinx.android.synthetic.main.fragment_welcome
public val LayoutContainer.detailButton: Button! defined in kotlinx.android.synthetic.main.fragment_welcome
How can I resolve that error?
Button you are accessing is inside your fragement but your are accessing it directly. You should access it via parent view like we do with java rootview.findViewById() so use
view.detailButton.setOnClickListener {
findNavController().navigate(R.id.action_welcomeFragment_to_detailsFragment)
}
I have faced the same problem with my adapter class and finally problem solved by adding the following code to build.gradle(Module: app) file of the project.
apply plugin: 'org.jetbrains.kotlin.android.extensions'
androidExtensions {
experimental = true
}
Since this new version of Kotlin, the Android Extensions have incorporated some new interesting features like caches in any class
I had this issue and figured out that the root cause was play-services with versions 17.x.x
My fix
Change
google_android_gms_version
in
implementation "com.google.android.gms:play-services-location:$google_android_gms_version"
implementation "com.google.android.gms:play-services-gcm:$google_android_gms_version"
from 17.x.x to 15.0.1
The reason is that play services 17.x.x has dependencies on Androidx
General way
Click the Gradle option in the extreme right panel,
Go to app -> help ->dependencies
Search androidx in the chart. Find out which library has it. Change or modify your gradle accordingly.
Those who can migrate to androidx, that well and good. If you have to keep it at appcompat, then follow the answer.
With androidx, you have to access with itemView
itemView.detailButton
import .view.* like below
import kotlinx.android.synthetic.main.fragment_welcome.view.*
instead of
import kotlinx.android.synthetic.main.fragment_welcome.*
I have two issues with this error.
I mixed receiver type in extension.
import android.widget.Toolbar
fun Toolbar.setMenuItem() : Boolean {
...
}
Then tried to apply this function to Toolbar in a layout. But in XML it was androidx.appcompat.widget.Toolbar.
This function led to the error:
private fun showProgress(view: View? = null) {
(view?.progressBar ?: progressBar)?.visibility = View.VISIBLE
}
Android Studio offered layouts that contained progressBar, and I chose one. But view?.progressBar continued to be red. Then I shortened a header:
private fun showProgress(view: View).
Only after that AS offered layouts. Then I again wrote: private fun showProgress(view: View? = null).