由于Retrofit enqueue()方法不完整,在创建ArrayAdapter时崩溃

时间:2018-09-13 21:03:52

标签: android

我的Android示例应用程序存在问题。这是Activity类代码的两种变体,其中一种会崩溃,而另一种则不会,我不明白为什么。在以下情况下,程序可以正常运行:

public class MainActivity extends AppCompatActivity {
@Inject
Retrofit retrofit;

@BindView(R.id.lvMain)
ListView lvMain;

@BindView(R.id.tvWelcome)
TextView tvWelcome;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ButterKnife.bind(this);
    ((RetrofitlearningApp) getApplication()).getGlobalConfigComponent().inject(this);

    tvWelcome.setGravity(Gravity.CENTER);

    HeroesApi heroesApi = retrofit.create(HeroesApi.class);
    Call<List<Hero>> call = heroesApi.getAll();
    call.enqueue(new HeroesCallback());
}

private List<String> getHeroNames(List<Hero> heroes) {
    return heroes.stream()
            .map(Hero::getName)
            .collect(Collectors.toList());
}

private class HeroesCallback implements Callback<List<Hero>> {

    @Override
    public void onResponse(Call<List<Hero>> call, Response<List<Hero>> response) {
        if (!(response.body() == null || response.body().isEmpty())) {
            List<Hero> heroes = response.body();

            ArrayAdapter<String> adapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, getHeroNames(heroes));
            lvMain.setAdapter(adapter);
        } else {
            tvWelcome.setText("No heroes found");
            Toast.makeText(getApplicationContext(), "No heroes found", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onFailure(Call<List<Hero>> call, Throwable t) {
        Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
        tvWelcome.setText("No access to Valhalla");
    }
}

}

但是在此示例中,尽管调用了enqueue()方法并且response.body()包含一个列表,但似乎从未调用过onResponse(),并且字段“ heroes”仍然为null并导致程序崩溃:

public class MainActivity extends AppCompatActivity {
@Inject
Retrofit retrofit;

@BindView(R.id.lvMain)
ListView lvMain;

@BindView(R.id.tvWelcome)
TextView tvWelcome;

private List<Hero> heroes;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ButterKnife.bind(this);
    ((RetrofitlearningApp) getApplication()).getGlobalConfigComponent().inject(this);

    tvWelcome.setGravity(Gravity.CENTER);

    HeroesApi heroesApi = retrofit.create(HeroesApi.class);
    Call<List<Hero>> call = heroesApi.getAll();
    call.enqueue(new HeroesCallback());

    ArrayAdapter<String> adapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, getHeroNames(heroes));
    lvMain.setAdapter(adapter);
}

private List<String> getHeroNames(List<Hero> heroes) {
    return heroes.stream()
            .map(Hero::getName)
            .collect(Collectors.toList());
}

private class HeroesCallback implements Callback<List<Hero>> {

    @Override
    public void onResponse(Call<List<Hero>> call, Response<List<Hero>> response) {
        if (!(response.body() == null || response.body().isEmpty())) {
            heroes = response.body();
        } else {
            tvWelcome.setText("No heroes found");
            Toast.makeText(getApplicationContext(), "No heroes found", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onFailure(Call<List<Hero>> call, Throwable t) {
        Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
        tvWelcome.setText("No access to Valhalla");
    }
}

}

请您解释一下这种现象吗?

0 个答案:

没有答案