Bonjour !

Je ne suis pas une bête en java mais j'aspire à faire mieux voici mon soucis :

J'ai créé un projet vierge tout simple dans Android Studio que j'ai rempli de mon code (créant un imagebutton partager),

Je le lance sur l'émulateur = tout fonctionne niquel

Je souhaites l'intégrer à mon code de l'app sur laquelle je travaille. Je la lance sur l'émulateur, mais quand je clique sur le bouton share : badaboum ça crash

Voici le code de l'app sur laquelle je bosse :

XML : (main)

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
<?xml version="1.0" encoding="utf-8"?>
 
<RelativeLayout
    android:id="@+id/activity_main_layout"
    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="com.akelato.myapplication.Controller.MainActivity">
 
    <com.akelato.myapplication.View.VerticalViewPager
        android:id="@+id/view_pager"
        android:layout_height="match_parent"
        android:layout_width="match_parent">
    </com.akelato.myapplication.View.VerticalViewPager>
 
    <ImageButton
        android:id="@+id/add_note"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/history_display"
        android:layout_alignParentEnd="true"
        android:layout_marginEnd="8dp"
        android:src="@drawable/note"
        android:background="@null"/>
 
    <ImageButton
        android:id="@+id/share_ic"
        android:layout_width="45dp"
        android:layout_height="45dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentTop="true"
        android:background="@null"
        android:padding="5dp"
        android:src="@drawable/icon_share_128" />
 
    <ImageButton
        android:id="@+id/history_display"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_marginBottom="10dp"
        android:src="@drawable/history"
        android:background="@null"/>
</RelativeLayout>

Java : (main)

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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package com.akelato.myapplication.Controller;
 
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.ImageButton;
import android.widget.EditText;
import android.widget.Toast;
import android.os.Bundle;
import android.content.DialogInterface;
import android.content.Intent;
 
import com.akelato.myapplication.Model.Preference;
import com.akelato.myapplication.View.VerticalViewPager;
import com.akelato.myapplication.R;
import com.akelato.myapplication.View.CustomSwipeAdapter;
import com.akelato.myapplication.Model.Serialize;
import com.akelato.myapplication.Model.Mood;
import org.joda.time.LocalDate;
import org.joda.time.Period;
import java.io.File;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
 
public class MainActivity extends AppCompatActivity {
    private ImageButton mAddNote, mHistory;
    private CustomSwipeAdapter mAdapter;
    private EditText mEditText;
    private File mFolder, moodFile;
    private int mCurrentMood;
    private List<Mood> moodLog = new ArrayList<>();
    private Mood newMood;
    private ObjectInputStream mInputStream;
    private ObjectOutputStream mOutputStream;
    private String mCurrentMoodNote, mNote, mCurrentDay, mCurrentMoodDay;
    private VerticalViewPager mViewPager;
    private Serialize serial;
    private Preference userPref;
    private ImageButton mShare;
 
    public static final String BUNDLE_STATE_MOOD = "usersMood";
    public static final String BUNDLE_STATE_NOTE = "usersNote";
    public static final String BUNDLE_STATE_DAY = "MoodsDay";
 
 
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                mViewPager = (VerticalViewPager) findViewById(R.id.view_pager);
                mAdapter = new CustomSwipeAdapter(this);
                mViewPager.setAdapter(mAdapter);
 
                mCurrentDay = new LocalDate().toString();
 
                userPref = new Preference(this);
                if(savedInstanceState != null) {
                    mCurrentMood = savedInstanceState.getInt(BUNDLE_STATE_MOOD);
                    mCurrentMoodNote = savedInstanceState.getString(BUNDLE_STATE_NOTE);
                    mCurrentMoodDay = savedInstanceState.getString(BUNDLE_STATE_DAY);
                } else {
                    mCurrentMood = userPref.getMoodPref();
                    mCurrentMoodNote = userPref.getNotePref();
                    mCurrentMoodDay = userPref.getDayPref();
                }
 
 
        System.out.println("CurrentMoodDay: "+mCurrentMoodDay +" CurrentDay: "+mCurrentDay);
 
