的Android自定义对话框(DialogFragment)

问题描述:

我有以下DialogFragment以下方法:的Android自定义对话框(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()不工作)。帮帮我!

与解决了这个:

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

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

+0

我试着打电话给在onViewCreated行()藏汉,那是我叫dialog.show的对话外()但仍然没有运气!任何想法为什么? – MathanMV 2013-04-10 13:18:59

+0

而不是试图从活动中查找视图,尝试从片段的视图中找到它。如果你想在'onViewCreated'中做到这一点,尝试用'getView()。findViewById'替换'getActivity()。findViewById'。 – kabuko 2013-04-10 17:22:44

尝试类似:

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

问候。

+0

.setContentView()不是AlertDialog构建器类中的方法! – MathanMV 2013-04-10 13:08:14

+0

没错。让我们改变方法。 – eltabo 2013-04-10 18:41:13