Android : Textview giving null pointer exception in fragment - android

I am having a fragment A in which i initialized another fragment say Frgament B. My fragment A is having a listview. On click of that listview i need to call a method of fragment B and update Textview. But as soon as i call this method it flashes following message in Logcat
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
My code is as follows :
public class FragmentA extends Fragment {
// in oncreateview
FragmentB fb=new FragmentB();
// on listview click item i call a method of fragment B
setdata(value); // where value is clicked position
}
// Fragment B is as follows :
public class FragmentB extends Fragment {
// IN ONCRAETEVIEW
initialized textview
return view;
public void setdata(int data)
{
// Updating textview settext method
}
}
Please help
Thanks in advance

Only the fragment B is add into the layout then the View of the fragment B is created. So you should call setdata() after the fragment transaction.
Fragment fragmentB = new Fragment();
getFragmentManager().beginTransaction().add(R.layout.container, fragmentB).commit();
fragmentB.setData(value);

FragmentManager m=getSupportFragmentManager();
FragmentB fb=(FragmentB )m.findFragmentById(R.id.your_fragment_id_from_xmlsetdata);
fb.setdata(value);
try with this code it will definitely works

Related

Android Get View In Fragment Returns Null

I have an activity with two fragments, Fragment A and Fragment B. I want to show a view hidden in Fragment A when the user touches a button in fragment B. How can I do this? I have tried to get the whole layout of the activity and get the view but I get a null pointer exception.
My activity layout is as shown below
This is the line I am using. It throws a null pointer exception.
shadowLine = getActivity().findViewById(R.id.shadowLine);
shadowLine.setVisibility(View.VISIBLE);
The simplest way, not the safest:
You can access hosting activity in fragment B by
HostActivity activity =(HostActivity) getActivity();
activity.callOtherFragment();
In that activity, you can access fragment A, by
public void callOtherFragment() {
YourFragment A = (YourFragment)getFragmentManager().findFragmentById(R.id.fragmentA);
A.showSomeStuff();
}
then implement your method in fragment A:
public void showSomeStuff() {
shadowLine.setVisibility(View.VISIBLE);
}

Setting fragments by tag

How do I go about setting my Fragments by tag since I did not explicitly make them in my XML so I can't use findbyID.
I have 4 Fragments in an Activity and want to pass data back and forth from the Fragments but I keep getting error
Attempt to invoke virtual method 'void
com.project.myproject.app.Fragment.addToList()' on a null object
reference
I don't think I am setting my Fragment's tags right so could someone please link me to an example of this?
Set the tag for fragment:
YourFragment fragment = getFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment, fragment, "YOUR_TAG").addToBackStack("YOUR_TAG").commit();
Get the fragment:
private YourFragment getFragment() {
YourFragment fragment = getSupportFragmentManager().findFragmentByTag("YOUR_TAG");
if (fragment == null) {
fragment = new YourFragment();
}
return fragment;
}

To call a method of a fragment from another fragment

I have 2 Fragments for an activity. e.g. FragmentA and FragmentB. I have a public method MethodA in FragmentA. Now I want to use MethodA in FragmentB of FragmentA.
One of the way to do is Create Interface and implement your activity with that interface. Now initilize that interface in FragmentB and on click of something you have to call the method of interface wherever you want to call method of FragmentB. Now in your activity callback method just call the method of FragmentB with the help of object
First you have to get list of all fragment and then child fragment then check your fragment instance.
for (Fragment fragment : getSupportFragmentManager().getFragments())
{
for (Fragment fragment1 : fragment.getFragmentManager().getFragments())
{
if (fragment1 instanceof Shoppingcart)
{
FragmentA mFragmentA = ((FragmentA) fragment1);
mFragmentA.A();
}
}
}

Fragment over Fragment issue

