我已使用默认实现覆盖了Fragment类的onCreateAnimation方法,这导致FATAL EXCEPTION。为什么?
java.lang.RuntimeException:无法启动活动 ComponentInfo {com.developer.pocviewmodel / com.developer.pocviewmodel.MainActivity}: java.lang.IllegalStateException:super.onCreateAnimation(transit, 输入,nextAnim)不能为空
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)
}
}
答案 0 :(得分:1)
java.lang.IllegalStateException : must not be null
。 onCreateAnimation
默认返回null。
如果您反编译类,则会看到该函数的Java版本:
@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
返回null并因此引发异常,因为返回值被标记为@NotNull。覆盖该方法并返回非null,或者将返回类型更改为Animation?
。但是由于您不对其进行任何操作,因此可以将其完全删除。您不必重写它,它不是抽象的。
为记录起见,如果使返回类型可为空,这是反编译的Java代码:
@Nullable
public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
return super.onCreateAnimation(transit, enter, nextAnim);
}