Schardt12
Schardt12

Reputation: 11

Type mismatch cannot convert from My Activity to Fragment

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;

public class MainActivity extends FragmentActivity {

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

        ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
        pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
    }

    private class MyPagerAdapter extends FragmentPagerAdapter {

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

        @Override
        public Fragment getItem(int pos) {
            switch(pos) {

            case 0: return FirstFragment.newInstance("FirstFragment, Instance 1");
            case 1: return SecondFragment.newInstance("SecondFragment, Instance 1");
            case 2: return ThirdFragment.newInstance("ThirdFragment, Instance 1");
            case 3: return ThirdFragment.newInstance("ThirdFragment, Instance 2");
            case 4: return ThirdFragment.newInstance("ThirdFragment, Instance 3");
            default: return ThirdFragment.newInstance("ThirdFragment, Default");
            }
        }

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

I am getting a type mismatch error. Basically it says it cannot convert the activity to a fragment. from what I have read it has something to do with my imports. Can anyone help me out?

Upvotes: 0

Views: 1905

Answers (2)

Abdulla Shoshaa
Abdulla Shoshaa

Reputation: 101

I know its late but am submitting this answer because it happens with a lot of developers.

This error appears due to library import conflict. Check your imports in FirstFragment, SecondFragment and ThirdFragment and make sure that you are using the same fragment library.

if you are using Fragments for older APIs you need to import android.support.v4.app.Fragment;

But if you are using Fragments for newer APIs use android.app.Fragment;

The problem comes when you use auto-complete to complete class name you may not notice what library you are importing.

Upvotes: 1

Konrad Krakowiak
Konrad Krakowiak

Reputation: 12365

Please look at the code below. I show you and I expand you how to create pager view:

MainActivity class:

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;

public class MainActivity extends FragmentActivity {

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

        ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
        pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
    }
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
                android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin"
                android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <!-- it is important to use ViewPager from support library -->
    <android.support.v4.view.ViewPager
            android:id="@+id/viewPager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

</RelativeLayout>

MyPagerAdapter.class

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;

public  class MyPagerAdapter extends FragmentStatePagerAdapter {

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

    @Override
    public Fragment getItem(int pos) {
        switch (pos) {

            case 0:
                return FirstFragment.newInstance("FirstFragment, Instance 1");
            case 1:
                return SecondFragment.newInstance("SecondFragment, Instance 1");
            case 2:
                return ThirdFragment.newInstance("ThirdFragment, Instance 1");
            case 3:
                return ThirdFragment.newInstance("ThirdFragment, Instance 2");
            case 4:
                return ThirdFragment.newInstance("ThirdFragment, Instance 3");
            default:
                return ThirdFragment.newInstance("ThirdFragment, Default");
        }
    }

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

fragment_layout.xml I created common layout for all fragments. I will change background colour and set text in source code below.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
    <TextView
            android:id="@+id/text_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"/>
</RelativeLayout>

FirstFragment.class

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class FirstFragment extends Fragment{

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v =inflater.inflate(R.layout.fragment_layout, container, false);
        v.setBackgroundColor(Color.RED);
        TextView textView = (TextView) v.findViewById(R.id.text_view);
        textView.setText(getArguments().getString("value"));
        return v;
    }

    public static FirstFragment newInstance(String s) {
        Bundle args = new Bundle();
        args.putString("value", s);
        FirstFragment result = new FirstFragment();
        result.setArguments(args);
        return result;
    }
}

SecondFragment.class

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class SecondFragment extends Fragment{

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View v =inflater.inflate(R.layout.fragment_layout, container, false);
        v.setBackgroundColor(Color.GRAY);
        TextView textView = (TextView) v.findViewById(R.id.text_view);
        textView.setText(getArguments().getString("value"));
        return v;
    }

    public static SecondFragment newInstance(String s) {
        Bundle args = new Bundle();
        args.putString("value", s);
        SecondFragment result = new SecondFragment();
        result.setArguments(args);
        return result;
    }
}

ThirdFragment.class

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class ThirdFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.fragment_layout, container, false);
        v.setBackgroundColor(Color.BLUE);
        TextView textView = (TextView) v.findViewById(R.id.text_view);
        textView.setText(getArguments().getString("value"));
        return v;
    }

    public static ThirdFragment newInstance(String s) {
        Bundle args = new Bundle();
        args.putString("value", s);
        ThirdFragment result = new ThirdFragment();
        result.setArguments(args);
        return result;
    }
}

Here is complete program witch display 5 fragment with text which is passed as parameter. Please try it. If you have one more question or something in my solution is not clear or my solution is not helpful for you. Go ahead, just ask. :)

Upvotes: 0

Related Questions