IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Android Discussion :

Passage de extend Activity à Fragment


Sujet :

Android

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Profil pro
    Inscrit en
    Janvier 2007
    Messages
    83
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2007
    Messages : 83
    Par défaut Passage de extend Activity à Fragment
    Bonjour à tous,
    J'ai commencé une simple application (un textview dans lequel on rentre un nombre, un bouton qui génère une chaine du nombre de caractères saisie avant et un bouton qui convertie les caractères de la chaine avec d'autres caractères). Le tout fonctionne, mais je cherche maintenant à intégrer ce code dans une activité type "fragment" (je souhaite utiliser un Drawer Menu).
    J'ai donc d'un côté un code qui fonctionne avec extend activity et de l'autre le système de Drawer menu qui lui fonctionne avec des fragments.
    Débutant et utilisant les fragments pour la première fois, je n'arrive pas à intégrer mon code dans l'activité.

    le code de basse :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    public class MainActivity extends Activity {
        Random random = new Random();
        private static final String _CHAR = "ATGC "; 
     
     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Button btn = (Button) findViewById(R.id.Gadn);
            btn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
     
                    TextView text_Random = (TextView) findViewById(R.id.adn); 
                    text_Random.setText(getRandomString()); //export sur textview
                }
            });
            Button btn2 = (Button) findViewById(R.id.convert); 
            btn2.setOnClickListener(new OnClickListener() {
     
                @Override
                public void onClick(View v) {
                    TextView convert = (TextView) findViewById(R.id.adn);
                    TextView arn = (TextView) findViewById(R.id.arn);
                    String source = convert.getText().toString();
                    String newValue = source.replace("A", "U");
                    newValue = newValue.replace("T", "A");
                    newValue = newValue.replace("G", "K"); 
                    newValue = newValue.replace("C", "G");
                    newValue = newValue.replace("K", "C");
                    arn.setText(String.valueOf(newValue)); 
                }
            });
        }
     
        public String getRandomString(){
            TextView input = (TextView) findViewById(R.id.input);
            float v = Float.parseFloat(input.getText().toString());
            StringBuffer randStr = new StringBuffer();
            for (int i = 0; i < v; i++) {
                int number = getRandomNumber();
                char ch = _CHAR.charAt(number);
                randStr.append(ch);
            }
            return randStr.toString();
        }
     
             private int getRandomNumber() {
            int randomInt = 0;
            randomInt = random.nextInt(_CHAR.length());
            if (randomInt - 1 == -1) {
                return randomInt;
            } else {
                return randomInt - 1;
     
           }
        }
     
     
    }
    Le code de la page fragment :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    public class menu1_Fragment extends Fragment {
        View rootview;
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            rootview = inflater.inflate(R.layout.menu1_layout, container, false);
            return rootview;
        }
    }
    En cherchant sur le net, j'ai trouvé deux pistes, la première consiste à passer extends Fragment en extends ActivityFragment (ce qui ne fonctionne pas (ou plutôt, je n'y arrive pas)). Dans la seconde méthode je fais :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    public class menu1_Fragment extends Fragment {
        View rootview;
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            rootview = inflater.inflate(R.layout.menu1_layout, container, false);
            return rootview;
        }
        Random random = new Random();
        private static final String _CHAR = "ATGC "; 
     
     
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            //setContentView(R.layout.activity_main);
            Button btn = (Button) rootview.findViewById(R.id.Gadn);
            btn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View rootview) {
     
                    TextView text_Random = (TextView) rootview.findViewById(R.id.adn); 
                    text_Random.setText(getRandomString()); 
                }
            });
            Button btn2 = (Button) rootview.findViewById(R.id.convert); 
            btn2.setOnClickListener(new OnClickListener() {
     
                @Override
                public void onClick(View rootview) {
                    TextView convert = (TextView) rootview.findViewById(R.id.adn);
                    TextView arn = (TextView) rootview.findViewById(R.id.arn);
                    String source = convert.getText().toString();
                    String newValue = source.replace("A", "U");
                    newValue = newValue.replace("T", "A");
                    newValue = newValue.replace("G", "K"); 
                    newValue = newValue.replace("C", "G");
                    newValue = newValue.replace("K", "C");
                    arn.setText(String.valueOf(newValue)); 
                }
            });
        }
     
        public String getRandomString(){
            TextView input = (TextView) rootview.findViewById(R.id.input);
            float v = Float.parseFloat(input.getText().toString());
            StringBuffer randStr = new StringBuffer();
            for (int i = 0; i < v; i++) {
                int number = getRandomNumber();
                char ch = _CHAR.charAt(number);
                randStr.append(ch);
            }
            return randStr.toString();
        }
     
        private int getRandomNumber() {
            int randomInt = 0;
            randomInt = random.nextInt(_CHAR.length());
            if (randomInt - 1 == -1) {
                return randomInt;
            } else {
                return randomInt - 1;
     
            }
        }
     
     
    }
    (j'ai passé le protected void en public void, pour le findViewById, j'ai rajouté rootview. et j'ai désactivé setContentView(R.layout.activity_main)

    Cela me retourne le log suivant :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    05-05 15:23:57.817  32311-32311/app.z0nen.menu D/ActivityThread﹕ handleBindApplication:app.z0nen.menu
    05-05 15:23:57.817  32311-32311/app.z0nen.menu D/ActivityThread﹕ setTargetHeapUtilization:0.75
    05-05 15:23:57.817  32311-32311/app.z0nen.menu D/ActivityThread﹕ setTargetHeapMinFree:2097152
    05-05 15:23:58.027  32311-32311/app.z0nen.menu D/AndroidRuntime﹕ Shutting down VM
    05-05 15:23:58.027  32311-32311/app.z0nen.menu W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x418a5c80)
    05-05 15:23:58.027  32311-32311/app.z0nen.menu E/AndroidRuntime﹕ FATAL EXCEPTION: main
        Process: app.z0nen.menu, PID: 32311
        java.lang.RuntimeException: Unable to start activity ComponentInfo{app.z0nen.menu/app.z0nen.DNA.MyActivity}: java.lang.NullPointerException
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2215)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2264)
                at android.app.ActivityThread.access$800(ActivityThread.java:144)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
                at android.os.Handler.dispatchMessage(Handler.java:102)
                at android.os.Looper.loop(Looper.java:136)
                at android.app.ActivityThread.main(ActivityThread.java:5172)
                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:798)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:614)
                at dalvik.system.NativeStart.main(Native Method)
         Caused by: java.lang.NullPointerException
                at app.z0nen.DNA.menu1_Fragment.onCreate(menu1_Fragment.java:33)
                at android.app.Fragment.performCreate(Fragment.java:1678)
                at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:859)
                at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
                at android.app.BackStackRecord.run(BackStackRecord.java:684)
                at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
                at android.app.Activity.performStart(Activity.java:5255)
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2178)
    ************at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2264)
    ************at android.app.ActivityThread.access$800(ActivityThread.java:144)
    ************at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
    ************at android.os.Handler.dispatchMessage(Handler.java:102)
    ************at android.os.Looper.loop(Looper.java:136)
    ************at android.app.ActivityThread.main(ActivityThread.java:5172)
    ************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:798)
    ************at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:614)
    ************at dalvik.system.NativeStart.main(Native Method)
    La ligne : DNA.menu1_Fragment.onCreate(menu1_Fragment.java:33) pointant vers la ligne Button btn = (Button) rootview.findViewById(R.id.Gadn);

    Merci par avance pour le coup de main.

  2. #2
    Membre confirmé
    Profil pro
    Inscrit en
    Janvier 2007
    Messages
    83
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2007
    Messages : 83
    Par défaut
    Bonjour à tous,
    J'ai réussis à résoudre le problème, a priori cela venait de ma façons de déclarer les boutons.

    Voici le code qui fonctionne :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    public class menu1_Fragment extends Fragment {
        View rootview;
        Random random = new Random();
        private static final String _CHAR = "ATGC ";
     
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            rootview = inflater.inflate(R.layout.menu1_layout, container, false);
            Button btn = (Button) rootview.findViewById(R.id.Gadn);
            Button btn2 = (Button) rootview.findViewById(R.id.convert);
            btn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
     
                    TextView text_Random = (TextView) rootview.findViewById(R.id.adn); 
                    text_Random.setText(getRandomString()); 
                }
            });
            btn2.setOnClickListener(new OnClickListener() {
     
                @Override
                public void onClick(View v) {
                    TextView convert = (TextView) rootview.findViewById(R.id.adn);
                    TextView arn = (TextView) rootview.findViewById(R.id.arn);
                    String source = convert.getText().toString();
                    String newValue = source.replace("A", "U");
                    newValue = newValue.replace("T", "A");
                    newValue = newValue.replace("G", "K"); 
                    newValue = newValue.replace("C", "G");
                    newValue = newValue.replace("K", "C");
                    arn.setText(String.valueOf(newValue));
                }
            });
     
            return rootview;
        }
        public String getRandomString(){
            TextView input = (TextView) rootview.findViewById(R.id.input);
            float v = Float.parseFloat(input.getText().toString());
            StringBuffer randStr = new StringBuffer();
            for (int i = 0; i < v; i++) {
                int number = getRandomNumber();
                char ch = _CHAR.charAt(number);
                randStr.append(ch);
            }
            return randStr.toString();
        }
     
        private int getRandomNumber() {
            int randomInt = 0;
            randomInt = random.nextInt(_CHAR.length());
            if (randomInt - 1 == -1) {
                return randomInt;
            } else {
                return randomInt - 1;
     
            }
        }
    }

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 0
    Dernier message: 04/05/2015, 10h42
  2. Passage d'objet entre fragment
    Par alex8276 dans le forum Android
    Réponses: 2
    Dernier message: 18/11/2014, 14h41
  3. Passage entre deux activity
    Par aladin2110 dans le forum Android
    Réponses: 6
    Dernier message: 11/07/2012, 04h39
  4. Passage de données entre activities d'un Workflow
    Par Bluedeep dans le forum Windows Workflow Foundation
    Réponses: 5
    Dernier message: 12/11/2008, 22h37
  5. fragment shader: passage en n&b
    Par nikhun dans le forum OpenGL
    Réponses: 15
    Dernier message: 25/09/2006, 08h40

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo