代码之家  ›  专栏  ›  技术社区  ›  MathanMV

Android自定义对话框(DialogFragment)

  •  0
  • MathanMV  · 技术社区  · 12 年前

    我有以下内容 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或尝试将Adapter附加到AutoCompleteTextView,我会得到一个空指针异常,并且不知道为什么(不知道为什么 getActivity.findViewByID() 不工作)。帮助

    3 回复  |  直到 11 年前
        1
  •  1
  •   eltabo    12 年前

    尝试以下操作:

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

    当做

        2
  •  1
  •   mmBs Tom7    11 年前

    解决方法:

    @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);
    
        3
  •  0
  •   kabuko    12 年前

    因为你还没有创建它。你打过电话 create 并返回 View 在方法的最后,但你正在尝试 findViewById 在此之前。