类型不匹配无法从我的活动转化为分片 [英] Type mismatch cannot convert from My Activity to Fragment

查看:99
本文介绍了类型不匹配无法从我的活动转化为分片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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?

推荐答案

请看看下面的code。我告诉你,我展开了如何创建寻呼机的看法:

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

MainActivity类:

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

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

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 我创建了常见的布局,所有片段。我会改变背景颜色和设置在下面源$ C ​​$ C文本。

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

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

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

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;
    }
}

下面是完整的程序巫婆显示5片段与作为参数传递的文本。请您尝试一下。如果你有一个问题或某事在我的解决方案是不明确或者我的解决方案是不是对你有帮助。来吧,只问。 :)

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. :)

这篇关于类型不匹配无法从我的活动转化为分片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