2017-04-06 42 views
0

我不斷收到:域訪問時,它是在同一個線程

java.lang.IllegalStateException: Realm access from incorrect thread. Realm objects can only be accessed on the thread they were created. 

但是線程已經被它甚至倒是一種境界對象,我解決這個任何時候中斷?

RealmConfiguration config = new RealmConfiguration.Builder() 
      .name("CustomSchedule.realm") 
      .schemaVersion(42) 
      .build(); 
    mRealm = Realm.getInstance(config); 

    mDays = app.getmDays(); 

    final Handler handler = new Handler(); 

    t = new Thread(new Runnable() { 
     public void run() { 
      while(!Thread.currentThread().isInterrupted()) { 
       while (mDays.isEmpty()) { 
       } 
       Thread.currentThread().interrupt(); 
       startActivity(); 
       handler.post(this); 
      } 
     } 
    }); 
    t.start(); 

    return rootView; 
} 

在startActivity部分中,我像往常一樣引用領域對象。

回答

0

我改變了主題到:

final Handler handler = new Handler(); 

    final Thread t = new Thread(new Runnable() { 
     public void run() { 
      while(!Thread.currentThread().isInterrupted()) { 
       while (mDays.isEmpty()) { 
       } 
       handler.post(new Runnable() { 
        @Override 
        public void run() { 
         startActivity(); 
        } 
       }); 
       Thread.currentThread().interrupt(); 
      } 
     } 
    }); 
    t.start(); 

而現在它的工作原理。

相關問題