android模拟器显示空白屏幕?

时间:2019-04-21 07:19:58

标签: android android-recyclerview dagger-2

我以前正在开发一个新闻应用程序,它显示JSON响应,但现在在片段类下面显示了空白屏幕,在其中我实现了dagger 2并进行了改装'

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;


    } 
 }

在适配器类之下

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


        }
    }

}

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 个答案:

答案 0 :(得分:0)

关于Dagger设置,这一行似乎不正确:

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

正确的设置不需要您强制转换.build()的结果。

如果BBCSportFragmentComponent使用组件依赖项(带有@Component(dependencies = ApplicationComponent.class)之类的注释),也许您的意思是:

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

然后,应将Dagger应该注入的字段注释为@Inject。否则bbcSportFragmentComponent.injectBBCSportFragment将成功执行任何操作。

然后,Dagger文档建议使用injecting a Fragment inside onAttach before super.onAttach()