我正在尝试使用 Scala 开发 Android 应用程序。
当我的应用程序启动时,它会立即显示一个对话框,如下所示:
override def onCreate(savedInstanceState: Bundle) {
super.onCreate(savedInstanceState)
competitionSetupDialogFragment.show(getFragmentManager(), HomeScreenActivity.CompetitionDialog)
}
override def onStart() {
super.onStart
resourceHelper.setActivity(this)
}
我的片段如下:
class CompetitionSetupDialogFragment extends DialogFragment {
@Inject
var competitionSetupDialogHandler: CompetitionSetupDialogHandler = null
@Inject
var resourceHelper: ResourceHelper = null
override def onCreateDialog(savedInstanceState: Bundle): Dialog = {
val builder = new AlertDialog.Builder(getActivity())
builder.setMessage(getString(R.string.competitionSetupDialogMessage))
.setCancelable(false)
.setView(getActivity().getLayoutInflater.inflate(R.layout.competition_setup, null))
.setNegativeButton(R.string.cancel, new OnClickListener {
def onClick(dialog: DialogInterface, which: Int) {
dismiss()
}
})
.setPositiveButton(R.string.create, new OnClickListener {
def onClick(dialog: DialogInterface, which: Int) {
val competitionNameInput = resourceHelper.findViewById(R.id.competitionNameInput).asInstanceOf[EditText]
val numberOfPlayersInput = resourceHelper.findViewById(R.id.numberOfPlayersInput).asInstanceOf[EditText]
val numberOfSubsInput = resourceHelper.findViewById(R.id.numberOfSubsInput).asInstanceOf[EditText]
val lengthOfHalfInput = resourceHelper.findViewById(R.id.lengthOfHalfInput).asInstanceOf[EditText]
competitionSetupDialogHandler.retrieveValuesAndSave(competitionNameInput, numberOfPlayersInput, numberOfSubsInput, lengthOfHalfInput)
}
})
builder.create()
}
}
最后,ResourceHelper 包含以下内容:
public View findViewById(int viewID) {
return activity.findViewById(viewID);
}
我的问题是我的 resourceHelper.findViewById 总是返回 null,即使这些是有效的视图。
我的想法是,当 onClick() 被调用时,视图已经被破坏,或者类似的东西。我该如何解决这个问题?
在这里要添加的一件事是我的对话框显示正常,只有在执行 onClick 时我才有问题