I’m trying to create a Fragment that has an image slide any idea?

Asked

Viewed 52 times

0

I am trying to use this example Daimajia / Androidimageslider, did not succeed.. someone can help me

' public class Fragmentteste 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);

  • Can you put in your question what you tried and what is going wrong?! It will be easier for someone to help you.

1 answer

0


You have to move the onCreate code to onCreateView and reference the layout views 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;

}
  • I implemented on onCreateView but am trying this logcat error now Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference on the line slImages.setCustomIndicator((PagerIndicator)getView().findViewById(R.id.custom_indicator));

  • trade getView() for rootView

  • It worked out the implementation was worth....

Browser other questions tagged

You are not signed in. Login or sign up in order to post.