android片段替换为参数nullpointexception

时间:2014-09-30 10:52:43

标签: android android-fragments android-viewpager

我在fragment中工作。我试图用参数替换片段。我想解释我的问题

我有一些framgnets。 在第一个片段中,我尝试替换包含viewc的第二个片段,这个viewpager也包含两个片段。

MoviewListResult是一个包含viewpagers的片段,此viewpager还包含两个MoviesRolls和ReadMoreAboutMovies。我在Bundle putstring中有nullpoint异常 这是我的来源

MoviewListResult newFragment = new MoviewListResult();

            FragmentTransaction transaction = getFragmentManager()
                    .beginTransaction();

            Bundle bundle = new Bundle();




            bundle.putString("image", arrayOfList.get(position).getBlurimage());


             newFragment.setArguments(bundle);
                transaction.replace(R.id.content_frame, newFragment);

                transaction.commit();

这是一个viewpager的片段代码

public class MoviewListResult extends Fragment {

// private TextView movies_title, category, descraption, writen_by_1,
// stars_1,
// georgian_time_1, world_time_1;
private ImageView play_trailer;


private MoviesRolls mfragment4;
private ReadMoreAboutMovies mfragment3;
private ArrayList<Fragment> fragmentList;
ViewPager mViewPager;
SectionsPagerAdapter mSe;



// private String image, title, category1, descraption1, writen_by, stars,
// georgian_time, world_time;

public final static String TAG = MoviewListResult.class.getSimpleName();

public static MoviewListResult newInstance() {
    return new MoviewListResult();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View v = inflater
            .inflate(R.layout.moview_list_result, container, false);


    play_trailer = (ImageView) v.findViewById(R.id.play_trailer);


    mSe = new SectionsPagerAdapter(getChildFragmentManager());



    mViewPager = (ViewPager) v.findViewById(R.id.pager1);

    mViewPager.setAdapter(mSe);


    // circle.seta(Color.parseColor("#ff0000"));

    mfragment3 = new ReadMoreAboutMovies();
    mfragment4 = new MoviesRolls();

    fragmentList = new ArrayList<Fragment>();
    fragmentList.add(mfragment3);
    fragmentList.add(mfragment4);

    mViewPager.setPageTransformer(false, new PageTransformer() {

        @Override
        public void transformPage(View page, float position) {
            page.setRotationY(position * -40);

        }
    });
    getItem(0);

    mViewPager.setOnPageChangeListener(new OnPageChangeListener() {

        @Override
        public void onPageSelected(int newPosition) {


            RelativeLayout.LayoutParams parms = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.FILL_PARENT,
                    RelativeLayout.LayoutParams.MATCH_PARENT);
            //parms.addRule(RelativeLayout.BELOW, R.id.main_tmp_button);
            mViewPager.setLayoutParams(parms);

            getItem(newPosition);

        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {

        }

        @Override
        public void onPageScrollStateChanged(int arg0) {


        }
    });




    play_trailer.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getActivity(), PlayTrailer.class);
            startActivity(intent);

        }
    });



    mViewPager.setOnTouchListener(new OnSwipeTouchListener(getActivity()) {
        @Override
        public boolean onSwipeTop() {

            RelativeLayout.LayoutParams parms = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.FILL_PARENT,
                    RelativeLayout.LayoutParams.MATCH_PARENT);
            parms.addRule(RelativeLayout.BELOW, R.id.main_tmp_button);
            mViewPager.setLayoutParams(parms);

            //StradaChefs3.popaplayout.setLayoutParams(parms);

            return true;
        }

        @Override
        public boolean onSwipeRight() {

            return true;
        }

        @Override
        public boolean onSwipeLeft() {

            return true;
        }

        @Override
        public boolean onSwipeBottom() {

            final float scale = getResources().getDisplayMetrics().density;
            int pixels = (int) (200 * scale + 0.5f);

            RelativeLayout.LayoutParams rel_btn = new RelativeLayout.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT, pixels);
            rel_btn.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
            mViewPager.setLayoutParams(rel_btn);





            return true;
        }
    });

    return v;
}





public class SectionsPagerAdapter extends FragmentPagerAdapter {

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

    @Override
    public Fragment getItem(int position) {
        return fragmentList.get(position);
    }

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

    @Override
    public CharSequence getPageTitle(int position) {

        return null;
    }
}


public Fragment getItem(int position) {

    Fragment newFragment = null;
    switch (position) {
        case 0:
            newFragment = ReadMoreAboutMovies.newInstance();
            Bundle bundle = new Bundle();
            bundle.putString("image", MainmoviesList.arrayOfList.get(position).getBlurimage());

            newFragment.setArguments(bundle);
            break;
        case 1:
            newFragment = MoviesRolls.newInstance();
            break;

        default:

            break;
    }

    return newFragment;
}

}

这是一个viewpager的片段代码

public class ReadMoreAboutMovies extends Fragment {

public static RelativeLayout popaplayout;
public static NetworkImageView images;
public static ScrollView scrollpopap;
private String image, title, category1, descraption1, writen_by, stars,
georgian_time, world_time,youtube;

public static TextView movies_title, category, descraption, writen_by_1, stars_1,
        georgian_time_1, world_time_1;

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

    View rootView = inflater.inflate(R.layout.readmoreaboutmoview, container,
            false);
    popaplayout = (RelativeLayout) rootView.findViewById(R.id.popaplayout);

    images = (NetworkImageView) rootView.findViewById(R.id.image);

    scrollpopap=(ScrollView)rootView.findViewById(R.id.scrollpopap);
    ImageLoader imageLoader = AppController.getInstance().getImageLoader();
    movies_title = (TextView)rootView. findViewById(R.id.movies_title);
    category = (TextView) rootView.findViewById(R.id.category);
    descraption = (TextView) rootView.findViewById(R.id.descraption);
    writen_by_1 = (TextView) rootView.findViewById(R.id.writen_by_1);
    stars_1 = (TextView) rootView.findViewById(R.id.stars_1);
    georgian_time_1 = (TextView) rootView.findViewById(R.id.georgian_time_1);
    world_time_1 = (TextView) rootView.findViewById(R.id.world_time_1);







    Bundle bundle = getArguments();
    image = bundle.getString("image");


    return rootView;
}

我在las java类中有错误。 我在这一行中有nullpointexception

    Bundle bundle = getArguments();
    image = bundle.getString("image");

我怎么能解决我的问题? 如果有人知道解决方案,请帮助我 感谢

1 个答案:

答案 0 :(得分:0)

没有分配给ReadMoreAboutMovies片段的包,只有你的MoviewListResult片段

您正在将ReadMoreAboutMovie添加到列表中,但不会将任何包传递给它。

您应该在返回片段时在getItem()方法中添加bundle。所以相反:

    public Fragment getItem(int position) {

        Fragment newFragment;
        switch (position) {
            case 0:
                newFragment = YourFirstFragment.newInstance();
                Bundle bundle = new Bundle();
                bundle.putString("image", arrayOfList.get(position).getBlurimage());
                //add other items
                newFragment.setArguments(bundle);
                break;
            case 1:
                newFragment = YourSecondFragment.newInstance();
                break;
            case 2:
                newFragment = YourThirdFragment.newInstance();
                break;
            default:
                //Do whatever
                break;
        }

        return newFragment;
    }