1

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

mmBs
  • 8,421
  • 6
  • 38
  • 46
user2737686
  • 33
  • 1
  • 4

3 Answers3

4

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


    }
Arsh Kaushal
  • 521
  • 1
  • 5
  • 18
Jitendra
  • 319
  • 1
  • 7
  • Thank you! This worked. The only change I had to make was to use the view I had custom created to set the textview rather than setmessage. setmessage worked fine but I had a custom view so it just appeared at the top. Thank you again! You rock. – user2737686 Mar 22 '14 at 21:12
  • Welcome. Great to know that it helped. – Jitendra Mar 24 '14 at 07:29
1

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

Matt Cremeens
  • 4,951
  • 7
  • 38
  • 67
zhen_khokh
  • 69
  • 1
  • 8
0

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

Attiq ur Rehman
  • 475
  • 1
  • 6
  • 21
  • I'm trying this but failing to make it work. The app still crashes. Should I be adding the interface to the DialogFragment and implementing it in the DialogFragment and in Activity A? I tried implementing it in both, just in DialogFragment (not sure why this would work) and just in Activity A but nothing works. – user2737686 Mar 22 '14 at 05:47
  • My friend, you will implement this interface in FragmentActivity and call from Activity class like this (new FragmentClass().implementedFunction();) – Attiq ur Rehman Mar 22 '14 at 09:19