Androider Androider - 1 month ago 9
Java Question

Cannot wait for changes inside a Looper thread

This is my first time I use Realm. My app stops when I launch it on the emulator. The log says:


Cannot wait for changes inside a Looper thread. Use
RealmChangeListeners instead.


Any help is welcomed.

* Log:

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();
}
}

Answer

Thanks @Budius for your answer. It was helpful for me to find the exact solution.

I solved this problem by the following code:

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();
            }
        };
Comments