这是我第一次使用Realm。当我在模拟器上启动它时,我的应用停止了。日志说:
不能等待Looper线程内的更改。使用 而不是RealmChangeListeners。
欢迎任何帮助。
*日志:
06-19 19:58:44.050 2464-2464/com.twitter.i_droidi.notah E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.twitter.i_droidi.notah, PID: 2464
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.twitter.i_droidi.notah/com.twitter.i_droidi.notah.MainActivity}: java.lang.IllegalStateException: Cannot wait for changes inside a Looper thread. Use RealmChangeListeners instead.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.IllegalStateException: Cannot wait for changes inside a Looper thread. Use RealmChangeListeners instead.
at io.realm.BaseRealm.waitForChange(BaseRealm.java:274)
at io.realm.Realm.waitForChange(Realm.java:121)
at com.twitter.i_droidi.notah.RealmController.refresh(RealmController.java:61)
at com.twitter.i_droidi.notah.MainActivity.onCreate(MainActivity.java:54)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
* MainActivity.java:
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.Toast;
import java.util.ArrayList;
import io.realm.Realm;
import io.realm.RealmResults;
public class MainActivity extends AppCompatActivity {
private NotesAdapter adapter;
private Realm realm;
private LayoutInflater inflater;
private RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
this.realm = RealmController.with(this).getRealm();
setupRecycler();
setRealmData();
RealmController.with(this).refresh();
setRealmAdapter(RealmController.with(this).getNotes());
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
inflater = MainActivity.this.getLayoutInflater();
View content = inflater.inflate(R.layout.edit_item, null);
final EditText editTitle = (EditText) content.findViewById(R.id.edit_title);
final EditText editBody = (EditText) content.findViewById(R.id.edit_body);
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setView(content)
.setTitle("Add Note")
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Notes note = new Notes();
note.setId(RealmController.getInstance().getNotes().size() + (int) System.currentTimeMillis());
note.setTitle(editTitle.getText().toString());
note.setBody(editBody.getText().toString());
if(editTitle.getText() == null || editTitle.getText().toString().equals("") || editTitle.getText().toString().equals(" "))
{
Toast warning = Toast.makeText(MainActivity.this, "Enter a valid title!", Toast.LENGTH_SHORT);
warning.show();
}
else
{
realm.beginTransaction();
realm.copyToRealm(note);
realm.commitTransaction();
adapter.notifyDataSetChanged();
recyclerView.scrollToPosition(RealmController.getInstance().getNotes().size() - 1);
}
}
})
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
});
}
public void setRealmAdapter(RealmResults<Notes> notes)
{
RealmNotesAdapter realmNotesAdapter = new RealmNotesAdapter(this.getApplicationContext(), notes);
adapter.setRealmAdapter(realmNotesAdapter);
adapter.notifyDataSetChanged();
}
private void setupRecycler()
{
recyclerView.setHasFixedSize(true);
final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);
adapter = new NotesAdapter(this);
recyclerView.setAdapter(adapter);
}
private void setRealmData()
{
ArrayList<Notes> notes = new ArrayList<>();
Notes note = new Notes();
note.setId(1);
note.setTitle("Test #1");
note.setBody("This is Test #1.");
note.setId(2);
note.setTitle("Test #2");
note.setBody("This is Test #2.");
note.setId(3);
note.setTitle("Test #3");
note.setBody("This is Test #3.");
for(Notes n : notes)
{
realm.beginTransaction();
realm.copyToRealm(n);
realm.commitTransaction();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
* RealmController.java:
import android.app.Activity;
import android.app.Application;
import android.app.Fragment;
import io.realm.Realm;
import io.realm.RealmResults;
public class RealmController {
protected static RealmController instance;
protected final Realm realm;
public RealmController(Application application)
{
realm = Realm.getDefaultInstance();
}
public static RealmController with(Fragment fragment)
{
if(instance == null)
{
instance = new RealmController(fragment.getActivity().getApplication());
}
return instance;
}
public static RealmController with(Activity activity)
{
if(instance == null)
{
instance = new RealmController(activity.getApplication());
}
return instance;
}
public static RealmController with(Application application)
{
if(instance == null)
{
instance = new RealmController(application);
}
return instance;
}
public static RealmController getInstance()
{
return instance;
}
public Realm getRealm()
{
return realm;
}
public void refresh()
{
realm.waitForChange();
}
public void clearAll()
{
realm.beginTransaction();
RealmResults<Notes> results = realm.where(Notes.class).findAll();
results.deleteAllFromRealm();
realm.commitTransaction();
}
public RealmResults<Notes> getNotes()
{
return realm.where(Notes.class).findAll();
}
public Notes getNote(int id)
{
return realm.where(Notes.class).equalTo("id", id).findFirst();
}
public boolean hasNotes()
{
return !realm.where(Notes.class).findAll().isEmpty();
}
}
答案 0 :(得分:3)
这只是一个阅读问题。错误日志和API文档。:
登录
不能等待Looper线程内的更改。改为使用RealmChangeListeners。
表示从RealmController.with(this).refresh();
删除该行onCreate
。可能的解决方法是使用onStart/onStop
添加/删除更改侦听器。
api文档:https://realm.io/docs/java/1.0.1/api/
没有Looper的线程上的Realm实例无法接收更新,除非手动调用waitForChange() (......) 阻止当前线程,直到Realm的新更改可用 (......) 抛出: java.lang.IllegalStateException - 如果从Looper线程调用它
这意味着你不能从任何Looper线程(包括UI线程)中调用它
答案 1 :(得分:0)
感谢 @Budius 的回答。找到确切的解决方案对我很有帮助。
我通过以下代码解决了这个问题:
private RealmChangeListener realmChangeListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.realm = RealmController.with(this).getRealm();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
setupRecycler();
realmChangeListener = new RealmChangeListener() {
@Override
public void onChange(Object element) {
RealmController.with(MainActivity.this).refresh();
}
};
答案 2 :(得分:0)
这是因为您不必在UI线程上刷新,这由Realm自动处理。
但如果您使用0.89.0+并在UI线程上进行同步提交后立即执行adapter.notifyDataSetChanged()
,那么您可能会遇到一些崩溃问题。您不必这样做,使用RealmChangeListener或只使用Realm的android-adapters