android模拟器显示空白屏幕? [英] android emulator showing empty screen?

查看:140
本文介绍了android模拟器显示空白屏幕?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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;


    } 
 }

在适配器类之下

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'

推荐答案

关于Dagger设置,这一行强制转换似乎不正确:

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

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

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

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

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

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

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

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

然后,Dagger文档建议super.onAttach()之前在onAttach内部插入一个片段> .

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

这篇关于android模拟器显示空白屏幕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