gpt4 book ai didi

java - 使缓存失效并重新启动并不能修复 BR 导入错误...数据绑定(bind)问题

转载 作者:行者123 更新时间:2023-12-02 01:27:40 27 4
gpt4 key购买 nike

几乎完成了这个应用程序,并在我的 Movie 类中遇到了数据绑定(bind)方面的编译时错误,我扩展了 BaseObserver 并在尝试构建项目时我可以' t 为 notifyPropertyChanged(BR.voteCount); 导入 BR 在二传手上。我看不到这个问题,但如果我尝试编译并且它通过了,它会在启动时崩溃并显示以下堆栈跟踪。

stackTrace

我想如果是生成的代码有问题,那么我该如何解决呢???

真的很困惑!任何帮助表示赞赏。

我添加了依赖项,看不到任何问题......

apply plugin: 'com.android.application'

android {
compileSdkVersion 29
dataBinding {
enabled = true
}
buildToolsVersion "29.0.0"
defaultConfig {
applicationId "com.example.tmdbclient"
minSdkVersion 19
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
def lifecycle_version = "2.0.0"

// ViewModel and LiveData
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
// alternatively - just ViewModel
implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version" // For Kotlin use lifecycle-viewmodel-ktx

annotationProcessor 'com.android.databinding:compiler:2.3.0'

implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.android.material:material:1.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

implementation 'com.squareup.retrofit2:retrofit:2.6.0'
implementation 'com.squareup.retrofit2:converter-gson:2.6.0'
implementation 'com.android.support:cardview-v7:29.0.0'
implementation 'com.android.support:recyclerview-v7:29.0.0'
implementation 'com.android.support:design:29.0.0'
implementation 'com.github.bumptech.glide:glide:4.9.0'

}
<小时/>

型号

package com.example.tmdbclient.model;

import android.os.Parcel;
import android.os.Parcelable;
import android.widget.ImageView;


import androidx.databinding.BaseObservable;
import androidx.databinding.Bindable;
import androidx.databinding.BindingAdapter;
import androidx.databinding.BR;
import com.bumptech.glide.Glide;
import com.example.tmdbclient.R;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

import java.util.ArrayList;
import java.util.List;

public class Movie extends BaseObservable implements Parcelable
{

@SerializedName("vote_count")
@Expose
private Integer voteCount;
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("video")
@Expose
private Boolean video;
@SerializedName("vote_average")
@Expose
private Double voteAverage;
@SerializedName("title")
@Expose
private String title;
@SerializedName("popularity")
@Expose
private Double popularity;
@SerializedName("poster_path")
@Expose
private String posterPath;
@BindingAdapter("posterPath")
public void loadImage(ImageView imageView, String imageURL) {

String imagePath = "https://image.tmdb.org/t/p/w500" + imageURL;

Glide.with(imageView.getContext())
.load(imagePath)
.placeholder(R.drawable.loading)
.into(imageView);



}


@SerializedName("original_language")
@Expose
private String originalLanguage;
@SerializedName("original_title")
@Expose
private String originalTitle;
@SerializedName("genre_ids")
@Expose
private List<Integer> genreIds = new ArrayList<Integer>();
@SerializedName("backdrop_path")
@Expose
private String backdropPath;
@SerializedName("adult")
@Expose
private Boolean adult;
@SerializedName("overview")
@Expose
private String overview;
@SerializedName("release_date")
@Expose
private String releaseDate;
public final static Parcelable.Creator<Movie> CREATOR = new Creator<Movie>() {


@SuppressWarnings({
"unchecked"
})
public Movie createFromParcel(Parcel in) {
return new Movie(in);
}

public Movie[] newArray(int size) {
return (new Movie[size]);
}

}
;

protected Movie(Parcel in) {
this.voteCount = ((Integer) in.readValue((Integer.class.getClassLoader())));
this.id = ((Integer) in.readValue((Integer.class.getClassLoader())));
this.video = ((Boolean) in.readValue((Boolean.class.getClassLoader())));
this.voteAverage = ((Double) in.readValue((Double.class.getClassLoader())));
this.title = ((String) in.readValue((String.class.getClassLoader())));
this.popularity = ((Double) in.readValue((Double.class.getClassLoader())));
this.posterPath = ((String) in.readValue((String.class.getClassLoader())));
this.originalLanguage = ((String) in.readValue((String.class.getClassLoader())));
this.originalTitle = ((String) in.readValue((String.class.getClassLoader())));
in.readList(this.genreIds, (java.lang.Integer.class.getClassLoader()));
this.backdropPath = ((String) in.readValue((String.class.getClassLoader())));
this.adult = ((Boolean) in.readValue((Boolean.class.getClassLoader())));
this.overview = ((String) in.readValue((String.class.getClassLoader())));
this.releaseDate = ((String) in.readValue((String.class.getClassLoader())));
}

public Movie() {
}


@Bindable
public Integer getVoteCount() {
return voteCount;
}

public void setVoteCount(Integer voteCount) {
this.voteCount = voteCount;
notifyPropertyChanged(BR.voteCount);
}


@Bindable
public Integer getId() {
return id;
}

public void setId(Integer id)
{
this.id = id;
notifyPropertyChanged(com.example.tmdbclient.BR.id);


}

@Bindable
public Boolean getVideo() {
return video;
}

public void setVideo(Boolean video) {

this.video = video;
notifyPropertyChanged(BR.video);
}

@Bindable
public Double getVoteAverage() {
return voteAverage;
}

public void setVoteAverage(Double voteAverage) {

this.voteAverage = voteAverage;
notifyPropertyChanged(BR.voteAverage);
}

@Bindable
public String getTitle() {
return title;
}

public void setTitle(String title) {

this.title = title;
notifyPropertyChanged(BR.title);
}

@Bindable
public Double getPopularity() {
return popularity;
}

public void setPopularity(Double popularity) {

this.popularity = popularity;
notifyPropertyChanged(BR.popularity);
}

@Bindable
public String getPosterPath() {
return posterPath;
}

public void setPosterPath(String posterPath) {

this.posterPath = posterPath;
notifyPropertyChanged(BR.posterPath);
}

@Bindable
public String getOriginalLanguage() {
return originalLanguage;
}

public void setOriginalLanguage(String originalLanguage) {
this.originalLanguage = originalLanguage;
notifyPropertyChanged(BR.originalLanguage);
}

@Bindable
public String getOriginalTitle() {
return originalTitle;
}

public void setOriginalTitle(String originalTitle) {

this.originalTitle = originalTitle;
notifyPropertyChanged(BR.originalTitle);
}

@Bindable
public List<Integer> getGenreIds() {
return genreIds;
}

public void setGenreIds(List<Integer> genreIds) {

this.genreIds = genreIds;
notifyPropertyChanged(BR.genreIds);
}

@Bindable
public String getBackdropPath() {
return backdropPath;
}

public void setBackdropPath(String backdropPath) {

this.backdropPath = backdropPath;
notifyPropertyChanged(BR.backdropPath);
}

@Bindable
public Boolean getAdult() {
return adult;
}

public void setAdult(Boolean adult) {

this.adult = adult;
notifyPropertyChanged(BR.adult);
}

@Bindable
public String getOverview() {
return overview;
}

public void setOverview(String overview) {
this.overview = overview;
notifyPropertyChanged(BR.overview);
}

@Bindable
public String getReleaseDate() {
return releaseDate;
}

public void setReleaseDate(String releaseDate) {

this.releaseDate = releaseDate;
notifyPropertyChanged(BR.releaseDate);
}


public void writeToParcel(Parcel dest, int flags) {
dest.writeValue(voteCount);
dest.writeValue(id);
dest.writeValue(video);
dest.writeValue(voteAverage);
dest.writeValue(title);
dest.writeValue(popularity);
dest.writeValue(posterPath);
dest.writeValue(originalLanguage);
dest.writeValue(originalTitle);
dest.writeList(genreIds);
dest.writeValue(backdropPath);
dest.writeValue(adult);
dest.writeValue(overview);
dest.writeValue(releaseDate);
}

public int describeContents() {
return 0;
}

}
<小时/>

Activity 主

<?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"

>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/swipe"
android:layout_width="match_parent"
android:layout_height="match_parent">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".view.MainActivity"
android:layout_marginTop="15dp">


<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvMovies"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:scrollbars="vertical"/>
</RelativeLayout>

</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

</layout>
<小时/>

Activity 电影

<?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:bind="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">

<data>

<variable
name="movie"
type="com.example.tmdbclient.model.Movie" />

</data>

<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".view.MovieActivity">

<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="match_parent"

android:theme="@style/Widget.Design.AppBarLayout">

<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/ctMovie"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:expandedTitleTextAppearance="@android:color/transparent"
app:layout_scrollFlags="scroll|exitUntilCollapsed">


<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<ImageView
android:id="@+id/ivMovieLarge"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:layout_collapseMode="parallax"
bind:posterPath="@{movie.posterPath}" />
</RelativeLayout>

<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_collapseMode="pin" />


</com.google.android.material.appbar.CollapsingToolbarLayout>

</com.google.android.material.appbar.AppBarLayout>


<include
android:id="@+id/secondary_layout"
layout="@layout/content_movie"
bind:secondaryMovie="@{movie}" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

</layout>
<小时/>

内容电影

<?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="secondaryMovie"
type="com.example.tmdbclient.model.Movie" />

</data>

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".view.MovieActivity"
tools:showIn="@layout/activity_movie">

<LinearLayout
android:id="@+id/add"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:showIn="@layout/activity_movie">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:id="@+id/tvMovieTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="3dp"
android:padding="16dp"
android:text="@{secondaryMovie.title}"
android:textSize="20sp"
app:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_creator="1"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/tvRating"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="3dp"
android:padding="16dp"
android:text="@{Double.toString(secondaryMovie.voteAverage)}"
android:textSize="16sp"
app:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_creator="1"
app:layout_constraintTop_toBottomOf="@id/tvMovieTitle"

/>


<TextView
android:id="@+id/tvSynopsis"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="3dp"
android:padding="16dp"
android:text="@{secondaryMovie.overview}"
android:textSize="16sp"
app:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_creator="1"
app:layout_constraintTop_toBottomOf="@id/tvRating"

/>


<TextView
android:id="@+id/tvRelease"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="3dp"
android:padding="16dp"
android:text="@{secondaryMovie.releaseDate}"
android:textSize="16sp"
app:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_creator="1"
app:layout_constraintTop_toBottomOf="@id/tvSynopsis"

/>

</androidx.constraintlayout.widget.ConstraintLayout>


</ScrollView>
</LinearLayout>
</RelativeLayout>

</layout>
<小时/>

电影列表项

<?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:bind="http://schemas.android.com/apk/res-auto">

<data>

<variable
name="movie"
type="com.example.tmdbclient.model.Movie" />

</data>


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

<androidx.cardview.widget.CardView
android:id="@+id/cvMovie"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:elevation="3dp"
app:cardCornerRadius="3dp">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:id="@+id/ivMovie"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="?attr/selectableItemBackgroundBorderless"
android:scaleType="fitXY"
bind:posterPath="@{movie.posterPath}" />


<TextView
android:id="@+id/tvTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/ivMovie"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:paddingRight="10dp"
android:text="@{movie.title}"
android:textColor="@color/colorPrimary"
android:textSize="15sp" />


<TextView
android:id="@+id/tvRating"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/tvTitle"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:paddingRight="10dp"
android:text="@{Double.toString(movie.voteAverage)}"
android:textColor="@color/colorPrimary"
android:textSize="15sp" />

</RelativeLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>

</layout>

最佳答案

你应该做的第一件事是

更改这些行

implementation 'com.android.support:cardview-v7:29.0.0'
implementation 'com.android.support:recyclerview-v7:29.0.0'
implementation 'com.android.support:design:29.0.0'

implementation 'androidx.cardview:cardview:1.0.0'
implementation 'androidx.recyclerview:recyclerview:1.0.0'
implementation 'androidx.design:design:1.0.0'

问题出在 Movie 类的 loadImage 方法上。将其定义为静态并且它应该可以工作。

关于java - 使缓存失效并重新启动并不能修复 BR 导入错误...数据绑定(bind)问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56655642/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com