YoKeyword / Fragmentation

[DEPRECATED] A powerful library that manage Fragment for Android
Apache License 2.0
9.72k stars 2.11k forks source link

求助大佬,使用了androidx包后出现类型转换异常,但V7包可以 #1239

Open OneTake1998 opened 4 years ago

OneTake1998 commented 4 years ago

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mall/com.example.mall.MainActivity}: java.lang.ClassCastException: com.example.mall.MainActivity cannot be cast to me.yokeyword.fragmentation.SupportActivity

abstract class ProxyActivity :
    AppCompatActivity(), ISupportActivity {
    private lateinit var mDelegate: SupportActivityDelegate
    abstract fun setRootFragment(): MallFragment

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mDelegate = SupportActivityDelegate(this)
        mDelegate.onCreate(savedInstanceState)
        initContainer(savedInstanceState)
    }

    private fun initContainer(savedInstanceState: Bundle?) {
        val container = FrameLayout(this)
        container.id = R.id.fragment_container
        setContentView(container)
        if (savedInstanceState == null) {
            mDelegate.loadRootFragment(
                R.id.fragment_container,
                setRootFragment()
            )
        }
    }

    override fun onDestroy() {
        mDelegate.onDestroy()
        super.onDestroy()
        System.gc()
        System.runFinalization()
    }

    override fun getSupportDelegate(): SupportActivityDelegate {
        return mDelegate
    }

    override fun post(runnable: Runnable?) {
        mDelegate.post(runnable)
    }

    override fun extraTransaction(): ExtraTransaction {
        return mDelegate.extraTransaction()
    }

    override fun getFragmentAnimator(): FragmentAnimator {
        return mDelegate.fragmentAnimator
    }

    override fun setFragmentAnimator(fragmentAnimator: FragmentAnimator) {
        mDelegate.fragmentAnimator = DefaultHorizontalAnimator()
    }

    override fun onCreateFragmentAnimator(): FragmentAnimator {
        return mDelegate.onCreateFragmentAnimator()
    }

    override fun onBackPressedSupport() {
        mDelegate.onBackPressedSupport()
    }

    override fun onBackPressed() {
        mDelegate.onBackPressed()
    }
}

`class MainActivity : ProxyActivity() {

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

override fun setRootFragment(): MallFragment {
    return TestFragment()
}

}`

`abstract class BaseFragment : SupportFragment() {

private lateinit var mRootView: View

abstract fun setLayout(): Any

abstract fun onBindView(savedInstanceState: Bundle?, rootView: View)

fun <T : View> findView(@IdRes viewId: Int): T {
    return mRootView.findViewById(viewId)
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    val rootView: View = when {
        setLayout() is Int -> inflater.inflate(setLayout() as Int, container, false)
        setLayout() is View -> setLayout() as View
        else -> throw ClassCastException("type of setLayout() must be int or view")
    }
    mRootView = rootView
    onBindView(savedInstanceState, rootView)
    return rootView
}

}`

`abstract class MallFragment : BaseFragment() {

private var mTouchTime: Long = 0

companion object {
    private const val WAIT_TIME = 2000L
}

protected fun exitWithDoubleClick(): Boolean {

    if (System.currentTimeMillis() - mTouchTime < WAIT_TIME) {
        _mActivity.finish()
    } else {
        mTouchTime = System.currentTimeMillis()
        Toast.makeText(_mActivity, "双击退出", Toast.LENGTH_SHORT).show()
    }

    return true
}

//关于文件或者Camera权限的内容

}`

JantHsueh commented 4 years ago

我看到你在我维护的项目下 https://github.com/JantHsueh/Fragmentation/issues 也提了同样的问题,已经在那边答复了。

同时建议使用我维护的新版本 1.0.4

testping commented 4 years ago

我也遇到了在慕课网的学习视频类型转换异常