Fragment cannot be cast to android.app.activity

会有一股神秘感。 提交于 2019-12-01 01:58:05

You cannot use PostPhotosActivity as activity because it is not an activity. It is fragment because it extends Fragment not Activity. Having this in your manifest is a mistake and calling startActivityForResult on intent

Intent i = new Intent(
PostActivity.this,
PostPhotosActivity.class);

causes the exception in casting because Fragment just cannot be casted to Activity.

You could use xml layout with this fragment in some other activity or change the fragment to activity. Example of xml layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <fragment class="com.example.PostPhotosFragment"
            android:id="@+id/post_photos_fragment"
            android:layout_width="match_parent" android:layout_height="match_parent" />


</LinearLayout>

and then in some activity which extends Activity class call setContentView(R.layout.yourlayout) and your fragment will be created in your activity.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!