2017-05-07 5 views
1

Ich habe Probleme mit diesem Fehler für den Aufruf von UpdateUI von Hauptaktivität, MoviesService und LiveMovieServices. Ich versuche Film appAktivität kann nicht gestartet werden ComponentInfo {Projekt} java.lang.IndexOutOfBoundsException: Index:

den Fehler zu machen:

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)  

Die Hauptaktivität

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; 

} 

}

Die MovieServices

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

}

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

}

Antwort

0
movies = new ArrayList<>(); 

Hier legen Sie eine leere ArrayList-movies zuweisen.

Dann, drei Java-Anweisungen später, rufen Sie movies.get(0). Da movies leer ist, können Sie kein get() Objekt aus movies.

0

Sie versuchen, das erste Element der leeren Arraylist, ohne warten Antwort von webservice zu bekommen.

Sie sollten für die Antwort von Ihrem Web-Service warten Ihre Arraylist zu füllen. Danach können Sie diese Methode aufrufen (wenn die Liste nicht leer ist).

UpdateUI(movies.get(0)); 
0

Filme leer ist, so kann man nicht movies.get (0)

+0

Ja, die Filme sind leer, aber ich habe für Schleife in LiveMovieServices getan, um die arbeitet, um sicherzustellen, –

Verwandte Themen