Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
OneTake1998 opened this issue Feb 25, 2020 · 2 comments

Comments

@OneTake1998
Copy link

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
Copy link

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

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

@testping
Copy link

testping commented Sep 2, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants