带有数字选择器的自定义布局对话框
Custom layout Dialog with Number Picker
我正在创建一个包含数字选择器的自定义布局的对话框片段。
为此,我创建了一个实现 NumberPicker.onValueChangeListener 的 DialogFragment class 和它将使用的布局 xml 文件。
我在将布局中的数字选择器与片段中的变量相关联时遇到问题 class 因为 'findViewById' "Method cannot be resolved"
我怎样才能解决这个问题?
以下代码元素:
对话框片段:
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);
布局 - "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" />
我可以将 getActivity() 放在 findViewById 之前,并且可以 运行 应用程序,但这样做会产生空对象引用错误。
PS:如果这很重要,按下按钮时 Main Activity 会调用对话框片段。
将 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);
}
}
我正在创建一个包含数字选择器的自定义布局的对话框片段。
为此,我创建了一个实现 NumberPicker.onValueChangeListener 的 DialogFragment class 和它将使用的布局 xml 文件。
我在将布局中的数字选择器与片段中的变量相关联时遇到问题 class 因为 'findViewById' "Method cannot be resolved"
我怎样才能解决这个问题?
以下代码元素:
对话框片段:
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);
布局 - "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" />
我可以将 getActivity() 放在 findViewById 之前,并且可以 运行 应用程序,但这样做会产生空对象引用错误。
PS:如果这很重要,按下按钮时 Main Activity 会调用对话框片段。
将 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);
}
}