Error recognizing dataBinding class android - android

in one of my projects I have started to implement dataBinding in the main activity by separating the data in a class called view, in another class called viewModel and in the model class. Using these classes and the dataBinding, the application has been compiled correctly, but when I put the classes in different packages to follow the MVVM architecture, the self-generated class named ActivityLoginBinding does not recognize the class that is inside the ViewModel package. I have already done searches of different projects to see if it was my mistake at syntax level, but they are well written. I have also noticed that all the projects are divided into different packages and work correctly. Any idea why this happens and how could it be solved? Thank you.
my 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="student3"
type="com.juan97.prueba.ViewModel.Student3ViewModel"/>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".View.MainActivity">
<TextView
android:layout_marginTop="50sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:hint="#{student3.hint1}"/>
<TextView
android:layout_marginTop="50sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:hint="#{student3.hint2}"/>
</LinearLayout>
</layout>
gradle module app
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.juan97.prueba"
minSdkVersion 19
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'
}
}
dataBinding {
enabled = true
}
}
dependencies {
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'
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'
}
Different packages I have:
compile error

Related

Can't Resolve AcitivityMainBinding for DataBinding

I'm trying to use MVVM Architure Pattern, but when I typed ActivityMainBinding nothing appeared in the suggestion menu
I tried to change the XML file name and changed it back to the original name but nothing happened
here is my dependency
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.alphabet"
minSdkVersion 16
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard- rules.pro'
}
}
dataBinding{
enabled = true
}
}
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
def lifecycle_version = "2.1.0"
// ViewModel and LiveData
implementation "android.arch.lifecycle:extensions:$lifecycle_version"
implementation 'io.reactivex.rxjava3:rxandroid:3.0.0'
// Because RxAndroid releases are few and far between, it is recommended you also
// explicitly depend on RxJava's latest version for bug fixes and new features.
// (see https://github.com/ReactiveX/RxJava/releases for latest 3.x.x version)
implementation 'io.reactivex.rxjava3:rxjava:3.0.0'
}
Main Activity
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
}
}
xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<data>
</data>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
.............................................................
The xml is malformed for data binding, it should be :
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="viewmodel"
type="com.myapp.data.ViewModel" />
</data>
<ConstraintLayout... /> <!-- UI layout's root element -->
</layout>
As per the documentation : https://developer.android.com/topic/libraries/data-binding the root element should be <layout> ... </layout>
And easy way to convert to xml data binding layout is to ALT-Enter on your standard root element view/viewgroup in existing, non data binding layouts and choose the "convert" option.

Unable to use barteksc PDF Viewer in my app

I tried to use [bartecksc AndroidPdfViewer][1]
[1]: https://github.com/barteksc/AndroidPdfViewer in my android app to view PDF files from the assets folder inside the app but it is not working. It shows a blank screen.
Here is what I did:
I added this dependency in the build.gradle file
implementation 'com.github.barteksc:android-pdf-viewer:3.1.0-beta.1'
(I even tried using implementation 'com.github.barteksc:android-pdf-viewer:2.8.2')
I added the following widget in activity_main.xml
<com.github.barteksc.pdfviewer.PDFView
android:id="#+id/pdfView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
I put the following code in the onCreate method
PDFView pdfView=findViewById(R.id.pdfView);
pdfView.fromAsset("sample_file.pdf");
The file that I wanted to display was located in the assets folder with the name sample_file.pdf
Following is my MainActivity.java
package com.umersoftwares.bartekscpdfview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.github.barteksc.pdfviewer.PDFView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PDFView pdfView=findViewById(R.id.pdfView);
pdfView.fromAsset("sample_file.pdf");
}
}
Following is my activity_main.xml
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.github.barteksc.pdfviewer.PDFView
android:id="#+id/pdfView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</android.support.constraint.ConstraintLayout>
My build.gradle file is:
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.umersoftwares.bartekscpdfview"
minSdkVersion 16
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(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
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.github.barteksc:android-pdf-viewer:2.8.2'
}
The problem was that it was necessary to call the .load() method on the PDFView object to display the pdf. So, in the java file, it should be like this
pdfView.fromAsset("sample_file.pdf")
.load();
Instead of only:
pdfView.fromAsset("sample_file.pdf");
The code you posted is missing necessary permissions for reading storage.
there is a demo over github with the library that you posted. check the code for requesting read storage permission and implement it in your code.
You can refer this sample class of demo provided with library.
https://github.com/barteksc/AndroidPdfViewer/blob/master/sample/src/main/java/com/github/barteksc/sample/PDFViewActivity.java

