Android 如何将数据从片段(包含recyclerview)传输到对话框片段,该对话框片段显示对话框片段上的选定项

Android 如何将数据从片段(包含recyclerview)传输到对话框片段,该对话框片段显示对话框片段上的选定项,android,android-fragments,Android,Android Fragments,如何将数据从片段(包含recyclerview)传输到对话框片段,该对话框片段显示对话框片段上的选定项,在我的情况下,我遇到了错误 这是RecycleServiceCollegeNewsletter.java public class RecyclerViewCollegeNewsLetter extends RecyclerView.Adapter<RecyclerViewCollegeNewsLetter.MyCollege> { static List<Topics&g

如何将数据从片段(包含recyclerview)传输到对话框片段,该对话框片段显示对话框片段上的选定项,在我的情况下,我遇到了错误

这是RecycleServiceCollegeNewsletter.java

public class RecyclerViewCollegeNewsLetter extends RecyclerView.Adapter<RecyclerViewCollegeNewsLetter.MyCollege> {

static List<Topics> topicses = new ArrayList<>();
Context context;
Topics topics;


public RecyclerViewCollegeNewsLetter(Context context, ArrayList<Topics> topicses) {
    this.context = context;
    this.topicses = topicses;
}

@Override
public RecyclerViewCollegeNewsLetter.MyCollege onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_row, parent, false);
    Log.d("ssss", "in oncreate");


    return new MyCollege(view);
}

@Override
public void onBindViewHolder(final RecyclerViewCollegeNewsLetter.MyCollege holder, final int position) {
    topics = topicses.get(position);
    holder.id.setText(topics.getId());
    holder.givenby.setText(topics.getGivenby());
    holder.article.setText(topics.getArticle());

    holder.more.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            FilterDialogFragment filterDialogFragment = new FilterDialogFragment();
            int pos=holder.getAdapterPosition();
            Bundle bundle = new Bundle();
            bundle.putString("more_id",topicses.get(pos).getId() );
            Log.e("rrrr",topicses.get(pos).getId());
            bundle.putString("more_given_by",topicses.get(pos).getGivenby());
            bundle.putString("more_article", topicses.get(pos).getArticle());
            bundle.putString("more_content", topicses.get(pos).getContent());

            //pasing data to filterDialogFragment
            filterDialogFragment.setArguments(bundle);


            showDialog();
        }
    });
    // holder.content.setText(topics.getContent());

    if (topics.getContent().length() > 100) {
        holder.content.setText((topics.getContent().substring(0, 99)) + "...");
    } else
        holder.content.setText(topics.getContent());

}

@Override
public int getItemCount() {
    return topicses.size();
}

public class MyCollege extends RecyclerView.ViewHolder {
    TextView id, content, article, givenby;
    Button more;

    public MyCollege(View itemView) {
        super(itemView);
        id = (TextView) itemView.findViewById(R.id.id);
        givenby = (TextView) itemView.findViewById(R.id.givenby);
        article = (TextView) itemView.findViewById(R.id.article);
        content = (TextView) itemView.findViewById(R.id.content);
        more = (Button) itemView.findViewById(R.id.more);

    }

}
void showDialog() {
    FilterDialogFragment filterDialogFragment = new FilterDialogFragment();
   // MoreDetails moreDetails = new MoreDetails();
    FragmentManager ft = ((MainActivity) context).getSupportFragmentManager();
    DialogFragment newFragment = filterDialogFragment.newInstance();
    newFragment.show(ft, "dialog");
 }
我把Bundle mArgs=getArguments()放进去

more_id.setText(mArgs.getString(“more_id”)

在onActivityCreated中,但显示错误:-

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
                                                                                 at com.example.kulde.itechvcet.FilterDialogFragment.onViewCreated(FilterDialogFragment.java:52)
                                                                                 at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1314)
                                                                                 at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
                                                                                 at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)
                                                                                 at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:758)
                                                                                 at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2363)
                                                                                 at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2149)
                                                                                 at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2103)
                                                                                 at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2013)
                                                                                 at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:710)
                                                                                 at android.os.Handler.handleCallback(Handler.java:739)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                 at android.os.Looper.loop(Looper.java:152)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5497)
                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

帮帮我,专业开发人员

选中此项并在FilterDialogFragment.java中使用

  public Bundle getBundle = null;
getBundle = this.getIntent().getExtras(); 
  String id= getBundle.getString("more_id");
more_id.setText(id);

您正在为FilterDialogFragment设置捆绑包值,但在showDialog()方法中创建FilterDialogFragment的新实例时未使用它

传递FilterDialogFragment变量

void showDialog(FilterDialogFragment ff) {
   // MoreDetails moreDetails = new MoreDetails();
    FragmentManager ft = ((MainActivity) context).getSupportFragmentManager();
    DialogFragment newFragment = ff.newInstance();
    newFragment.show(ft, "dialog");
 }
您没有向DialogFragment传递任何值,也没有尝试检索实际上不存在的
more\u id
键的值

Bundle mArgs = getArguments();
more_id.setText(mArgs.getString("more_id"));
如下更改您的实现

static FilterDialogFragment newInstance(String moreId) {
    FilterDialogFragment f = new FilterDialogFragment();
    Bundle bundle = new Bundle();
    bundle.putSTring("more_id", moreId);
    f.setArguments(bundle);
    return f;
}

并相应地在调用DialogFragment时更改代码

在将存储的数据放入bundle时进行调试,但我没有使用intent传输数据。bundle bundle=new bundle();bundle.putString(“more_id”,topicses.get(pos.getId());使用java.lang.NullPointerException查看此行:尝试在com.example.kulde.itechvcet.FilterDialogFragment.onViewCreated上的空对象引用上调用虚拟方法“java.lang.String android.os.Bundle.getString(java.lang.String)”(FilterDialogFragment.java:55)将调试点放入bundle.putString(“more_id”,topicses.get(pos.getId());并检查正在存储的数据,我已经以相同的方式执行了:-void
showDialog(){FilterDialogFragment FilterDialogFragment=new FilterDialogFragment();//MoreDetails MoreDetails=new MoreDetails();FragmentManager ft=((MainActivity)上下文)。getSupportFragmentManager();DialogFragment newFragment=filterDialogFragment.newInstance();newFragment.show(ft,“dialog”);}
静态filterDialogFragment newInstance(){FilterDialogFragment f=new FilterDialogFragment();return f;}
您没有向片段传递更多的id值,这就是它给出null指针异常的原因。getArguments();是非静态方法。您不能应用于静态方法使用公共静态FilterDialogFragment newInstance(String moreId)
Bundle mArgs = getArguments();
more_id.setText(mArgs.getString("more_id"));
static FilterDialogFragment newInstance(String moreId) {
    FilterDialogFragment f = new FilterDialogFragment();
    Bundle bundle = new Bundle();
    bundle.putSTring("more_id", moreId);
    f.setArguments(bundle);
    return f;
}