SetText of TextView in DialogFragment from Calling Activity

人走茶凉 提交于 2019-12-08 09:56:48

问题


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!!


回答1:


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


    }



回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/22571282/settext-of-textview-in-dialogfragment-from-calling-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!