any View not appearing in the android studio editor

when building the app the view i've put in appears but in the editor it is not appearing, and sometimes when i create a ImageView some of the images I pasted in the drawables causes rendering problems. I recently updated my android studio and now it is full of bugs.the image attached below is a new project, I only added image view and convert it to relative layout since constraint layout gives me a ton of errors. thank you for answering my question.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Hello World!"
android:textAppearance="#style/TextAppearance.AppCompat"
android:visibility="visible" />
<ImageView
android:id="#+id/imageView"
android:layout_width="30dp"
android:layout_height="30dp" />
</RelativeLayout>
P.S.
the image view's src or compatsrc stuff, I've deleted it since it causes errors when building just to test it out. and when opening existing projects the app folder seems to be deleted/moved.
here is the gradle file
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.rowel.animation"
minSdkVersion 15
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(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0-rc01'
implementation 'com.android.support.constraint:constraint-layout:1.1.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'
SDK 28 is buggy and has lots of layout issues and program bugs.
you can try adding Base. to your theme's parent in styles.xml
eg:
<resource>
<style name="AppTheme" parent="Theme.AppCompat">
...
to
<resource>
<style name="AppTheme" parent="Base.Theme.AppCompat">
...
if doesn't work, then try Invalidating Cache
if that doesn't work either, try changing targetSdkVersion to 27 or below
Change your compileSdkVersion and targetSdkVersion to 27
and also change this
implementation 'com.android.support:appcompat-v7:27.1.1'

Androidx error inflating view

I'm trying to use Androidx. The app is pretty new, so there is not much code. I did use the "Refactor to Androidx" option in android studio. But sometime after that, it stopped working. I don't know what made it stop working.
What am I supposed to do?
But it get this error
Error inflating class androidx.constraintlayout.widget.ConstraintLayout
Main activity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
pick_image.setOnClickListener {
toast("Pick image clicked")
}
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/pick_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="Pick image"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
build.gradle (app)
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.alvarlagerlof.blurr"
minSdkVersion 19
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
// Androidx
implementation 'androidx.core:core-ktx:1.0.0-alpha3'
implementation 'androidx.appcompat:appcompat:1.0.0-alpha1'
implementation 'androidx.constraintlayout:constraintlayout:1.1.1'
// Testing
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.0-alpha3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha3'
}
EDITED 2: Since the newer version, they've reverted back to androidx.constraintlayout.widget.ConstraintLayout.
Just keep reading if you're using constraint layout version 1.1.1
EDITED: As Arturo Mejia's answer, just press ⇧⌘R or Ctrl + Shift + R to
replace any
androidx.constraintlayout.widget.ConstraintLayout
with
androidx.constraintlayout.ConstraintLayout
in all XML file that use Constraint Layout.
That's a change since constraint layout version v1.1.1 (in v1.1.0 ConstraintLayout class is still inside ".widget" package)
Old workaround answer:
Change from
implementation 'androidx.constraintlayout:constraintlayout:1.1.1'
to
implementation 'androidx.constraintlayout:constraintlayout:1.1.0'
I tried to press Command + Click at ConstraintLayout in
androidx.constraintlayout.widget.ConstraintLayout from XML file to
show the original class but it doesn't find any thing. After I edited
the constraint layout version to 1.1.0 then it's there.
In my case updating dependency
implementation 'androidx.constraintlayout:constraintlayout:1.1.2'
to
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
has helped
I fixed it by changing the gradle dependencies form alpha3 to alpha1

Rendering Problems:Binary XML file line #-1: No start tag found

Initially I was getting error in Manifest file at android:theme="#style/AppTheme" . I look for the solution and from this solution Android Studio: Error in Layout Files - Header expected , I get rid of that error.
But on the other hand I started getting errors in all my layout preview like this
xml file for the above image is
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MainActivity" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Process Inbox"
android:id="#+id/heading"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="center"
android:textColor="#964acf"
android:textStyle="bold" />
<ExpandableListView
android:id="#+id/inbox_expandable_list"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/heading">
</ExpandableListView>
</RelativeLayout>
I tried undo the changes but no success, can anyone guide me the reason or solution for the problem.
here is my app.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.abc.xyz"
minSdkVersion 14
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.0'
compile 'io.realm:realm-android:0.82.1'
compile 'com.google.code.gson:gson:2.2.2'
}
There's a similar question here and here
Moving the problematic tab between different split views did the trick in a second.

Categories

Resources