FragmentPagerAdapter方法,getItem()错误

时间:2015-12-30 13:05:17

标签: android sqlite android-fragments

这是我的情况:我想展示一些片段。在我的情况下,我有一个sqlite数据库与一些学生,字段是: _id,姓名,姓氏,性别,photoSize,field1,field2,field3,field4。例如,如果field1 == 1我必须显示片段1,如果field2 == 2我必须显示片段2等。我不知道自己做错了什么。这是代码。

public class SectionsPagerAdapter extends FragmentPagerAdapter {

        Bundle b = getIntent().getExtras();
        int i = 2;
        Cursor c;
        Cursor c2;

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            // getItem is called to instantiate the fragment for the given page.
            // Return a PlaceholderFragment (defined as a static inner class below).
            String id = b.getString("id");
            c2 = dbm.buscarAlumno(id);
            if (c.moveToFirst()) {

                switch (position) {
                    case 0:
                        if (c2.getString(5).equals("1")) {
                            return PlaceholderFragment.newInstance(position);
                        } else {
                            break;
                        }
                    case 1:
                        if (c2.getString(6).equals("1")) {
                            return PlaceholderFragment.newInstance(position);
                        } else {
                            break;
                        }
                    case 2:
                        if (c2.getString(7).equals("1")) {
                            return PlaceholderFragment.newInstance(position);
                        } else {
                            break;
                        }
                    case 3:
                        if (c2.getString(8).equals("1")) {
                            return PlaceholderFragment.newInstance(position);
                        } else {
                            break;
                        }
                    case 4:
                        return PlaceholderFragment.newInstance(position);
                }
            }
            return null;
        }

        @Override
        public int getCount() {
            // Show 1 total pages.
            return 5;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            String id = b.getString("id");
            c = dbm.buscarAlumno(id);
            if (c.moveToFirst()) {
                switch (position) {
                    case 0:
                        if (c.getString(5).equals("1")){
                            return "PISTA";
                        }
                    case 1:
                        if (c.getString(6).equals("1")){
                            return "ESTABLO";
                        }else{break;}
                    case 2:
                        if (c.getString(7).equals("1")){
                            return "EMOCIONES";
                        }else{break;}
                    case 3:
                        if (c.getString(8).equals("1")){
                            return "NECESIDADES";
                        }else{break;}
                    case 4:
                        return b.getString("alumno");
                }
            }return null;
        }
    }

dbm变量在容器类中声明,表示DataBaseManager,而buscarAlumno(id)是返回学生的方法,并且发誓该方法工作正常。这是logcat:

12-30 07:43:14.419 3104-3104/? W/EGL_emulation: eglSurfaceAttrib not implemented
12-30 07:43:14.429 3104-3104/? D/OpenGLRenderer: Enabling debug mode 0
12-30 07:43:17.849 1340-1354/? W/Binder: Caught a RuntimeException from the binder stub implementation.
                                         java.lang.NullPointerException
                                             at android.inputmethodservice.IInputMethodWrapper.setSessionEnabled(IInputMethodWrapper.java:280)
                                             at com.android.internal.view.IInputMethod$Stub.onTransact(IInputMethod.java:129)
                                             at android.os.Binder.execTransact(Binder.java:404)
                                             at dalvik.system.NativeStart.run(Native Method)
12-30 07:43:17.849 1253-1353/? W/InputMethodManagerService: Got RemoteException sending setActive(false) notification to pid 3076 uid 10057

如果我不使用数据库,那就完美了。

请帮忙!谢谢大家!

1 个答案:

答案 0 :(得分:0)

您似乎从null返回getItem() - 这就是您获得NullPointerException的原因。您的getItem()允许在某些情况下返回空指针。例如,如果item为0且后面的内容不为真:

if (c2.getString(5).equals("1")) {

然后返回空指针,与getPageTitle相同,您可能会在此处返回null