0

In my adnroid app when the user goes to their own profile, there is a fragment there with two buttons - X points and settings. For the button X points I want to change the text to whatever the amount of points they have, for example 12 points.

I've tried numerous things but nothing seems to work:

Attempt 1:

        myProfileActionButtonsHolder = (TableRow) findViewById(R.id.myProfileActionButtonsHolder);
        getSupportFragmentManager().beginTransaction().replace(R.id.myProfileActionButtonsHolder, new MyProfileActionButtonsFragment()).commit();

        MyProfileActionButtonsFragment.bMyProfilePoints = (Button) findViewById(R.id.bMyProfilePoints);
        MyProfileActionButtonsFragment.bMyProfilePoints.setText("asd");

Attempt 2:

    MyProfileActionButtonsFragment myProfileActionButtonsFragment = (MyProfileActionButtonsFragment) getSupportFragmentManager().findFragmentById(R.id.myProfileActionButtonsHolder);
    ((Button)myProfileActionButtonsFragment.getView().findViewById(R.id.bMyProfileSettings)).setText("asd");

Attempt 3

myProfileActionButtonsFragment.setBMyProfileSettingsText("asd"); //setBMyProfileSettingsText is a custom method defined inside the fragment

Here is how my fragment looks:

public class MyProfileActionButtonsFragment extends SherlockFragment {
    public static Button bMyProfilePoints;
    public Button bMyProfileSettings;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

        View view= inflater.inflate(R.layout.my_profile_action_buttons_fragment, container, false);
        bMyProfilePoints = (Button) view.findViewById(R.id.bMyProfilePoints);
        bMyProfileSettings = (Button) view.findViewById(R.id.bMyProfileSettings);

         return view;
    }

    public void setBMyProfileSettingsText(String text) {
        bMyProfilePoints.setText(text);
    }
}

Im ALWAYS getting a NullPointerException on the line where I try to set the text to the button.

Kaloyan Roussev
  • 14,515
  • 21
  • 98
  • 180

2 Answers2

1

Declare an interface in Fragment, and implement the interface in the activity.

Call the interface through callback in Fragment when button is clicked. You can have a public function in Fragment to update the TextView, so activity directly call the function to update the text.

Something like this

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 the activity to update its `TextView`
listener.onClickOnB(stringMessage);
}

interface ClickOnB{
public void onClickOnB(String message);
}

}

and the activity

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);

}
}

for more details:

update TextView in fragment A when clicking button in fragment B

Community
  • 1
  • 1
gaurav5430
  • 12,934
  • 6
  • 54
  • 111
  • I dont want to set the text when the button is clicked. I want to set the text upon activity creation. Also, I have a function that I call to update the text, but it doesnt work. I read that SO question and its answers and they didnt work for me. There is something wrong with my code I guess – Kaloyan Roussev Dec 23 '13 at 12:58
0

Try this code-

getSupportFragmentManager().beginTransaction().replace(R.id.myProfileActionButtonsHolder, new MyProfileActionButtonsFragment(),"your_tag").commit();
MyProfileActionButtonsFragment fragment = getSupportFragmentManager().findFragmentByTag("your_tag");
if(null!=fragment){
fragment.setBMyProfileSettingsText("asd");
}

hope this works.

nikvs
  • 1,090
  • 5
  • 9
  • I did that and this time I didnt get the NPE and the app didnt crash, but text was not set to "asd". I think there is something wrong with my fragment code... – Kaloyan Roussev Dec 23 '13 at 13:04
  • thats because,it is checking either fragment is null or not ? Off course you fragment is null, that is the reason code inside if is not executing. I am into the same problem. Hope we will find the solution soon. – Murtaza Khursheed Hussain Feb 04 '14 at 06:47