MathanMV
MathanMV

Reputation: 412

Android Custom Dialog (DialogFragment)

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!

Upvotes: 0

Views: 2304

Answers (3)

MathanMV
MathanMV

Reputation: 412

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

Upvotes: 1

eltabo
eltabo

Reputation: 3807

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.

Upvotes: 1

kabuko
kabuko

Reputation: 36302

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.

Upvotes: 0

Related Questions