2017-08-23 108 views
0

我建立一個應用程序來測試改造CardViewRecyclerViewGlide。我有這樣的對象「項目」,我定義了一個電影的屬性:不兼容類型:列表<Movie>不能轉換爲List <Item>

public class Item { 
    @SerializedName("poster_path") 
    private String posterPath; 
    @SerializedName("adult") 
    private boolean adult; 
    @SerializedName("overview") 
    private String overview; 
    @SerializedName("release_date") 
    private String releaseDate; 
    @SerializedName("genre_ids") 
    private List<Integer> genreIds = new ArrayList<Integer>(); 
    @SerializedName("id") 
    private Integer id; 
    @SerializedName("original_title") 
    private String originalTitle; 
    @SerializedName("original_language") 
    private String originalLanguage; 
    @SerializedName("title") 
    private String title; 
    @SerializedName("backdrop_path") 
    private String backdropPath; 
    @SerializedName("popularity") 
    private Double popularity; 
    @SerializedName("vote_count") 
    private Integer voteCount; 
    @SerializedName("video") 
    private Boolean video; 
    @SerializedName("vote_average") 
    private Double voteAverage; 

    public Item(String posterPath, boolean adult, String overview, String releaseDate, List<Integer> genreIds, Integer id, 
        String originalTitle, String originalLanguage, String title, String backdropPath, Double popularity, 
        Integer voteCount, Boolean video, Double voteAverage) { 
     this.posterPath = posterPath; 
     this.adult = adult; 
     this.overview = overview; 
     this.releaseDate = releaseDate; 
     this.genreIds = genreIds; 
     this.id = id; 
     this.originalTitle = originalTitle; 
     this.originalLanguage = originalLanguage; 
     this.title = title; 
     this.backdropPath = backdropPath; 
     this.popularity = popularity; 
     this.voteCount = voteCount; 
     this.video = video; 
     this.voteAverage = voteAverage; 
    } 

    public String getPosterPath() { 
     return posterPath; 
    } 

    public void setPosterPath(String posterPath) { 
     this.posterPath = posterPath; 
    } 

    public boolean isAdult() { 
     return adult; 
    } 

    public void setAdult(boolean adult) { 
     this.adult = adult; 
    } 

    public String getOverview() { 
     return overview; 
    } 

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

    public String getReleaseDate() { 
     return releaseDate; 
    } 

    public void setReleaseDate(String releaseDate) { 
     this.releaseDate = releaseDate; 
    } 

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

    public void setGenreIds(List<Integer> genreIds) { 
     this.genreIds = genreIds; 
    } 

    public Integer getId() { 
     return id; 
    } 

    public void setId(Integer id) { 
     this.id = id; 
    } 

    public String getOriginalTitle() { 
     return originalTitle; 
    } 

    public void setOriginalTitle(String originalTitle) { 
     this.originalTitle = originalTitle; 
    } 

    public String getOriginalLanguage() { 
     return originalLanguage; 
    } 

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

    public String getTitle() { 
     return title; 
    } 

    public void setTitle(String title) { 
     this.title = title; 
    } 

    public String getBackdropPath() { 
     return backdropPath; 
    } 

    public void setBackdropPath(String backdropPath) { 
     this.backdropPath = backdropPath; 
    } 

    public Double getPopularity() { 
     return popularity; 
    } 

    public void setPopularity(Double popularity) { 
     this.popularity = popularity; 
    } 

    public Integer getVoteCount() { 
     return voteCount; 
    } 

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

    public Boolean getVideo() { 
     return video; 
    } 

    public void setVideo(Boolean video) { 
     this.video = video; 
    } 

    public Double getVoteAverage() { 
     return voteAverage; 
    } 

    public void setVoteAverage(Double voteAverage) { 
     this.voteAverage = voteAverage; 
    } 
} 

而且裏面MainActivity我宣佈名單的項目:

private List<Item> movies; 

後來onCreate方法裏面我把這種方法從IMDB API請求一些數據:

