mercredi 30 décembre 2015

Fragment: getItem() mistake

this is my situation: I want to show a few fragments. In my case, I have a sqlite database with a some students, the fields are: _id, Name, Lastname, sex, photoSize, field1, field2, field3, field4. For example, if field1 == 1 i have to show the fragment 1, if field2 == 2 i have to show the fragment 2 and so.I'd like to select which fragment I'm gonna show. I don't know what i am doing wrong. Here is the code.

public class SectionsPagerAdapter extends FragmentPagerAdapter {

        Bundle b = getIntent().getExtras();
        int i = 5;


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

        @Override
        public Fragment getItem(int position) {

            String id = b.getString("id");
            c1 = dbm.buscarAlumno(id);
            // getItem is called to instantiate the fragment for the given page.
            // Return a PlaceholderFragment (defined as a static inner class below).
            try {
                if (c1.moveToFirst()){
                    switch (position){
                        case 0:
                            if (c1.getString(5).equals("1")){
                                return PlaceholderFragment.newInstance(position);
                            }
                        case 1:
                            if (c1.getString(6).equals("1")){
                                return PlaceholderFragment.newInstance(position);
                            }
                        case 2:
                            if (c1.getString(7).equals("1")){
                                return PlaceholderFragment.newInstance(position);
                            }
                        case 3:
                            if (c1.getString(8).equals("1")){
                                return PlaceholderFragment.newInstance(position);
                            }
                        case 4:
                                return PlaceholderFragment.newInstance(position);
                    }
                }

            }
            finally {
                c1.close();
            }
        return null;

        }

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

        @Override
        public CharSequence getPageTitle(int position) {
            String id = b.getString("id");
            c = dbm.buscarAlumno(id);
            try {
                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");
                    }
                }

            }
            finally {
                c.close();
            }
            return null;
        }
    }

getItem() doesn't work, but getPageTitle works fine. The dbm variable is declare in the container class, means DataBaseManager, and buscarAlumno(id) is the method that returns the student, and swear that this method works perfect. Here is the logcat errors:

12-30 11:48:00.168 1847-1847/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                 Process: laboratorio.hermes2, PID: 1847
                                                 java.lang.RuntimeException: Unable to instantiate application android.app.Application: java.lang.IllegalStateException: Unable to get package info for laboratorio.hermes2; is package not installed?
                                                     at android.app.LoadedApk.makeApplication(LoadedApk.java:507)
                                                     at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4301)
                                                     at android.app.ActivityThread.access$1500(ActivityThread.java:135)
                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
                                                     at android.os.Handler.dispatchMessage(Handler.java:102)
                                                     at android.os.Looper.loop(Looper.java:136)
                                                     at android.app.ActivityThread.main(ActivityThread.java:5001)
                                                     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:785)
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
                                                     at dalvik.system.NativeStart.main(Native Method)
                                                  Caused by: java.lang.IllegalStateException: Unable to get package info for laboratorio.hermes2; is package not installed?
                                                     at android.app.LoadedApk.initializeJavaContextClassLoader(LoadedApk.java:368)
                                                     at android.app.LoadedApk.getClassLoader(LoadedApk.java:321)
                                                     at android.app.LoadedApk.makeApplication(LoadedApk.java:500)
                                                     at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4301) 
                                                     at android.app.ActivityThread.access$1500(ActivityThread.java:135) 
                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256) 
                                                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                     at android.os.Looper.loop(Looper.java:136) 
                                                     at android.app.ActivityThread.main(ActivityThread.java:5001) 
                                                     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:785) 
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
                                                     at dalvik.system.NativeStart.main(Native Method) 
12-30 11:48:00.178 1389-1389/? W/EGL_emulation: eglSurfaceAttrib not implemented
12-30 11:48:00.178 1253-1264/? W/InputMethodManagerService: Got RemoteException sending setActive(false) notification to pid 1783 uid 10057
12-30 11:48:00.198 1356-1367/? 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 11:48:06.238 1389-1389/? E/ActivityThread: Performing stop of activity that is not resumed: {com.android.launcher/com.android.launcher2.Launcher}
                                                 java.lang.RuntimeException: Performing stop of activity that is not resumed: {com.android.launcher/com.android.launcher2.Launcher}
                                                     at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3133)
                                                     at android.app.ActivityThread.handleStopActivity(ActivityThread.java:3220)
                                                     at android.app.ActivityThread.access$1100(ActivityThread.java:135)
                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1223)
                                                     at android.os.Handler.dispatchMessage(Handler.java:102)
                                                     at android.os.Looper.loop(Looper.java:136)
                                                     at android.app.ActivityThread.main(ActivityThread.java:5001)
                                                     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:785)
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
                                                     at dalvik.system.NativeStart.main(Native Method)



12-30 11:55:16.425 1356-1368/? 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)

Please help! Thanks.

Aucun commentaire:

Enregistrer un commentaire