无法将应用程序强制转换为androidx.appcompat.app.AppCompatActivity

时间:2019-09-07 07:47:50

标签: java android

所以基本上我想从Adapter过渡到片段。

AppCompatActivity activity = (AppCompatActivity) view.getContext();

                Fragment fragments = new Subject_Chapter_Fragment();
                FragmentManager fragmentManager = activity.getSupportFragmentManager();
                FragmentTransaction ft = fragmentManager.beginTransaction();
                ft.replace(R.id.frame_container, fragments);
                ft.commit();
java.lang.ClassCastException: android.app.Application cannot be cast to androidx.appcompat.app.AppCompatActivity
        at soft.gen.studentapp.Activities.adapter.Adapter_Subjects$1.onClick(Adapter_Subjects.java:45)

1 个答案:

答案 0 :(得分:0)

尝试获取应用上下文,然后将其投放到您的活动中

AppCompatActivity activity = (AppCompatActivity) view.getContext().getApplicationContext();