gpt4 book ai didi

android - 无法实例化 fragment (找不到 fragment 构造函数)

转载 作者:行者123 更新时间:2023-11-29 02:21:50 25 4
gpt4 key购买 nike

我在某些设备上遇到了这个问题,并且在我的崩溃分析中遇到错误。当应用遇到 ANR 问题并且错误是

Unable to start activity ComponentInfo{com.qwykr.dryver.base/com.qwykr.dryver.base.activities.MainActivity}: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.qwykr.dryver.base.fragments.SlideFragment: could not find Fragment constructor

@Override
protected void onCreate(Bundle arg0) {
// TODO Auto-generated method stub
super.onCreate(arg0);
}

Slide 的构造函数是

  @SuppressLint("ValidFragment")
public SideMenuFragment(Context mContext) {
this.mContext=mContext;
}

并且有时会出现 ANR,这是导致此问题的原因。有时它工作正常,有时它会导致问题

最佳答案

这个例子在 Kotlin 上,但你也可以在 Java 上做

abstract class BaseFragment : Fragment() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}

override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
}

override fun onDestroyView() {
super.onDestroyView()
}

override fun onResume() {
super.onResume()
}
}
class ApproveFragment : BaseFragment() {

companion object {
fun newInstance(orderId: Int) = ApproveFragment().apply {
arguments = Bundle(1).apply {
putInt("ORDER_ID", orderId)
}
}
}

override fun onCreateView(
inflater: LayoutInflater?,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater?.inflate(R.layout.fragment_approve, container, false)
}

override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
}
}

关于android - 无法实例化 fragment (找不到 fragment 构造函数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55119934/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com