Search code examples
androidandroid-fragmentsfragmentstatepageradapter

Android Images not appear in Fragment


I made an application extending an activity, all worked fine. To improve the application I changed the activity to Fragment (using onCreateView , onActivityCreated), but now all the images not shown. I don't get any errors.

I open the Fragments inside the FragmentActivity using FragmentStatePagerAdapter.

Is there something I'm missing ?

Fragment XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:padding="10dp"
    >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minWidth="170dp"
        android:maxWidth="170dp"
        android:minHeight="170dp"
        android:maxHeight="170dp"
        app:srcCompat="@drawable/logo_pic"
        android:id="@+id/iv_profile_img"
        android:layout_below="@+id/relativeview_search_bar"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="12dp"
        android:background="@drawable/circle_image"
        />
</RelativeLayout>

Fragment Class:

public class TempActivity extends Fragment {

    private static final String TAG = "SearchCars";

    ArrayList<ListUser> userChatItems;
    MessagesAdapter mAdapter;

    FragmentActivity con;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_listview_users_profiles, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //con = this.getContext();
        con = this.getActivity();

        Log.v(TAG, "Initializing ...");

        View v = getView();
    }
}

PageAdapter:

public class PageAdapter extends FragmentStatePagerAdapter {

    public PageAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return new MyProfileActivity();
            default:
                break;
        }
        return null;
    }

    @Override
    public int getCount() {
        return 1;
    }
}

Main Fragment Activity:

public class MainFragmentActivity extends FragmentActivity{

    ViewPager viewpager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_fragment);

        viewpager = (ViewPager) findViewById(R.id.pager);
        PageAdapter pageAdapter = new PageAdapter(getSupportFragmentManager());
        viewpager.setAdapter(pageAdapter);
    }

}

Solution

  • Well I found that if I change to android:src instead of app:srcCompat all is working fine.

    It's probably better to use app:secCompat when extending from AppCompatActivity But, when I use Fragment it doesn't work any more.