ViewPager在片段中。如何处理它

时间:2015-03-24 01:40:45

标签: android android-fragments android-viewpager

这是我的代码。

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.daimajia.slider.library.Animations.DescriptionAnimation;
import com.daimajia.slider.library.SliderLayout;
import com.daimajia.slider.library.SliderTypes.BaseSliderView;
import com.daimajia.slider.library.SliderTypes.TextSliderView;

import java.util.HashMap;

public class S1 extends Fragment implements BaseSliderView.OnSliderClickListener{

SliderLayout slider;

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

    slider = (SliderLayout)getActivity().findViewById(R.id.slider);

    HashMap<String,String> url_maps = new HashMap<String, String>();
    url_maps.put("Hannibal", "http://bybar.besaba.com/M1F1/1.png");
    url_maps.put("Big Bang Theory", "http://tvfiles.alphacoders.com/100/hdclearart-10.png");
    url_maps.put("House of Cards", "http://cdn3.nflximg.net/images/3093/2043093.jpg");
    url_maps.put("Game of Thrones",         "http://images.boomsbeat.com/data/images/full/19640/game-of-thrones-season-4-jpg.jpg");

    for(String name : url_maps.keySet()){
        TextSliderView textSliderView = new TextSliderView(getActivity());

        textSliderView
                .description(name)
                .image(url_maps.get(name))
                .setScaleType(BaseSliderView.ScaleType.Fit)
                .setOnSliderClickListener(this);


        textSliderView.getBundle()
                .putString("extra",name);

        slider.addSlider(textSliderView);
    }
    slider.setPresetTransformer(SliderLayout.Transformer.Accordion);
    slider.setPresetIndicator(SliderLayout.PresetIndicators.Center_Bottom);
    slider.setCustomAnimation(new DescriptionAnimation());
    slider.setDuration(4000);

    return inflater.inflate(R.layout.fragment_s1, container, false);
}
@Override
public void onSliderClick(BaseSliderView slider) {
    Toast.makeText(getActivity(),slider.getBundle().get("extra") + "",Toast.LENGTH_SHORT).show();
}


}

我在这里收到错误

slider.setPresetTransformer(SliderLayout.Transformer.Accordion);

这是log

    java.lang.NullPointerException
        at bybar.biyer.S1.onCreateView(S1.java:27)
        at         android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:953)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1136)
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1499)
        at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:488)
        at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
        at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441)
        at android.view.View.measure(View.java:17479)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5364)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at android.view.View.measure(View.java:17479)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5364)
        at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:453)
        at android.view.View.measure(View.java:17479)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5364)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at android.view.View.measure(View.java:17479)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5364)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
        at android.view.View.measure(View.java:17479)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5364)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2532)
        at android.view.View.measure(View.java:17479)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2262)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1391)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1590)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1249)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6585)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:803)
        at android.view.Choreographer.doCallbacks(Choreographer.java:603)
        at android.view.Choreographer.doFrame(Choreographer.java:573)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:789)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5586)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
        at dalvik.system.NativeStart.main(Native Method)

S1是我活动中的片段。我想在这个片段中使用viewpager。它会导致此错误。我正在为2个飞蛾开发android。我无法处理它。我应该在这里做什么?

或者我使用错误的viewpager lib来执行此操作。如果我将我的viewpager更改为另一个(来自SliderLayout)?

0 个答案:

没有答案