android emulator showing empty screen?

2019-08-20 11:50发布

问题:

I am developing a news app previously it was showing JSON response but now showing the empty white screen below fragment class where I have implemented dagger 2 and retrofit'

public class BBCSportFragment extends Fragment implements ArticleAdapter.ClickListener {

    public List<Article> articleList = new ArrayList<Article>();
    @ActivityContext
    public Context activityContext;
    @ApplicationContext
    public Context mContext;

    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    BBCSportFragmentComponent bbcSportFragmentComponent;
    BBCFragmentContextModule bbcFragmentContextModule;
    private SportNews sportNews;
    private ArticleAdapter articleAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_bbcsport, container, false);
        ButterKnife.bind(this, view);
        SportInterface sportInterface = SportClient.getApiService();
        Call<SportNews> call = sportInterface.getArticles();
        call.enqueue(new Callback<SportNews>() {
            @Override
            public void onResponse(Call<SportNews> call, Response<SportNews> response) {
                sportNews = response.body();
                if (sportNews != null && sportNews.getArticles() != null) {
                    articleList.addAll(sportNews.getArticles());
                }
                articleAdapter = new ArticleAdapter(articleList, sportNews);
                ApplicationComponent applicationComponent = MyApplication.get(Objects.requireNonNull(getActivity())).getApplicationComponent();
                bbcSportFragmentComponent = (BBCSportFragmentComponent) DaggerApplicationComponent.builder().contextModule(new ContextModule(getContext())).build();
                bbcSportFragmentComponent.injectBBCSportFragment(BBCSportFragment.this);

                RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
                recyclerView.setLayoutManager(layoutManager);
                recyclerView.setAdapter(articleAdapter);
            }

            @Override
            public void onFailure(Call<SportNews> call, Throwable t) {

            }
        });


        return view;


    } 
 }

below adapter class

public class ArticleAdapter extends RecyclerView.Adapter<ArticleAdapter.CustomViewHolder> {


    public static final String urlKey = "urlKey";
    public static final String imageKey = "imageKey";
    public ArticleAdapter.ClickListener listener;
    Context context;
    private List<Article> articles;


    public ArticleAdapter(List<Article> articles, SportNews sportNews) {
        this.articles = articles;
        this.listener = listener;

    }

    public ArticleAdapter(ArticleAdapter.ClickListener clickListener) {
    }

    @NonNull
    @Override
    public CustomViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View itemView = (View) LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.article_list, null);
        return new CustomViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull CustomViewHolder customViewHolder, int position) {
        Article article = articles.get(position);
        customViewHolder.articleAuthor.setText(article.getAuthor());
        customViewHolder.articleTitle.setText(article.getTitle());
        Picasso.get().load(article.getUrlToImage()).into(customViewHolder.articleImage);
        customViewHolder.itemView.setOnClickListener(v -> {
            Intent intent = new Intent(v.getContext(), DetailActivity.class);

            intent.putExtra("urlKey", article.getUrl());
            intent.putExtra("imageKey", article.getUrlToImage());

            v.getContext().startActivity(intent);
        });


    }


    @Override
    public int getItemCount() {
        if (articles == null) return 0;
        return articles.size();
    }

    public interface ClickListener {
    }

    public class CustomViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.articleAuthor)
        TextView articleAuthor;
        @BindView(R.id.articleTitle)
        TextView articleTitle;
        @BindView(R.id.articleImage)
        ImageView articleImage;


        public CustomViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);


        }
    }

}

below app.gradle

apply plugin: 'com.android.application' android {
    compileOptions.incremental = false
    packagingOptions {
        exclude 'META-INF/proguard/androidx-annotations.pro'
    }
    configurations {
        all*.exclude group: 'com.android.support', module: 'support-v4'
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    compileSdkVersion 28
    defaultConfig {
        applicationId "edgar.yodgorbek.sportnews"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    } } dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    //noinspection GradleCompatible
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'com.google.code.gson:gson:2.8.2'
    implementation 'com.squareup.retrofit2:retrofit:2.3.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
    //noinspection GradleCompatible
    implementation 'com.android.support:design:28.0.0'
    implementation 'com.squareup.picasso:picasso:2.71828'
    implementation 'com.jakewharton:butterknife:10.1.0'
    annotationProcessor 'com.jakewharton:butterknife-compiler:10.1.0'
    implementation 'com.google.dagger:dagger:2.15'
    annotationProcessor 'com.google.dagger:dagger-compiler:2.15'
    implementation group: 'com.squareup.okhttp3', name: 'okhttp', version: '3.10.0'
    implementation group: 'com.squareup.okhttp3', name: 'logging-interceptor', version: '3.9.0'

回答1:

Regarding Dagger setup, this line of cast does not seem correct:

// BBCSportFragment.onCreate > Callback.onResponse
(BBCSportFragmentComponent) DaggerApplicationComponent.builder().contextModule(new ContextModule(getContext())).build();

A correct setup should not require you to cast the result of .build().

If BBCSportFragmentComponent is using component dependency (with an annotation like @Component(dependencies = ApplicationComponent.class)), maybe you mean this:

bbcSportFragmentComponent =
    DaggerBBCSportFragmentComponent.builder()
        .applicationComponent(applicationComponent)
        .contextModule(new ContextModule(getContext()))
        .build();
bbcSportFragmentComponent.injectBBCSportFragment(BBCSportFragment.this);

Then, the fields that Dagger should inject should be annotated @Inject. Otherwise bbcSportFragmentComponent.injectBBCSportFragment will run successfully doing nothing.

Then, Dagger documentation suggests injecting a Fragment inside onAttach before super.onAttach().