I'm trying to create a fragment that has an image slide some idea?

0

I'm trying to use this example Daimajia / AndroidImageSlider, I did not succeed..anyone can help me

'public class FragmentTest extends Fragment implements BaseSliderView.OnSliderClickListener, BaseSliderView.ImageLoadListener {

private String[] images = {
        "http://650-366/b029bkm8cts7ft1979kddqvpa75773e68c2eb27d72ab1d6021f30d9163.jpeg",
        "http://366/13p68advcgehqrsad7cvukfs13460c7eaddd5d02e75bfcd55d07c37ac4.jpeg"};

private String[] effects = {"Default"};

private SliderLayout.Transformer[] effectsId = {
        SliderLayout.Transformer.Default};

private SliderLayout slImages;

public FragmentTeste() {
}

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

}
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // SLIDERLAYOUT
    slImages = (SliderLayout) getView().findViewById(R.id.slImages);

    TextSliderView aux1 = new TextSliderView(getActivity());
    aux1.description("Image Web 1");
    aux1.image(images[0]);
    aux1.setOnSliderClickListener(FragmentInicio.this);
    aux1.setOnImageLoadListener(FragmentInicio.this);
    slImages.addSlider(aux1);

    aux1 = new TextSliderView(getActivity());
    aux1.description("Image Web 2");
    aux1.image(images[1]);
    aux1.setOnSliderClickListener(FragmentInicio.this);
    aux1.setOnImageLoadListener(FragmentInicio.this);
    slImages.addSlider(aux1);

    DefaultSliderView aux2 = new DefaultSliderView(getActivity());
    aux2.image(R.drawable.image_1);
    aux2.setOnSliderClickListener(FragmentInicio.this);
    aux2.description("1");
    slImages.addSlider(aux2);

    aux2 = new DefaultSliderView(getActivity());
    aux2.image(R.drawable.image_2);
    aux2.setOnSliderClickListener(FragmentInicio.this);
    aux2.description("2");
    slImages.addSlider(aux2);

    slImages.setPresetTransformer(effectsId[0]);
    //slImages.setPresetIndicator(SliderLayout.PresetIndicators.Right_Bottom);
    slImages.setCustomIndicator((PagerIndicator)getView().findViewById(R.id.custom_indicator));
    //slImages.setDuration(4000);
    //slImages.setSliderTransformDuration(8000, null);
    slImages.setIndicatorVisibility(PagerIndicator.IndicatorVisibility.Invisible);

    PagerIndicator p = (PagerIndicator)getView().findViewById(R.id.custom_indicator);
    p.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.i("LOG", "ID: "+v.getId());
        }
    });


}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

}

@Override
public void onSliderClick(BaseSliderView baseSliderView) {
    Log.i("LOG", "onSliderClick(" + baseSliderView.getDescription() + ")");
    slImages.startAutoCycle();
}

@Override
public void onStart(BaseSliderView baseSliderView) {
    Log.i("LOG", "onStart(" + baseSliderView.getDescription() + ")");
}

@Override
public void onEnd(boolean b, BaseSliderView baseSliderView) {
    Log.i("LOG", "onEnd(" + baseSliderView.getDescription() + ")");
}

} '  logcat ---- Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById (int)' on a null object reference-- line --- slImages = ( SliderLayout) getView (). FindViewById (R.id.slImages);

    
asked by anonymous 11.07.2017 / 04:24

1 answer

0

You have to move the code from onCreate to onCreateView and reference layout views in this way:

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

    slImages = (SliderLayout) rootView.findViewById(R.id.slImages);

// ... Resto do código

    return rootView;

}
    
11.07.2017 / 14:19