Bonjour,


Je suis en train de mettre en place une application capable de lire des sms.

Pour lire les Sms je fais le code 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
        final ListView listView = (ListView)findViewById(R.id.receiveSMS);
 
        Uri uri = Uri.parse("content://sms/inbox");
         c= getContentResolver().query(uri, null, null ,null,null);
        startManagingCursor(c);
    List<SmsData> listInput = new ArrayList<SmsData>();
        // Read the sms data and store it in the list
        if(c.moveToFirst()) {
            for(int i=0; i < c.getCount(); i++) {
 
                String phone = c.getString(c.getColumnIndexOrThrow("address")).toString();
                String body = c.getString(c.getColumnIndexOrThrow("body")).toString();
                SmsData smsdata = new SmsData();
                smsdata.setNumber(phone);
                smsdata.setBody(body);
 
                listInput.add(smsdata);
 
                c.moveToNext();
            }
        }
 
c.close();

Apres avoir sélectionner un message de la liste, une nouvelle page (Activity) est ouverte affichant le message.
Mais lorsque je reviens sur la page précédente j'ai le bug 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
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: rexad.com.smsdatacollect, PID: 4555
                  java.lang.RuntimeException: Unable to resume activity {rexad.com.smsdatacollect/rexad.com.smsdatacollect.ReceiveDataActivity}: android.database.StaleDataException: Attempted to access a cursor after it has been closed.
                      at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2986)
                      at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3017)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1347)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:135)
                      at android.app.ActivityThread.main(ActivityThread.java:5254)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:372)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
                   Caused by: android.database.StaleDataException: Attempted to access a cursor after it has been closed.
                      at android.database.BulkCursorToCursorAdaptor.throwIfCursorIsClosed(BulkCursorToCursorAdaptor.java:64)
                      at android.database.BulkCursorToCursorAdaptor.requery(BulkCursorToCursorAdaptor.java:133)
                      at android.database.CursorWrapper.requery(CursorWrapper.java:186)
                      at android.app.Activity.performRestart(Activity.java:6042)
                      at android.app.Activity.performResume(Activity.java:6068)
                      at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2975)
                      at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3017)*
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1347)*
                      at android.os.Handler.dispatchMessage(Handler.java:102)*
                      at android.os.Looper.loop(Looper.java:135)*
                      at android.app.ActivityThread.main(ActivityThread.java:5254)*
                      at java.lang.reflect.Method.invoke(Native Method)*
                      at java.lang.reflect.Method.invoke(Method.java:372)*
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)*
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)*
Je vois bien que c'est la gestion du Cursor qui pose problème.

Comment gérer correctement le Cursor pour pouvoir revenir sur la page apres avois fermé le Cursos.


Merci d'avance.