【问题标题】:DialogFragment - Where to access layout components [i.e. editText.setText("")]DialogFragment - 访问布局组件的位置 [即editText.setText("")]
【发布时间】:2013-11-14 10:06:58
【问题描述】:

在进行了大量研究并尝试(我认为)我能找到的每一种生命周期方法之后,我真的陷入了困境:

我想要什么:基本上我有这个 DialogFragment(“ProfileUsernameDialogFragment”),它应该显示我的布局文件(“fragment_profile_header_username_dialog”)。 在这个布局文件中,我有一些 TextViews 和一些 EditTexts。我想将这些 EditTexts 的文本设置为我在程序中获得的某个值,因此无法预先设置。

我有什么:

public class ProfileUsernameDialogFragment extends DialogFragment {

...

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    EditText usernameText = (EditText) this.getView().findViewById(R.id.username_field);
    usernameText.setText("TEST");
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Log.d(AppController.LOG_STRING, this.getClass().toString() + " - onCreateDialog  - START");
    AlertDialog.Builder builder = new AlertDialog.Builder(this.getActivity());
    View view = this.getActivity().getLayoutInflater().inflate(R.layout.fragment_profile_header_username_dialog, null);
    builder.setView(view);
    builder.setPositiveButton(SAVE_SETTINGS, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int whichButton) {
            // Tried this too:
            // EditText usernameText = (EditText)
            // ProfileUsernameDialogFragment.this.getView().findViewById(R.id.forename_label);
            // usernameText.setText("TEST");
        }
    });
    builder.setNegativeButton(ABORT_ACTION, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int whichButton) {
            dialog.dismiss();
        }
    });
    builder.setTitle(CHANGE_NAME_SETTINGS);
    Dialog dialog = builder.create();
    return dialog;
}
}

我得到的: 是 NullPointerException,无论我尝试了 findViewById(R.id.username_field) 的哪个位置。 (我也尝试过 onCreate 和其他一些方法,我开始认为我可能有除代码位置之外的另一个问题,但我真的不知道)我试图在 DialogFragments 中关注至少 6 个关于 NullPointers 的类似问题,所以我想我需要高级帮助^^

我应该添加我对 android 完全陌生,所以请友好,但是非常欢迎会议提示等!提前谢谢:)

【问题讨论】:

  • “预先设置”是什么意思
  • view.findViewById(R.id...) 应该可以工作
  • 我的意思是我不能使用字符串资源或类似的东西。我不确定你的意思是应该工作吗?我目前所拥有的东西不起作用 - 我应该改变什么,或者你知道我还有什么其他问题吗?

标签: android android-layout nullpointerexception android-dialogfragment


【解决方案1】:

以下是您需要的:

mAlertDialog.setOnShowListener(new DialogInterface.OnShowListener() {

        @Override
        public void onShow(final DialogInterface dialog) {

            final Button b = mAlertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
            b.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(final View view) {

                    v.findViewById(R.id...)
                }
            });
        }
    });
            });
        }
    });

【讨论】:

  • 非常感谢!我试图总是通过 getView() 访问视图,但它不起作用,你的答案对我有用。所以对其他人来说:必须使用此处创建的视图:View view = this.getActivity().getLayoutInflater().inflate(R.layout.fragment_profile_header_username_dialog, null);
【解决方案2】:

除了@David 的答案之外的另一个选择是使用 onStart() 方法。

看起来您在问题中是按照这些思路考虑的,除了您使用了 onActivityCreated() 方法。

例如:

    @Override
    public void onStart() 
    {
        super.onStart();
        EditText usernameText = (EditText) this.getDialog().findViewById(R.id.username_field);
        usernameText.setText("TEST");
    }

【讨论】:

    【解决方案3】:

    Android 文档有很好的example

    所以,如果你不会在你的布局中设置文本,你需要在onCreateView()方法中设置它,例如:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup cont,
                             Bundle savedInstanceState) {
        // Inflate the layout to use as dialog or embedded fragment
        View view = inflater.inflate(R.layout.app_info, cont, false);
        TextView title = (TextView)view.findViewById(R.id.dialog_header);
        TextView content = (TextView)view.findViewById(R.id.dialog_content);
        Button button = (Button)view.findViewById(R.id.dialog_button);
    
        title.setText(this.dialogTitle);
        content.setText(this.dialogContent);
    
        button.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                getDialog().dismiss();
            }
        });
    
        return view;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多