Android onResume崩溃

时间:2016-03-06 15:13:35

标签: android sms runnable

我制作了一个使用runnable读取特定类型短信的应用。每次都行得很好。我收到短信,将其从未读状态移到读取一个,但它崩溃onResume()并给出以下错误:

Unable to resume activity {com.android.testthread/com.android.testthread.MainActivity}: android.database.StaleDataException: Attempted to access a cursor after it has been closed.

如何解决这个问题?

public class MainActivity extends Activity {
    boolean recieved = false;
    private TextView text;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text = (TextView) findViewById(R.id.textView1);
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                while (!recieved) {
                    doFakeWork();
                    text.post(new Runnable() {
                        @Override
                        public void run() {
                            List<Sms> list = getAllSms();

                            if (list.size() > 0) {
                                if (list.get(0).getAddress().equals("Engage") && list.get(0).getFolderName().equals("inbox") && list.get(0).getReadState().equals("0")) {
                                    setReadMessage(list.get(0).getId());
                                    text.setText("Got it");
                                    Toast.makeText(MainActivity.this, list.get(0).getMsg(), Toast.LENGTH_SHORT).show();
                                    recieved = true;
                                }
                            }
                        }
                    });
                }
            }
        };
        new Thread(runnable).start();

    }

    public void setReadMessage(String SmsMessageId){
        ContentValues values = new ContentValues();
        values.put("read",true);
        getContentResolver().update(Uri.parse("content://sms/inbox"),values,
                "_id="+SmsMessageId, null);
    }

    public List<Sms> getAllSms() {
        List<Sms> lstSms = new ArrayList<Sms>();
        Sms objSms = new Sms();
        Uri message = Uri.parse("content://sms/");
        ContentResolver cr = this.getContentResolver();

        Cursor c = cr.query(message, null, null, null, null);
        this.startManagingCursor(c);
        int totalSMS = c.getCount();

        if (c.moveToFirst()) {
            for (int i = 0; i < totalSMS; i++) {

                objSms = new Sms();
                objSms.setId(c.getString(c.getColumnIndexOrThrow("_id")));
                objSms.setAddress(c.getString(c
                        .getColumnIndexOrThrow("address")));
                objSms.setMsg(c.getString(c.getColumnIndexOrThrow("body")));
                objSms.setReadState(c.getString(c.getColumnIndex("read")));
                objSms.setTime(c.getString(c.getColumnIndexOrThrow("date")));
                if (c.getString(c.getColumnIndexOrThrow("type")).contains("1")) {
                    objSms.setFolderName("inbox");
                } else {
                    objSms.setFolderName("sent");
                }

                lstSms.add(objSms);
                c.moveToNext();
            }
        }
        c.close();

        return lstSms;
    }

    private void doFakeWork() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您正在尝试访问已关闭的光标。 1)尝试在onDestroy和onPause中关闭该光标,这是正确的方法。 2)并尝试在onResume中打开它。