Android自定义对话框(DialogFragment)

时间:2013-04-09 23:22:53

标签: android android-dialog android-dialogfragment

我使用以下方法获得以下DialogFragment

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

自定义布局正确膨胀,但如果我尝试更改TextView或尝试将适配器附加到AutoCompleteTextView,我会得到一个空指针异常,并且无法找出原因(不知道为什么{{1 }} 不管用)。救命啊!

3 个答案:

答案 0 :(得分:1)

尝试类似:

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

问候。

答案 1 :(得分:1)

通过以下方式解决了这个问题:

@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;
}

并调用此方法删除标题:

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

答案 2 :(得分:0)

因为你还没有创建它。您致电create并在方法结束时返回View,但您之前尝试findViewById