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.