This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 4 years ago.
i have started to learn android recently and i was looking at a fragment example in a video tutorial
in the tutorial they haven't made the textview static but when i tried to implement fragment with some changes in the code i started to get null pointer exception my code looks like this
public class BlankFragment2 extends Fragment {
TextView tv;
public BlankFragment2() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_blank_fragment2, container, false);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
tv= (TextView) getActivity().findViewById(R.id.txt2);
}
public void setName(String name){
//tv=(TextView)getActivity().findViewById(R.id.txt2);
if(tv == null){
System.out.println("tv is null");
}
tv.setText("Hi "+name);
}
}
i am getting null pointer exception at settext unless i make the textview static can someone please explain why
this is not duplicate of what is NPE i know what NPE is and why it shows up my question here is that after initializing the textview in onActivityCreated why it is null again in setname function
Not getActivity().findViewById(), it must be view.findViewById() because Activity.findViewById() will look for a View in the Activity layout, whereas view.findViewById() will look for the View in the Fragment layout.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View inflatedView = inflater.inflate(R.layout.fragment_blank_fragment2, container, false);
return inflatedView;
}
And in onViewCreated you can initialise your TextView
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// here goes your intialisation using view
tv= (TextView) view.findViewById(R.id.txt2);
}
Make sure you have a TextView with the id txt2 in fragment_blank_fragment2.xml
NullPointerException is thrown when an application attempts to use an
object reference that has the null value.
FYI
You should return VIEW's object
Check findViewById(R.id.txt2 . Make sure you this ID present in XML.
Don't
tv= (TextView) getActivity().findViewById(R.id.txt2);
Do
tv= (TextView) view.findViewById(R.id.txt2);
Try this way
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.fragment_blank_fragment2, container, false);
tv= (TextView) view.findViewById(R.id.txt2);
return view;
}
Sorry guys it was a stupid mistake on my side,
in my mainactivity i wrote
BlankFragment2 frag2 = new BlankFragment2();
frag2.setName(name);
i corrected it by writing
BlankFragment2 frag2=(BlankFragment2)getFragmentManager().findFragmentById(R.id.frag2);
fragb.setName(name);
now it is working
Related
i was working with a NavigationDrawer for a menu and there is a fragment for every item on the menu for example i have a home menu which have a homefragemnt.xml and a homefragment.java so in the xml file file there is a textview i wanted to change that textviews values using the MainActivity.java
and used the following code:TextView t1 = (TextView) findViewById(R.id.changetxt);t1.setText("ChangedText");
changetxt is the id of the TextView in the homefragment.xml file
but this code give a runtime error saying Attempt to invoke virtual method on a null object reference.
What is causing this problem and how do i solve it?
You will have to get the reference of textview from the fragment view not activity.
The view which you inflated in the onCreateView of fragment.
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.myview,container,false);
TextView t1 = (TextView) view.findViewById(R.id.changetxt);t1.setText("ChangedText");
return view;
}
here view is the View that the fragment is initaialised with.
this link will help you
Fragment :
public class MyFragment extends Fragment {
TextView t1;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_main, container, false);
t1 = (TextView) view.findViewById(R.id.changetxt);
return view;
}
public void setTextViewText(String value){
t1.setText(value);
}
}
Activity :
myFragment.setTextView("ChangedText");
Hope it helps you.
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 ");
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);
}
}
you can write introduction constructor ,which contain a parameter Type String ,
and on the onCreateView ,you can initialize the textivew and settext
I'm doing a Navigation Drawer app. I have all the cases working, I mean, when in touch a section the app goes to the right View. But when I want to start coding I don't know how to use methods inside the View. I think my code will explain better than me:
public class News extends android.support.v4.app.Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
setText();
return inflater.inflate(R.layout.lay_news, container, false);
}
public void setText(){
TextView texts = (TextView) getView().findViewById(R.id.textView);
texto.setText("hi");
}
}
NullPointerException when I call the method setText. I know that it's the TextView definition, but I don't know how to "find" it in the layout without getView() or getActivty(), both return a NullPointerException when the view loads.
Just do
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.lay_news, container, false);
setText(v);
return v;
}
and
public void setText(View v){
TextView texts = (TextView)v.findViewById(R.id.textView);
texto.setText("hi");
}
Whenever I use findViewById in my Activity's onCreate I get a NullPointerException. For example:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
TextView mTextView = (TextView) findViewById(R.id.textview);
}
I have read that the problem might by that the views may not be fully loaded when I try to find them and that is why I get a null pointer. If I call for findViewById in onCreateView of my Activity's fragment, everything works fine:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
TextView mTextView = (TextView) rootView.findViewById(R.id.textview);
}
However, I need to use mTextView outside this fragment class. I have seen many examples in Android's official documentation where findViewById is used in the Activity's onCreate. What am I doing wrong?
From the layout xmls, place your textview in the layout xml- activity_main so the activity's setContentView can find it there.
Currently the textview is in the layout xml fragment_main
fyi: fragment_main and activity_main are two different layouts. If your app structure doesn't demand the fragment then you don't need it at all.
you can try something like this -
class MainActivity extends Activity{
public TextView tv;
protected void onCreate(Bundle savedInstance){
......
}
public void doActionOnTextView()
{
//at this point you have the textview in tv
// do what you want to do with it
}
}
class MyFragment extends Fragment{
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
TextView mTextView = (TextView) rootView.findViewById(R.id.textview);
mTextView.postRunnable(new Runnable(){
((MainActivity)getActivity()).tv=mTextView;
((MainActivity)getActivity()).doActionOnTextView();
}
}
Its not the best solution, but would work for you now.
I have a fragment with a control (a TextView) with id R.id.currentCycleIndicator and want to programmatically set its value from within the fragment.
Both
getActivity().findViewById(R.id.currentCycleIndicator) and
getView().findViewById(R.id.currentCycleIndicator)
return null.
How can I get reference to that TextView inside the fragment code?
Try getting its value in onCreateView() callBack function of your Fragment like below :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_layout, container, false);
TextView textView = (TextView) view.findViewById(R.id.currentCycleIndicator);
return view;
}
You can do that by overriding the onViewCreated function as below :
private TextView tv;
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
tv = (TextView) view.findViewById(R.id.currentCycleIndicator);
}
Inflate your view inside onCreateView and refer to your TextView from there.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.layoutwheretextviewis, container, false);
TextView text= (TextView) view.findViewById(R.id.currentCycleIndicator);
return view;
}