4

I'm facing this issue on some devices and getting an error on my crash analytics. this issue comes when the app is in trouble with ANR and the error is

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);
}

and Slide constructor is

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

And some time ANR occur with the cause of this issue. sometimes its work fine and sometimes its cause the issue

Mohit Lakhanpal
  • 1,309
  • 3
  • 11
  • 19
  • 8
    That crash is not an ANR. All fragments need a `public` zero-argument constructor. Your crash is saying that `SlideFragment` does not have a `public` zero-argument constructor. Note that your code is for a different class (`SideMenuFragment`). – CommonsWare Mar 12 '19 at 11:04
  • @CommonsWare I have Already declared public in all constructor. and specially SlideFargment has a public constructor. – Mohit Lakhanpal Mar 12 '19 at 11:30
  • It needs to be a `public` **zero-argument** constructor. According to the crash, you lack such a constructor. – CommonsWare Mar 12 '19 at 12:09
  • @CommonsWare Im a newbie and facing the same issue. Can you please explain where exactly I want to create constructor and how? – Shihas Sep 16 '19 at 07:21
  • @CommonsWare You want to create two constructors one is the default and one is custom – Mohit Lakhanpal Sep 16 '19 at 07:53
  • @Shihas: How to create a fragment class is covered in [the documentation](https://developer.android.com/training/basics/fragments/creating) and any modern book or course on Android app development. [Here is a fragment class](https://gitlab.com/commonsguy/cw-jetpack-java/blob/v0.5/FragmentManual/src/main/java/com/commonsware/jetpack/samplerj/fragments/ListFragment.java) from [one of my books](https://commonsware.com/Jetpack). By having no constructors, you automatically get the default public zero-argument constructor that the framework needs. – CommonsWare Sep 16 '19 at 10:32
  • @MohitLakhanpal: I do not recommend that you implement any constructors on your fragment class. Let the compiler-supplied public zero-argument constructor handle matters. – CommonsWare Sep 16 '19 at 10:33
  • @CommonsWare Thanks for you reply. Even if I make nullable, its showing same error.. – Shihas Sep 16 '19 at 10:50
  • @Shihas: Delete the constructor completely. Worst-case, remove the constructor parameter completely. – CommonsWare Sep 16 '19 at 10:59

1 Answers1

1

This example on Kotlin, but you can do it in Java too

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)
    }
}
Boken
  • 4,825
  • 10
  • 32
  • 42
  • Try convert Kotlin to Java, Android studio can do it. Check this post: https://stackoverflow.com/questions/50895662/converting-kotlin-to-java – jDroidCoder Group Mar 12 '19 at 11:30
  • Group Tools -> Kotlin -> Show Kotlin Bytecode option is missing and Tools >> kotlin >> Show Kotlin bytecode >> Decompile also missing – Mohit Lakhanpal Mar 12 '19 at 11:50