I have overrided the onCreateAnimation method of the Fragment class with default implementation and this leads to FATAL EXCEPTION. Why?
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.developer.pocviewmodel/com.developer.pocviewmodel.MainActivity}: java.lang.IllegalStateException: super.onCreateAnimation(transit, enter, nextAnim) must not be null
class MainFragment : Fragment() {
companion object {
fun newInstance() = MainFragment()
}
private lateinit var viewModel: MainViewModel
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
return inflater.inflate(R.layout.main_fragment, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
viewModel = ViewModelProviders.of(this).get(MainViewModel::class.java)
// TODO: Use the ViewModel
}
override fun onCreateAnimation(transit: Int, enter: Boolean, nextAnim: Int): Animation {
return super.onCreateAnimation(transit, enter, nextAnim)
}
}
java.lang.IllegalStateException : must not be null
is thrown when an @NotNull parameter or variable is null, or if a @NotNull function returns null. onCreateAnimation
returns null by default.
If you decompile your class, you'll see the Java version of the function:
@NotNull
public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
Animation var10000 = super.onCreateAnimation(transit, enter, nextAnim);
Intrinsics.checkExpressionValueIsNotNull(var10000, "super.onCreateAnimation(transit, enter, nextAnim)");
return var10000;
}
onCreateAnimation
returns null and therefore throws an exception because the return value is marked as @NotNull. Either override the method and return non-null, or change the return type to Animation?
. But since you don't do anything with it, you can remove it entirely. You don't have to override it, it's not abstract.
For the record, this is the decompiled Java code if you make the return type nullable:
@Nullable
public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
return super.onCreateAnimation(transit, enter, nextAnim);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.