如何从适配器中解决此意向崩溃问题?

时间:2019-06-03 09:24:41

标签: java android nullpointerexception

我试图从单击的适配器列表中启动一个活动,并发送信息以显示在新活动的片段中,但是它崩溃了。

我试图使setTitle和其他方法@nullable,但是没有用

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder viewHolder, int position) {

        Article currentArticle = articles.get(position);
        Log.e("article", currentArticle.getTitle());
        String pubDateString;
        try {
            String sourceDateString = currentArticle.getPubDate();

            SimpleDateFormat sourceSdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.ENGLISH);
            Date date = sourceSdf.parse(sourceDateString);

            SimpleDateFormat sdf = new SimpleDateFormat("dd MMMM yyyy", Locale.getDefault());
            pubDateString = sdf.format(date);

        } catch (ParseException e) {
            e.printStackTrace();
            pubDateString = currentArticle.getPubDate();
        }

        viewHolder.title.setText(currentArticle.getTitle());

        Picasso.get()
                .load(currentArticle.getImage())
                .placeholder(R.drawable.placeholder)
                .into(viewHolder.image);

        viewHolder.pubDate.setText(pubDateString);

        StringBuilder categories = new StringBuilder();
        for (int i = 0; i < currentArticle.getCategories().size(); i++) {
            if (i == currentArticle.getCategories().size() - 1) {
                categories.append(currentArticle.getCategories().get(i));
            } else {
                categories.append(currentArticle.getCategories().get(i)).append(", ");
            }
        }

        viewHolder.category.setText(categories.toString());

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {

            @SuppressLint("SetJavaScriptEnabled")
            @Override
            public void onClick(View view) {
                //show article content inside a dialog
                articleView = new WebView(mContext);

                articleView.getSettings().setLoadWithOverviewMode(true);

                String title = articles.get(viewHolder.getAdapterPosition()).getTitle();
                String content = articles.get(viewHolder.getAdapterPosition()).getContent();

                articleView.getSettings().setJavaScriptEnabled(true);
                articleView.setHorizontalScrollBarEnabled(false);
                articleView.setWebChromeClient(new WebChromeClient());
                articleView.loadDataWithBaseURL(null, "<style>img{display: inline; height: auto; max-width: 100%;} " +

                        "</style>\n" + "<style>iframe{ height: auto; width: auto;}" + "</style>\n" + content, null, "utf-8", null);

                mDetailFragment.setTitle(currentArticle.getTitle());
                mDetailFragment.setAuthor(currentArticle.getAuthor());
                mDetailFragment.setPubDate(currentArticle.getPubDate());
                mDetailFragment.setDescription(currentArticle.getDescription());
                mDetailFragment.setImage(currentArticle.getImage());
                mDetailFragment.setContent(currentArticle.getContent());
                mDetailFragment.setLink(currentArticle.getLink());
                mContext.startActivity(new Intent(mContext,DetailActivity.class));
            }
        });
    }

这是我的logcat

  

E / Android运行时:致命异常:主要       流程:com.example.rssreader,PID:20126       java.lang.NullPointerException:尝试调用虚拟方法'void   com.example.rssreader.Controllers.Fragments.DetailFragment.setTitle(java.lang.String)'   在空对象引用上           在com.example.rssreader.Adapters.ArticleAdapter $ 1.onClick(ArticleAdapter.java:141)           在android.view.View.performClick(View.java:6294)           在android.view.View $ PerformClick.run(View.java:24770)           在android.os.Handler.handleCallback(Handler.java:790)           在android.os.Handler.dispatchMessage(Handler.java:99)           在android.os.Looper.loop(Looper.java:164)           在android.app.ActivityThread.main(ActivityThread.java:6494)           在java.lang.reflect.Method.invoke(本机方法)           在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:438)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

这是我的详细信息片段:     公共类DetailFragment扩展了Fragment {

    @BindView(R.id.detail_title) TextView title;
    @BindView(R.id.detail_author) TextView author;
    @BindView(R.id.detail_date) TextView pubDate;
    @BindView(R.id.detail_description) TextView description;
    @BindView(R.id.detail_image) TextView image;
    @BindView(R.id.detail_content) TextView content;
    @BindView(R.id.detail_link) TextView link;

    public DetailFragment() {   }

    public static DetailFragment newInstance () {
        return (new DetailFragment());
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view= inflater.inflate(R.layout.fragment_detail, container, false);

        ButterKnife.bind(this, view);
        return view;

    }
    public void setTitle(String s){ title.setText(s); }
    public void setAuthor(String s){
        author.setText(s);
    }
    public void setPubDate(String s){
        pubDate.setText(s);
    }
    public void setDescription(String s){
        description.setText(s);
    }
    public void setImage(String s){
        image.setText(s);
    }
    public void setContent(String s){ content.setText(s); }
    public void setLink(String s){
        link.setText(s);
    }

}

2 个答案:

答案 0 :(得分:0)

您的 watch: { x(){ this.updateX() }, y(){ this.updateY() } } 为空,这就是mDetailFragment的原因。为什么还要在适配器NullPointerException中处理mDetailFragment

您可以创建一个Intent,用现在在onBindViewHolder中设置的值填充它,然后mDetailFragment将处理您的DetailActivity

Fragment

这样,您在启动Intent i = new Intent(mContext, DetailActivity.class); i.putExtra("setTitle", currentArticle.getTitle()); i.putExtra("setAuthor", currentArticle.getAuthor()); startActivity(i); 时会将所有信息发送到DetailActivity

您可以像以下示例一样使用它:

DetailActivity

答案 1 :(得分:0)

您的应用在以下行崩溃,因为您没有初始化DetailFragment对象

  // initialize here mDetailFragment object
  DetailFragment mDetailFragment=new DetailFragment();
  if(currentArticle.getTitle()!=null && !TextUtils.isEmpty(currentArticle.getTitle())){
 mDetailFragment.setTitle(currentArticle.getTitle());
}