Issue
I am using navigation architecture component library and my app's starting point is this fragment:
class MainFragment : BaseFragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_main, container, false)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
}
Which inherits from an abstract class which is BaseFragment
:
abstract class BaseFragment : Fragment() {
}
When I run my app I get:
Unable to instantiate fragment io.example.MainFragment: calling Fragment constructor caused an exception
But this does not happen if MainFragment
extends Fragment
instead of BaseFragment
. What is the reason? Does this have something to do with how the Navigation Architecture Component works?
Solution
I had the similar issue because I had val in MyBaseFragment
protected abstract val gpsMsg: String
which I was overiding this way in other Fragment before fragment attached to context.
override val gpsMsg: String = getString(R.string.gps_not_enabled)
So the underlying error was because context was null and getString uses getResources()
which returns requireContext().getResources()
. And in the requireContext()
source code error will be thrown.
public final Context requireContext() {
Context context = getContext();
if (context == null) {
throw new IllegalStateException("Fragment " + this + " not attached to a context.");
}
return context;
}
So the error thrown causes fragment not to be instantiated. So I would advice being careful with context thing when override.
Answered By - Nux