        LocalDate sDate = new LocalDate(LocalDate.parse(mCurrentMoodDay));
        LocalDate cDate = new LocalDate(LocalDate.parse(mCurrentDay));
        Period period = new Period(sDate, cDate);
        int deltaDays = period.toStandardDays().getDays();
        if (deltaDays != 0){
            mFolder = new File(getFilesDir() + "/mood");
            if (!mFolder.exists()){
                mFolder.mkdir();
            }
            moodFile = new File(mFolder.getAbsolutePath() + "/moodLog1.dat");
            serial = new Serialize();
            moodLog = serial.deserialize(moodFile);
 
            newMood = new Mood(mCurrentMood, deltaDays, mCurrentMoodNote);
            serial.serialize(moodLog, newMood, moodFile);
 
            mCurrentMood = 2;
            mCurrentMoodNote = null;
            mCurrentMoodDay = mCurrentDay;
 
        }
 
        mAddNote =(ImageButton)findViewById(R.id.add_note);
        mAddNote.setOnClickListener(new View.OnClickListener() {
 
            @Override public void onClick(View v) {
            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            View mView = getLayoutInflater().inflate(R.layout.note_layout, null);
            mEditText = (EditText) mView.findViewById(R.id.note_text);
            builder.setView(mView);
            builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    mNote = mEditText.getText().toString();
                    if(!mNote.isEmpty()){
                        Toast.makeText(MainActivity.this,
                                "Enregistrée",
                                Toast.LENGTH_SHORT).show();
                    }
                    dialog.cancel();
                }
            });
            builder.setNegativeButton("Annuler", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
            AlertDialog dialog = builder.create();
            dialog.show();
        }
    });
 
        mHistory = (ImageButton)findViewById(R.id.history_display);
            mHistory.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent historyActivity = new Intent(MainActivity.this, HistoryActivity.class);
                startActivity(historyActivity);
            }
        });
        mShare = (ImageButton) findViewById(R.id.share_ic);
            mShare.setOnClickListener(new ImageButton.OnClickListener() {
                @Override
                public void onClick(View v) {
                Intent shareActivity = new Intent(MainActivity.this, ShareActivity.class);
                startActivity(shareActivity);
                }
        });
    }
    @Override
    protected void onSaveInstanceState(Bundle outState) {
 
        outState.putInt(BUNDLE_STATE_MOOD, mCurrentMood);
        outState.putString(BUNDLE_STATE_NOTE, mCurrentMoodNote);
        outState.putString(BUNDLE_STATE_DAY, mCurrentMoodDay);
 
        super.onSaveInstanceState(outState);
    }
 
    @Override
    protected void onStop() {
        int moodItem = mViewPager.getCurrentItem();
        userPref.setMoodPref(moodItem);
        String date = new LocalDate().toString();
        userPref.setDayPref(date);
        userPref.setNotePref(mNote);
        super.onStop();
    }
}

Et voici le code à rajouter qui pose soucis :

XML :

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
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    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="com.akelato.share.MainActivity"
    tools:ignore="ContentDescription">
 
    <RelativeLayout
        android:layout_width="368dp"
        android:layout_height="495dp"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="8dp"
        tools:ignore="MissingConstraints">
 
        <ImageButton
            android:id="@+id/share_ic"
            android:layout_width="45dp"
            android:layout_height="45dp"
            android:layout_alignParentEnd="true"
            android:layout_alignParentTop="true"
            android:background="@null"
            android:padding="5dp"
            android:src="@drawable/icon_share_128" />
    </RelativeLayout>
</android.support.constraint.ConstraintLayout>

Java :

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
package com.akelato.share;
 
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
 
public class MainActivity extends AppCompatActivity {
 
    ImageButton bt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bt= (ImageButton) findViewById(R.id.share_ic);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent myIntent = new Intent(Intent.ACTION_SEND);
                myIntent.setType("text/plain");
                String shareBody = "Je partage blablablaaaa ";
                String shareSub = "blablablaaa";
                myIntent.putExtra(Intent.EXTRA_SUBJECT, shareSub);
                myIntent.putExtra(Intent.EXTRA_TEXT,shareBody);
                startActivity(Intent.createChooser(myIntent, "Partager"));
            }
        });
    }
}
Ou pourrais-je l'intégrer sans causer de conflits ?
Dois-je créer une nouvelle class spécialement pour le share ?
Devrais-je en plus rajouter quelques lignes dans le manifest ?

Merci d'avance de vos réponses, je suis un peu perturbé de tout essayer sans résultat.
Cordialement,