My requirement is :
In one Fragment, had textview, by clicking on that one DialogFragment is appearing with a list of values. By clicking on value, The dialog fragment should be closed and selected values should be displayed in textview in the first fragment.
But here the problem is accessing textview object is null.
Communicating between fragments using interface i.e., By clicking on list in dialogFragment one interface is calling. That interface method implemented in first fragment.
So, How to access the textview object?
Here code is:
FragmentManager manger = getFragmentManager();
listDialogFragment.show(manger, null);
public interface onOkClickListener {
void setOnOkClickListener(int pos,String selectedID,MainActivity actv);
}
#Override
public void setOnOkClickListener(int pos,String id,MainActivity actv) {
textView object// accessing null
}
Thanks In advance..

update TextView in fragment A when clicking button in fragment B

I have two fragments sitting side by side in the same activity. When I touch a button in the right fragment (fragment B), I need a TextView in the left fragment to update (fragment A). I have looked all over for the best way to do this, but nothing seems to work for my needs. Could someone possibly give me an example of how I would code this? Fragment A is set through the XML layout, and fragment B gets loaded programmatically into a container. I have tried accomplishing this by using a method in fragment A to update the text, and calling on that method from a method in the parent activity. I then call on the method in the parent activity from fragment B.
This is the code in fragment B that declares the interface and calls a method in the interface
AttackCards attackCards;
public interface AttackCards {
public void deckSize();
}
public void onAttach(DeckBuilder deckBuilder) {
super.onAttach(deckBuilder);
attackCards = (AttackCards) deckBuilder;
}
attackCards.deckSize(); //this is in my onclick methods
This is the code in the activity that implements the interface and calls the method in fragment A
public class DeckBuilder extends Activity implements AttackCards{
public void deckSize() {
DeckBuilderFragment deckBuilderFragment = (DeckBuilderFragment)getFragmentManager().
findFragmentById(R.id.deckbuilder_fragment);
deckBuilderFragment.deckSize();
}
This is the method in fragment A that appends the textview with the contents of a shared preferences value
public void deckSize() {
deckSize = (TextView) getView().findViewById(R.id.decksize);
final SharedPreferences defaultDeck = getActivity()
.getSharedPreferences("defaultDeck", Context.MODE_PRIVATE);
deckSize.setText(String.valueOf(defaultDeck.getInt("decksize", 0)));
}
Sadly this attempt simply brings me a nullpointer when touching a button. I am getting a null pointer at
attackCards.deckSize(); //this is in my onclick methods
Could someone please help me out with an example of how to do this correctly?
One fragment should not communicate to another fragment directly. It should do so through attached activity. The detail explanation with code example is available here
Android Developer site
Declare an interface in Fragment B, and implement the interface in the activity. Call the interface through callback in Fragment B when button is clicked. You can have a public function in Fragment A to update the TextView, so activity directly call the function to update the text.
You can define an interface in Fragment B and implement it on the MainActivity. Then on the callback method (onClickOnB in this case) set the text on the TextView. You should obtain a reference of the TextView in the Activity's onCreate() after setContentView(). This works because Fragment A is static. Otherwise, you can create a public method inside Fragment A so you can set the text from inside the callback by getting a reference of Fragment A and calling such method.
Fragment B
public class FragmentB extends Fragment implements onClickListener{
ClickOnB listener;
public void setOnFragmentBClickListener(ClickOnB listener){
this.listener = listener;
}
#Override
public void onClick(View v){
//stringMessage is a `String` you will pass to `Fragment` A to update its `TextView`
listener.onClickOnB(stringMessage);
}
interface ClickOnB{
public void onClickOnB(String message);
}
}
MainActivity
public class MainActivity extends Activity implements ClickOnB{
#Override
protected onCreate(Bundle savedInstanceState){
//Get a reference of `Fragment` B somewhere in your code after you added it dynamically and set the listener.
((FragmentB)getFragmentManager().findFragmentByTag("FragmentB")).setOnFragmentBClickListener(this);
}
#Override
public void onClickOnB(String message){
//Set the text to the `TextView` here (I am assuming you get a reference of the `TextView` in onCreate() after inflating your layout.
mTextView.setText(message);
}
}

Categories

Resources