1

in my app, i have a TabLayout that every tab is a separate fragment.
I open dialog in one of this fragment.
in my dialog contain a list that I want when I clicked to list item, The content of item pass to in textView in A number of fragment

public class ReadingFragment extends Fragment{
   TextView textView;
   View view;
   FloatingActionButton actionButton;

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

       textView = view.findViewById(R.id.txtSaved);
       actionButton = view.findViewById(R.id.floatingActionButton);

        actionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DialogSearch search = new DialogSearch();
                search.show(getActivity().getSupportFragmentManager(),"baranAbFa");
        }
    });

  }

    public void setData(String x){
       textView.setText(x);
   }
}  

This MainActivity Code(the method that received data from Dialog and pass to Fragment ):

public class MainActivity extends AppCompatActivity{

     public void setGetAndSend(String name){
        Fragment frags= viewPagerAdapter.getItem(2);
        ((ReadingFragment) frags).setData(name);
        Toast.makeText(MainActivity.this, name, Toast.LENGTH_SHORT).show();
    }
}

And this in My Dialog Class And method that handle ItemClickListener:
(strings is a list that displays in list View)

public class DialogSearch extends DialogFragment {

   View dialogView;

   @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
    super.onCreateDialog(savedInstanceState);

    dialogView = getActivity().getLayoutInflater().inflate(R.layout.search_dialog, new RelativeLayout(getContext()), false);

    final Dialog builder = new Dialog(getActivity());

    listViewSearch.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                    ((MainActivity)getActivity()).setGetAndSend(strings.get(i));

                }
            });
    }
}

And when run and click to list item this error is occured: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
also View in ReadingFragment too is null

Jaymin
  • 2,879
  • 3
  • 19
  • 35
Amir2511
  • 37
  • 7

0 Answers0