ApiInterface apiService = 
       ApiClient.getClient().create(ApiInterface.class); 

     Call<MoviesResponse> call = apiService.getTopRatedMovies(API_KEY); 
     call.enqueue(new Callback<MoviesResponse>() { 
      @Override 
      public void onResponse(Call<MoviesResponse> call, Response<MoviesResponse> response) { 
       int statusCode = response.code(); 
       movies = response.body().getResults(); 
       //recyclerView.setAdapter(new MoviesAdapter(movies, R.layout.list_item_movie, context)); 

      } 

      @Override 
      public void onFailure(Call<MoviesResponse> call, Throwable t) { 
       // Log error here since request failed 
       Log.e(TAG, t.toString()); 
      } 
     }); 

我試圖插入「電影」列表內從API的所有數據,但問題是,我得到的是說T時的誤差這個'電影'列表來自android.graphics.Movie而不是我的班級項目。我不知道爲什麼。我怎樣才能解決這個問題?

編輯:

錯誤消息

Executing tasks: [:app:assembleDebug] 

Configuration 'compile' in project ':app' is deprecated. Use 'implementation' instead. 
:app:buildInfoDebugLoader 
:app:preBuild UP-TO-DATE 
:app:preDebugBuild UP-TO-DATE 
:app:compileDebugAidl UP-TO-DATE 
:app:compileDebugRenderscript UP-TO-DATE 
:app:checkDebugManifest UP-TO-DATE 
:app:generateDebugBuildConfig UP-TO-DATE 
:app:generateDebugResValues UP-TO-DATE 
:app:generateDebugResources UP-TO-DATE 
:app:mergeDebugResources UP-TO-DATE 
:app:createDebugCompatibleScreenManifests UP-TO-DATE 
:app:processDebugManifest UP-TO-DATE 
:app:splitsDiscoveryTaskDebug UP-TO-DATE 
:app:processDebugResources UP-TO-DATE 
:app:generateDebugSources 
:app:javaPreCompileDebug UP-TO-DATE 
:app:compileDebugJavaWithJavac 
/Users/Mvaguimaraes/Documents/Android Projects/MusicAppCardView/app/src/main/java/com/example/mvaguimaraes/musicappcardview/MainActivity.java:86: error: incompatible types: List<Movie> cannot be converted to List<Item> 
       movies = response.body().getResults(); 
               ^
1 error 

FAILED 
:app:buildInfoGeneratorDebug 

FAILURE: Build failed with an exception. 

* What went wrong: 
Execution failed for task ':app:compileDebugJavaWithJavac'. 
> Compilation failed; see the compiler error output for details. 

* Try: 
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. 

* Get more help at https://help.gradle.org 

BUILD FAILED in 17s 

15 actionable tasks: 3 executed, 12 up-to-date 
+0

如果可能,請附上您的logcat – Mandy8055

+0

感謝您的回答。該項目無法構建以顯示logcat中的錯誤,但我使用Gradle控制檯中的擴展錯誤消息更新了該問題。 –

回答

2

這意味着,你在什麼地方進口了錯誤的Movie類。

檢查你的改造服務,如果你列入

import android.graphics.Movie; 

,而不是某個地方自己的模型導入。

仔細檢查您的改造服務ApiInterface,如果你已經列表或只是一個普通的列表作爲返回值,而不是

List<Item> 

這意味着確保它像

@GET("your/path") 
Call<List<Item>> listItems(); 

,而不是

@GET("your/path") 
Call<List> listItems(); 

@GET("your/path") 
Call<List<Movie>> listItems(); 
+0

感謝您的幫助,顯然AndroidStudio會自動將MovieResponse類中的android.graphics.Movie導入到我的Item類(以前稱爲Movie)中。 –

+0

這導致了MainActivity中的錯誤。 –

+1

常見問題;-)作爲開發人員,您經常會遇到這種情況。在這種情況下請相信我;) –

相關問題