-1

I have a Fragment Activity i need to set Text to the text view in that Activity,when i am using following code getting null pointer exception. can any other approach is there to set data for text view .

public class Introduction extends Fragment  {
     TextView txt;
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
            // Inflate the layout for this fragment

         View v= inflater.inflate(R.layout.activity_introduction, container, false);
         txt=(TextView) v.findViewById(R.id.officeheader);
         return v;

        }
    public void setText(String text){

        txt.setText(text);
    }
    }

and in My Fragment Activity class :

frgManager=getSupportFragmentManager();
         frgManager.beginTransaction()

        . replace(R.id.fragmentoffice, introduction,"introduction")
         .commit();

        introduction.setText(" Office Coffee that will \n have everyone talking ");
vinod kumar
  • 59
  • 1
  • 12

2 Answers2

2

set string in bundle

Bundle bundl = new Bundle();
bundl.putString("Text", text);
introduction.setArguments(bundl)
frgManager=getSupportFragmentManager();
frgManager.beginTransaction()
. replace(R.id.fragmentoffice, introduction,"introduction")
.commit();

get text from bundle

public class Introduction extends Fragment  {
     TextView txt;
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
            // Inflate the layout for this fragment
String str = getArguments().getString("Text");
         View v= inflater.inflate(R.layout.activity_introduction, container, false);
         txt=(TextView) v.findViewById(R.id.officeheader);
         setText(str);
         return v;

        }
    public void setText(String text){

        txt.setText(text);
    }
    }
rahul.ramanujam
  • 5,608
  • 7
  • 34
  • 56
-1

you can write introduction constructor ,which contain a parameter Type String , and on the onCreateView ,you can initialize the textivew and settext

jackGao
  • 32
  • 3