0

I have the following DialogFragment with the following method :

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    LayoutInflater inflater = getActivity().getLayoutInflater();
    builder.setView(inflater.inflate(R.layout.buddy_challenge, null));

    this.title = (TextView)getActivity().findViewById(R.id.challenge_title);
    this.challenge = (Button)getActivity().findViewById(R.id.challenge_button);
    this.at = (AutoCompleteTextView)getActivity().findViewById(R.id.chall_ex);

    ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("action", "getAllEx"));
    new ServerCallEx().execute(params);
    return builder.create();
}

The custom layout inflates properly but if I try changing the TextView or try attach an Adapter to the AutoCompleteTextView I get a null pointer exception and can't figure out why (don't know why the getActivity.findViewByID() is not working). Help!

3 Answers 3

1

Try something like:

    LayoutInflater inflater = getActivity().getLayoutInflater();
    View view = inflater.inflate(R.layout.buddy_challenge, null);
    builder.setView(view);

    this.title = (TextView)view.findViewById(R.id.challenge_title);

Regards.

Sign up to request clarification or add additional context in comments.

1 Comment

.setContentView() is not a method in the AlertDialog builder class!
1

Resolved this with :

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.buddy_challenge, container, false);
    this.title = (TextView)view.findViewById(R.id.challenge_title);
    this.at = (AutoCompleteTextView)view.findViewById(R.id.chall_ex);
    this.at.setThreshold(1);
    return view;
}

and calling this to remove the title:

challDialog.setStyle(DialogFragment.STYLE_NO_TITLE, android.R.style.Theme_Holo_Light_Dialog);

Comments

0

Because you haven't created it yet. You called create and return the View at the end of the method, but you're trying to do findViewById before that.

2 Comments

I tried calling the line in onViewCreated() aswell as outside of the dialog where I call dialog.show() but still have no luck! any ideas why?
Instead of trying to find the view from the activity, try to find it from the fragment's view. If you want to do it in onViewCreated, try replacing getActivity().findViewById with getView().findViewById.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.