Note that there are some explanatory texts on larger screens.

plurals
  1. POfindViewById returns null when invoked from non-Activity
    primarykey
    data
    text
    <p>I am attempting to develop an Android application using Scala.</p> <p>When my application launches it immediately displays a dialog as follows:</p> <pre><code>override def onCreate(savedInstanceState: Bundle) { super.onCreate(savedInstanceState) competitionSetupDialogFragment.show(getFragmentManager(), HomeScreenActivity.CompetitionDialog) } override def onStart() { super.onStart resourceHelper.setActivity(this) } </code></pre> <p>My fragment is as follows:</p> <pre><code>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() } } </code></pre> <p>Lastly, the ResourceHelper contains the following:</p> <pre><code>public View findViewById(int viewID) { return activity.findViewById(viewID); } </code></pre> <p>My problem is that my resourceHelper.findViewById always returns null, even though these are valid views.</p> <p>My thinking is that by the time the onClick() is invoked, the view has been destroyed, or something similar. How can I get around this issue?</p> <p>One thing to add here is that my dialog displays fine, it is only when onClick is performed that I have an issue</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload