我如何在片段中添加imageslider以及为什么我会得到适配器(适配器)错误?

时间:2016-06-20 13:06:51

标签: android android-fragments nullpointerexception

您好我试图将imageslider添加到我的活动中,但我收到错误

关注this tutorial..

在这个位置,我在第45行得到了这个错误:

  

引起:java.lang.NullPointerException:尝试调用虚拟   方法'无效   android.support.v4.view.ViewPager.setAdapter(android.support.v4.view.PagerAdapter)'   在空对象引用上

第45行是:

sds.setAdapter(adapter);

这是我的actvity.java

public class Sayfalar extends AppCompatActivity implements View.OnClickListener {

    private TabLayout tabLayout;
    private ViewPager viewPager,sds;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sayfalar);

        sds = (ViewPager) findViewById(R.id.resimler);
        PagerAdapter adapter = new CustomAdapter(Sayfalar.this);
        sds.setAdapter(adapter);


        viewPager = (ViewPager) findViewById(R.id.viewpager);
        setupViewPager(viewPager);

        tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);

    }

    public void showDialog (View v){
        MyDialog dialog = new MyDialog();
        dialog.show(getSupportFragmentManager(),"my_dialog");
    }

    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFrag(new Fragment1(), "Fragment1");
        adapter.addFrag(new Fragment2(), "Fragment2");
        adapter.addFrag(new Fragment3(), "Fragment3");
        adapter.addFrag(new Fragment4(), "Fragment4");
        adapter.addFrag(new Fragment5(), "Fragment5");
        adapter.addFrag(new Fragment6(), "Fragment6");
        adapter.addFrag(new Fragment7(), "Fragment7");
        adapter.addFrag(new Fragment8(), "Fragment8");
        adapter.addFrag(new Fragment9(), "Fragment9");
        viewPager.setAdapter(adapter);
    }

    public void onClick(View view) {


        }

    class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

        public ViewPagerAdapter(FragmentManager manager) {
            super(manager);
        }

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

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

        public void addFrag(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }
    }
}

这是我的activity.xml

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/popupmenu"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabMode="scrollable"/>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>

如果我将此代码添加到我的activity.xml中,我会在所有片段中看到我的图片,但我希望它在一个片段中。

以下是代码:

<ImageView
        android:id="@+id/resim"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

这是fragment.xml之一

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Kaynaklar.Agil">


    <android.support.v4.view.ViewPager
        android:id="@+id/resimler"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
</RelativeLayout>

这里是fragment1.java

public class Fragment1 extends Fragment {

    public Fragment1 () {
        // Required empty public constructor
    }

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment1, container, false);
    }

}

3 个答案:

答案 0 :(得分:1)

问题在于:

sds = (ViewPager) findViewById(R.id.resimler);
PagerAdapter adapter = new CustomAdapter(Sayfalar.this);
sds.setAdapter(adapter);

您的ViewPager布局中似乎没有sayfalar resimler ID,因此sds将为null,并且然后,您尝试在setAdapter()引用上致电null

要设置ViewPager中的第二个Fragment1,您可以执行以下操作:

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

    ViewPager sds = (ViewPager) view.findViewById(R.id.resimler);
    // assuming CustomAdapter requires an Activity or Context as a constructor param
    PagerAdapter adapter = new CustomAdapter(getActivity());
    sds.setAdapter(adapter);

    return view;
}

答案 1 :(得分:0)

你犯了一个错误。您无法访问Activity中的R.id.resimler,因为viewPager是在Fragment的xml中声明的。 (这就是为什么你为这个变量得到null):

sds = (ViewPager) findViewById(R.id.resimler);

为了引用此viewPager,您需要做的是将此代码迁移到Fragment的方法onCreateView中,如下所示:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment1, container, false);
    ViewPager sds = (ViewPager) view.findViewById(R.id.resimler);
    //and do whatever you want with the ViewPager at this point
    return view;
}

答案 2 :(得分:0)

您可以检查此代码。

public class Sayfalar extends AppCompatActivity {
    private TabLayout tabLayout;
    private ViewPager viewPager,sds;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sayfalar);

        viewPager = (ViewPager) findViewById(R.id.viewpager);
        setupViewPager(viewPager);

        tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);

    }

    public void showDialog (View v){
        MyDialog dialog = new MyDialog();
        dialog.show(getSupportFragmentManager(),"my_dialog");
    }

    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(adapter);
    }

    public void onClick(View view) {


    }

    class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

        public ViewPagerAdapter(FragmentManager manager) {
            super(manager);
        }

        @Override
        public Fragment getItem(int position) {
            Fragment1 fragment1=Fragment1.newInstance(position);
            return fragment1;
        }

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

        public void addFrag(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return "title "+position;
        }
    }
}

//片段

   public class Fragment1 extends Fragment {
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View view=inflater.inflate(R.layout.fragment,container,false);
            return view;
        }

        @Override
        public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
            ImageView imageView= (ImageView) view.findViewById(R.id.imageview);
            imageView.setImageResource(R.mipmap.ic_launcher);
        }

        public static Fragment1 newInstance(int position) {
            Fragment1 fr=new Fragment1();
            Bundle bundle=new Bundle();
            bundle.putInt("position",position);
            fr.setArguments(bundle);
            return fr;
        }
    }