Android自定义对话框(DialogFragment)
我使用以下方法创建了以下Android自定义对话框(DialogFragment),android,android-dialog,android-dialogfragment,Android,Android Dialog,Android Dialogfragment,我使用以下方法创建了以下DialogFragment: @Override public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); LayoutInflater inflater = getActivity().getLayoutInflater(); builder.se
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();
}
@覆盖
创建对话框上的公共对话框(Bundle savedInstanceState){
AlertDialog.Builder=新建AlertDialog.Builder(getActivity());
LayoutFlater充气机=getActivity().GetLayoutFlater();
builder.setView(充气机.充气(R.layout.buddy_challenge,null));
this.title=(TextView)getActivity().findViewById(R.id.challenge\u title);
this.challenge=(按钮)getActivity().findViewById(R.id.challenge_按钮);
this.at=(AutoCompleteTextView)getActivity().findViewById(R.id.chall_ex);
ArrayList params=新的ArrayList();
参数add(新的BasicNameValuePair(“action”、“getAllEx”);
新建ServerCallEx().execute(参数);
返回builder.create();
}
自定义布局会正常膨胀,但如果我尝试更改TextView或尝试将适配器连接到AutoCompleteTextView,我会收到一个空指针异常,并且无法找出原因(不知道为什么
getActivity.findViewByID()
不起作用)。救命啊 因为您还没有创建它。您调用了create
并在方法末尾返回了视图,但在此之前您正在尝试执行findViewById
。尝试以下操作:
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);
关于。通过以下方式解决了此问题:
@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);
.setContentView()不是AlertDialog builder类中的方法!我尝试在onViewCreated()中调用该行,也尝试在调用dialog.show()的对话框外部调用该行,但仍然没有成功!知道为什么吗?与其试图从活动中找到视图,不如尝试从片段的视图中找到它。如果要在创建的视图中执行此操作,请尝试将getActivity().findViewById
替换为getView().findViewById
。