user2737686
user2737686

Reputation: 33

SetText of TextView in DialogFragment from Calling Activity

Building an Android app and having some trouble. I'd appreciate any help!

I have created an class that extends DialogFragment (Account_Create_Error) that I call from Activity A. I want to set the TextView field in this DialogFragment from Activity A. I created a method in my dialogfragment

public void setError(String message) {
           TextView error = (TextView)getActivity().findViewById(R.id.message);
           error.setText(message);
}

I then use this method in Activity A by doing

Account_Create_Error error = new Account_Create_Error();
error.show(getFragmentManager(), "error");
error.setError(json.getString("response"));

I seem to get a nullpointer exception from findViewById.

Please let me know if providing any more of my code would be helpful.

Thank you!!

Upvotes: 1

Views: 5569

Answers (3)

Jitendra
Jitendra

Reputation: 319

We can pass data to dialog fragment using the constructor.

UserActionDialogFragment dialog = UserActionDialogFragment.newInstance(errorMesssage);
dialog.show(getFragmentManager(), TAG);

Where UserActionDialogFragment extends DialogFragment

public class NotificationDialogFragment extends DialogFragment {

    private static final String TAG = "NotificationDialogFragment";

    private String mMessageToDisplay;

    public static NotificationDialogFragment newInstance(
            String message) {

        NotificationDialogFragment infoDialog = new NotificationDialogFragment();
        mMessageToDisplay = message


    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity());
        alertDialog.setMessage(mMessageToDisplay);
        alertDialog.setNeutralButton(R.string.label_ok,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface arg0, int arg1) {

                    }
                });
        return alertDialog.create();
    }


    }

Upvotes: 4

zhen_khokh
zhen_khokh

Reputation: 69

Try to get EditText from viewer thats set in builder, not getActivity(). This is should be enough

@Override
public Dialog onCreateDialog(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
    LayoutInflater inflater = getActivity().getLayoutInflater();
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    View view = inflater.inflate(R.layout.your_dialog, null);
    builder.setView(view);  
    EditText error = (EditText)(view.findViewById(R.id.message));
    ....
    return builder.create();
}

I didn't use onViewCreated method as mention above. The viewer is set up in builder as advised in doc

Upvotes: 1

Attiq ur Rehman
Attiq ur Rehman

Reputation: 475

My dear friend, i also have faced the same problem in one of my project. I solve it in this way : Create an interface and implement it in my fragment class. In my implemented function, i update my textView. (in your case, your need to initialize the errorTV before calling this function).

Upvotes: 0

Related Questions