gpt4 book ai didi

java - 无法等待 Looper 线程内的更改

转载 作者:搜寻专家 更新时间:2023-11-01 08:34:26 25 4
gpt4 key购买 nike

这是我第一次使用 Realm。当我在模拟器上启动它时,我的应用程序停止了。日志说:

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

欢迎任何帮助。

* 日志:

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

最佳答案

这只是阅读的问题。错误日志和 API 文档。:

日志

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

这意味着从 onCreate 中删除 RealmController.with(this).refresh(); 行。可能的解决方法是使用 onStart/onStop 添加/删除更改监听器。

api 文档:https://realm.io/docs/java/1.0.1/api/

Realm instances on threads without a Looper cannot receive updates unless waitForChange() is manually called (...) Blocks the current thread until new changes to the Realm are available (...) Throws: java.lang.IllegalStateException - if calling this from a Looper thread

这意味着你不能从任何 Looper 线程(包括 UI 线程)调用它

关于java - 无法等待 Looper 线程内的更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37909851/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com