Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot use TraitContext in Fragments or other places where context is null in constructor #60

Open
sgrif opened this issue Aug 29, 2013 · 1 comment

Comments

@sgrif
Copy link
Contributor

sgrif commented Aug 29, 2013

I've run into issues attempting to do the following:

class MyFragment extends ListFragment with TraitContext[Context] {
  override def basis = getActivity
  lazy val adapter = new MyAdapter

  override def onCreate(savedInstanceState: Bundle) = {
    super.onCreate(savedInstanceState)
    setListAdapter(adapter)
  }

  //Actual data is given to the adapter later, though the data itself is irrelevant
}

class MyAdapter(implicit context: Context) extends BaseAdapter {
  def layoutInflater = LayoutInflater.from(context)

  override def getView(index: Int, convertView: View, parent: ViewGroup) = {
    if (convertView == null) {
      layoutInflater.inflate(...) // Error occurs here
    }
  }
}

The issue stems from the fact that getActivity returns null at the point where implicit val ctx = basis is evaluated. This could be fixed if ctx were changed to a def or a lazy val.

@ngirardin
Copy link

Did you try to use the SFragmentActivity from the scaloid-support-v4 project?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants