0

我有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 我得到一个空指针异常并且无法弄清楚为什么(不知道为什么getActivity.findViewByID()不工作)。帮助!

4

3 回答 3

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

问候。

于 2013-04-09T23:27:31.680 回答
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);
于 2013-04-10T14:12:22.583 回答
0

因为你还没有创建它。您在方法结束时调用create并返回了View,但您试图在findViewById此之前执行此操作。

于 2013-04-09T23:25:33.300 回答