java - 无法启动 Activity ComponentInfo{project} java.lang.IndexOutOfBoundsException : Index:

标签 java android movie

我在从主 Activity 、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) 

主要 Activity

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);
}

}

最佳答案

movies = new ArrayList<>();

在这里,您将一个空的 ArrayList 分配给 movies

然后,在三个 Java 语句之后,您调用 movies.get(0)。由于 movies 为空,因此您无法从 moviesget() 对象。

关于java - 无法启动 Activity ComponentInfo{project} java.lang.IndexOutOfBoundsException : Index:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43832218/

相关文章:

java - 如何在Java中进行.MOV到3GP的转换操作

Java Swing 为游戏添加组件

java - 中断 JMS MessageConsumer.receive() 后如何终止 "ActiveMQ Transport"线程

java - Socket.getInputStream().read(byte[]) 是否保证在至少读取一些数据后不会阻塞?

java - canvas.drawBitmap() 不缩放图像

android - 在 Kotlin 中使用 "when"语句时,如何针对每种情况调用多个函数?

android - 从 Android 应用程序以无线方式向 Mac 发送按键命令

android - 如果记录不存在于 LiveData 和 Room 会怎样?

iphone - 如何在iOS中播放电影片段

打印警告 : EOF timestamp not reliable 后 FFmpeg 卡住