0

Estou tentando usar este exemplo Daimajia / AndroidImageSlider, não obtive sucesso..alguem pode me ajudar

' 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-- linha ---slImages = (SliderLayout) getView().findViewById(R.id.slImages);

1
  • Tem como você colocar na sua pergunta o que você tentou e o que está dando errado?! Ficara mais fácil alguém te ajudar.
    – viana
    Commented 11/07/2017 às 2:25

1 Resposta 1

0

Você tem que mover o código do onCreate para o onCreateView e referenciar as views do layout dessa forma:

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;

}
3
  • Implementei no onCreateView mas estou tento este erro agora logcat Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference na linha slImages.setCustomIndicator((PagerIndicator)getView().findViewById(R.id.custom_indicator)); Commented 11/07/2017 às 18:12
  • troca o getView() por rootView Commented 11/07/2017 às 18:43
  • Deu certo a implementação valeu.... Commented 12/07/2017 às 1:52

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .