导航抽屉和动态创建的片段冲突

时间:2016-03-12 15:05:18

标签: android-fragments

我在android studio中使用默认的Navigation Drawer,我有一个动态创建的片段,并使用TabLayout来运行它。它将正常运行,但当我尝试从导航抽屉加载相同的片段时,我遇到错误。请参阅以下内容:

viewController

但是当我尝试从导航抽屉加载它时出现错误错误:(115,62)错误:不兼容类型:简介无法转换为片段

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;



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


        final RelativeLayout relativeLayout = new RelativeLayout(getActivity());
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
        relativeLayout.setLayoutParams(layoutParams);

        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);

        final ImageView introImage = new ImageView(getActivity());
        introImage.setId(R.id.introImageId);
        introImage.setImageResource(R.drawable.introduction);

        introImage.setAnimation(AnimationUtils.loadAnimation(this.getContext(), android.R.anim.slide_out_right));

        introImage.setPadding(100, 100, 100, 100);

        final TextView introText = new TextView(getActivity());
        introText.setText(R.string.introduction_text);
       // introText.setTextAppearance(android.R.style.TextAppearance_Medium);
        introText.setTextSize(20);
        introText.setPadding(25,25,25,25);
        introText.setTextColor(ContextCompat.getColor(this.getContext(), R.color.colorPrimary));
        params.addRule(RelativeLayout.BELOW, introImage.getId());
        introText.setAnimation(AnimationUtils.loadAnimation(this.getContext(),android.R.anim.slide_in_left));
        relativeLayout.addView(introText,params);
        relativeLayout.addView(introImage);


        return relativeLayout;

    }

}
  

TabPagerAdapter

 public boolean onNavigationItemSelected(MenuItem item) {

        FragmentManager fn = getFragmentManager();

        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_introduction) {

            fn.beginTransaction().replace(R.id.content_frame,new Introduction()).commit();

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

1 个答案:

答案 0 :(得分:0)

我替换了getFragmentManager();使用getSupportFragmentManager();及其工作

 // FragmentManager fn = getFragmentManager();
            FragmentManager fn = getSupportFragmentManager();