我从主要活动,MoviesService和LiveMovieServices调用UpdateUI时遇到此错误。我正在尝试制作电影应用
错误:
05-07 15:30:07.956 3694-3694/com.youssefz.beastmovies.live E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.youssefz.beastmovies.live, PID: 3694
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.youssefz.beastmovies.live/com.example.youssefz.beastmovies.activities.MainActivity}: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.get(ArrayList.java:411)
at com.example.youssefz.beastmovies.activities.MainActivity.onCreate(MainActivity.java:55)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
主要活动
public class MainActivity extends BaseActivity {
@BindView(R.id.activity_main_progressBar)
ProgressBar progressBar;
@BindView(R.id.activity_main_movie_summary)
TextView movieSummary;
@BindView(R.id.activity_main_movieTitle)
TextView movieTitle;
@BindView(R.id.activity_main_movie_vote)
TextView movieVote;
@BindView(R.id.activity_main_moviePicture)
ImageView moviePicture;
@BindView(R.id.activity_main_movieReleaseDate)
TextView movieReleaseDate;
@BindView(R.id.activity_main_left_arrow)
ImageView leftArrow;
@BindView(R.id.activity_main_right_arrow)
ImageView rightArrow;
ArrayList<Movie> movies;
int index;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
movies = new ArrayList<>();
index=0;
bus.post(new MovieServices.SearchMoviesRequest("query"));
UpdateUI(movies.get(0));
}
private void UpdateUI(Movie movie) {
progressBar.setVisibility(View.VISIBLE);
movieTitle.setText(movie.getMovieTitle());
movieSummary.setText(movie.getMovieSummary());
movieReleaseDate.setText(movie.getMovieSummary());
movieVote.setText(Double.toString(movie.getMovieRating()));
Log.i(MainActivity.class.getSimpleName(),movie.getMoviePicture());
Picasso.with(this).load(movie.getMoviePicture())
.fit()
.centerCrop()
.into(moviePicture, new Callback() {
@Override
public void onSuccess() {
progressBar.setVisibility(View.GONE);
}
@Override
public void onError() {
}
});
}
@OnClick(R.id.activity_main_left_arrow)
public void setUpLeftArrow() {
if(index == 0){
Toast.makeText(this,"This is the start of the movies!",Toast.LENGTH_LONG).show();
}else{
index --;
UpdateUI(movies.get(index));
}
}
@OnClick(R.id.activity_main_right_arrow)
public void setUpRightArrow() {
if(index == movies.size()-1){
Toast.makeText(this,"This is the end of the movies!",Toast.LENGTH_LONG).show();
}else{
index ++;
UpdateUI(movies.get(index));
}
}
@Subscribe
public void getMovieMessage(MovieServices.SearchMoviesResponse response) {
movies.clear();
movies = response.movies;
}
}
电影服务
public class MovieServices {
private MovieServices(){
}
public static class SearchMoviesRequest{
public String query;
public SearchMoviesRequest (String query){
this.query = query;
}
}
public static class SearchMoviesResponse{
public ArrayList<Movie> movies;
}
}
LiveMovieService:
public class LiveMovieService extends BaseLiveServices {
public LiveMovieService(BeastMoviesApplication application) {
super(application);
}
@Subscribe
public void getMovieMessage(MovieServices.SearchMoviesRequest request){
MovieServices.SearchMoviesResponse response = new MovieServices.SearchMoviesResponse();
response.movies = new ArrayList<>();
for(int i=0;i<4;i++){
Movie movie = new Movie ("Joe's android movie" + i+ "!", "The is a movie where Joe has Android Dreams" + i, BeastMoviesApplication.BESE_PICTURE_URL+"/wSJPjqp2AZWQ6REaqkMuXsCIs64.jpg","10/3/2016",5.0);
response.movies.add(movie);
}
bus.post(response);
}
}
答案 0 :(得分:0)
movies = new ArrayList<>();
在此,您将空ArrayList
分配给movies
。
然后,三个Java语句稍后,您调用movies.get(0)
。由于movies
为空,因此您无法get()
movies
中的对象。
答案 1 :(得分:0)
您正试图获取空 ArrayList 的第一项,而无需等待 webservice 中的响应。
您应该等待来自网络服务的响应来填充 ArrayList 。之后,您可以调用此方法(如果列表不为空)。
UpdateUI(movies.get(0));
答案 2 :(得分:0)
电影是空的,因此您无法使用movies.get(0)