Summary of Fragment inheritance issues on Android

1. Discovery of the problem   

   In the preliminary Android learning, I first came into contact with Fragment, a tool for implementing interface conversion, but found some problems when using it. When creating the Fragment class, let it inherit the Fragment according to the learning video.

But when using add, replace and other functions in the activity, it will report that the parameters do not match, and the required parameters are in the form of android.app.fragment; this problem has made me think for a long time.

2. Problem solving

      First of all, I imported android.app.fragment in the activity but still showed that it was not used. After a lot of information query, it was found that it was a problem of fragment inheritance; when the newly created fragment class inherited android.app.Fragment ,

The reported error disappeared.

3. The root of the problem

   After another large amount of searching, I found the root cause: fragment exists in two classes (androidx.fragment.app.Fragment and android.app.Fragment), they can not be mixed. If you directly inherit fragment

In my configuration, it will inherit the androidx.fragment.app.Fragment class, but the method I use is in the android.app.Fragment class, so it will report an error.

Guess you like

Origin www.cnblogs.com/zgk666/p/12687508.html