0

in my Android app I have this class called "TripleTab2". TripleTab2 is a fragment that is called as part of a tabbed activity.

public class TripleTab2 extends Fragment {

private TextView nameAndNumberText;
private LinearLayout nameAndNumbersLayoutSMS;

private Map<String, String> nameToNumberMapping;
private View view;
private ArrayList<String> chosenSMSContacts;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.assign_pattern_tab_2, container, false);
    nameAndNumberText = (TextView) view.findViewById(R.id.numAndNameView);
    nameAndNumbersLayoutSMS = (LinearLayout) view.findViewById(R.id.nameAndNumbersLayoutSMS);
    chosenSMSContacts = getArguments().getStringArrayList("numbersToMove");
    createTextViews();

    for(String chosenSMSContact : chosenSMSContacts){
        nameAndNumberText.append(chosenSMSContact);
    }

    return view;
}

The following line of code causes a problem (NullPointerException):

chosenSMSContacts = getArguments().getStringArrayList("numbersToMove");
        createTextViews();

Actually, that makes sense, because the actvity-class called "ChooseSMSContactActivity" (which sends this data) is called after the tripleTab2-class.

Here it is:

public class ChooseSMSContactActivity extends AppCompatActivity {

private LinearLayout nameAndNumbersLayoutSMS;
private Map<String, String> nameToNumberMapping;
private Button selectedNumberBtn;
private ArrayList<String> selectedNamesAndNumbers;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_choose_smscontact_to_add);

    nameAndNumbersLayoutSMS = (LinearLayout) findViewById(R.id.dropDownMenuLayout);
    selectedNumberBtn = (Button) findViewById(R.id.selectedNumberBtn);
    createCheckboxes();
    selectedNamesAndNumbers = new ArrayList<>();


    selectedNumberBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            for(int i=0; i < nameAndNumbersLayoutSMS.getChildCount(); i++){
                if(nameAndNumbersLayoutSMS.getChildAt(i) instanceof CheckBox){
                    CheckBox checkBox = (CheckBox) nameAndNumbersLayoutSMS.getChildAt(i);

                    if(checkBox.isChecked()){
                        selectedNamesAndNumbers.add(checkBox.getText().toString());
                        Bundle bundle = new Bundle();
                        bundle.putStringArrayList("numbersToMove", selectedNamesAndNumbers);
                        TripleTab2 tripleTab2 = new TripleTab2();
                        tripleTab2.setArguments(bundle);
                    }
                }
            }
        }
    });


}

So, TripleTab2 does not have this data on creation, because it is called before ChooseSMSContactActivity.

Does anyone have suggestions on how to resolve this problem? Should I use a BroadcastReceiver or something like that?

Umair
  • 6,366
  • 15
  • 42
  • 50
Nora
  • 1,825
  • 8
  • 31
  • 47
  • 1
    Just checking in, you are creating the TripleTab2 class and passing the bundle, however you aren't doing anything with your `tripleTab2` object. It just sits there. You need to display that specific object in your activity. I'm guessing you create the fragment through XML right now? – Zun Jun 26 '18 at 08:00
  • 1
    The `NullPointerException` is due to not creating an instance of chosenSMSContacts. You have to initiate the instance then used it otherwise NullPointerException will be prompt. – badarshahzad Jun 26 '18 at 08:00
  • 1
    You know the cause of problem.What exactly you want to achieve here. You can also use startActivityForResult() which will return the result back to your `tab2 fragment.` – Umair Jun 26 '18 at 08:01
  • 1
    https://stackoverflow.com/questions/14970790/fragment-getarguments-returns-null – Zun Jun 26 '18 at 08:01
  • 1
    I suggest using the fragment factory method to create the instance of the Fragment. You can check the `Include fragment factory methods?` option when creating Fragment in Android Studio. It will generate a template for you. – Wesely Jun 26 '18 at 08:03
  • 1
    You are passing a value via bundle why not you just pass the value in construtor while creating your instance `TripleTab2 tripleTab2 = new TripleTab2();` – badarshahzad Jun 26 '18 at 08:03
  • Thank you for all the suggestions! I will try them out now – Nora Jun 26 '18 at 08:10
  • 1
    You can use a ViewModel in Activity and then the fragments can observe the LiveData from ViewModelProvider of Activity in the Fragment – EpicPandaForce Jun 26 '18 at 10:57
  • Possible duplicate of [Android - How to pass data from activity to fragment?](https://stackoverflow.com/questions/39137017/android-how-to-pass-data-from-activity-to-fragment) – Sachin Rajput Jun 26 '18 at 13:01

1 Answers1

0

I solved this by using the EventBus-library.

In TripleTab2 EventBus was registered, and an onEvent-method was created:

EventBus.getDefault().register(this);

@Subscribe
public void onEvent(CustomMessageEvent event){
    TextView nameAndNum = new TextView(view.getContext());
    nameAndNum.append(event.getCustomMessage());
    nameAndNumbersLayoutSMS.addView(nameAndNum);
}

The CustomMessageEvent-class looks like this:

public class CustomMessageEvent {

private String customMessage;

public String getCustomMessage() {
    return customMessage;
}

public void setCustomMessage(String customMessage) {
    this.customMessage = customMessage;
}

In the button onClickListener in ChooseSMSActivity I added an EventBus event:

selectedNumberBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            for(int i=0; i < nameAndNumbersLayoutSMS.getChildCount(); i++){
                if(nameAndNumbersLayoutSMS.getChildAt(i) instanceof CheckBox){
                    CheckBox checkBox = (CheckBox) nameAndNumbersLayoutSMS.getChildAt(i);

                    if(checkBox.isChecked()){
                        selectedNamesAndNumbers.add(checkBox.getText().toString());
                        CustomMessageEvent event = new CustomMessageEvent();
                        event.setCustomMessage(checkBox.getText().toString());
                        EventBus.getDefault().post(event);
                    }
                }
            }
        }
    });
Nora
  • 1,825
  • 8
  • 31
  • 47