简体   繁体   English

带数字选择器的自定义布局对话框

[英]Custom layout Dialog with Number Picker

I'm creating a Dialog Fragment with a custom layout which includes a Number Picker. 我正在创建一个具有自定义布局的对话框片段,其中包括一个数字选择器。

To do so, I've created a DialogFragment class which implements NumberPicker.onValueChangeListener and a layout xml file that it will use. 为此,我创建了一个DialogFragment类,该类实现NumberPicker.onValueChangeListener和它将使用的布局xml文件。

I'm having an issue associating the Number Picker in the layout with a variable in the fragment class because 'findViewById' "Method cannot be resolved" 我在将布局中的数字选择器与片段类中的变量相关联时遇到问题,因为“ findViewById” “无法解析方法”

How can I get around this problem? 我该如何解决这个问题?

Elements of code below: 下面的代码元素:

Dialog Fragment: 对话框片段:

public class PlayersDialogueFragment extends DialogFragment implements NumberPicker.OnValueChangeListener {

NumberPicker numberOfPlayersPicker = null;

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the Builder class for convenient dialog construction
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    LayoutInflater inflater = getActivity().getLayoutInflater();
    builder.setView(inflater.inflate(R.layout.players_fragment_layout, null));

    numberOfPlayersPicker = (NumberPicker) findViewById(R.id.numberOfPlayersPicker);
    numberOfPlayersPicker.setMaxValue(4);
    numberOfPlayersPicker.setMinValue(2);

Layout - "players_fragment_layout": 布局-“ players_fragment_layout”:

<NumberPicker
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/numberOfPlayersPicker"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

I can put getActivity() before findViewById and can run the app, but it gives a null object reference error by doing so. 我可以将getActivity()放在findViewById之前,并可以运行该应用程序,但是这样做会产生空对象引用错误。

PS: If it matters, the dialog fragment is being called by Main Activity upon button press. PS:如果很重要,则按一下“主活动”即可调用对话框片段。

Assign inflater.inflate(R.layout.players_fragment_layout, null) to a variable v , and then call v.findViewById like so: inflater.inflate(R.layout.players_fragment_layout, null)分配给变量v ,然后像下面这样调用v.findViewById

public class PlayersDialogueFragment extends DialogFragment implements NumberPicker.OnValueChangeListener {

    NumberPicker numberOfPlayersPicker = null;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the Builder class for convenient dialog construction
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View v = inflater.inflate(R.layout.players_fragment_layout, null);
        builder.setView(v);

        numberOfPlayersPicker = (NumberPicker) v.findViewById(R.id.numberOfPlayersPicker);
        numberOfPlayersPicker.setMaxValue(4);
        numberOfPlayersPicker.setMinValue(2);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM