- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
这是我第一次使用 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/
我的应用程序有问题。我需要在特定时间间隔获取位置更新,因此基本上需要能够很好地控制 GPS 模块,这对于 Android 操作系统来说并不容易。基本上我需要以 5 分钟的间隔打开 GPS 2 分钟。我
考虑以下 fragment : Looper.prepare(); handler = new Handler() { @Override public void ha
在 Android 中,如果是 UI 线程,我们只需创建 Handler,因为主 UI 线程已经有了它的 Looper。 我想知道在框架代码中为 UI 线程调用 Looper.prepare 和 Lo
有人可以简单地向我解释一下以下术语以及它们之间的关系吗? handler - 每个线程都有一个处理程序?每个 View 都有一个处理程序?执行post()。操作系统主线程有默认处理程序吗? 循环器 -
我正在创建一个线程来处理请求。在线程中,我必须调用 Looper.prepare() ,这是它正在使用的其他一些功能所需要的,然后我调用 Looper.loop() 。但是当请求的连接关闭时,我会在不
所以我有这个循环器,我用它来执行长时间运行的任务。我将 Worker 对象传递给它,它本质上是一个 Runnable 的包装器 我注意到它似乎正在泄漏大小完全相同的 Message 对象 知道为什么会
我试图理解 android 中的循环器和处理程序,但被编写的示例卡住了。我想做的是,向线程添加一个循环程序,使线程在 run() 方法中连续运行。然后将消息或 runnables 发布到 hanlde
我有一个在互联网上请求数据信息的应用程序(客户端-服务器应用程序),但是这种通信非常慢,因此我决定创建一个 AsyncTask 来管理延迟。在 doInBackground 中,我调用了 Looper
我想知道 Looper 类实际上是如何处理 Looper 附加到的 Thread 中的 Runnables(通过 Handler 类)的?如果一个 looper 在它的 messageQueue 中循
我在使用 Android looper 时遇到问题。我有一个扩展了 AsynTask 的类。在 doInBackground() 方法中,我有 Looper.prepare() 和下面的一些代码。 它
我有一个线程,用于定期更新 Activity 中的数据。我创建了线程并启动了一个循环程序,以便将处理程序与 postDelay() 一起使用。在我的 Activity 的 onDestroy() 中,
我是 Android 新手。我想知道 Looper 类的作用以及如何使用它。我已阅读 Android Looper class documentation但我无法完全理解它。我在很多地方都见过它,但无
我有一个帮助类,用于在数据库 Realm 做一些工作。如您所知,我们在使用 Realm 时有一些限制,例如: Realm 实例不会在非循环线程上自动刷新。 Realm 对象只能在创建它们的线程上访问
我有一个子线程正在运行以无限地执行任务。我想(1)不断将数据发送回UI线程,并且(2)偶尔将数据(对应于按钮)发送到子线程以暂停/继续无限任务。我的问题是子线程卡在了循环程序中,这意味着任务无法执行。
这个问题已经有答案了: Can't create handler inside thread that has not called Looper.prepare() (30 个回答) 已关闭 1 年
我在这段代码中循环遍历对象的attaylist并为每个对象分配一个回调,但是当我开始使用CountDownTimer时,它崩溃了Can't create handler inside thread t
我的 Android 应用程序允许用户从 Fragment 更新他们的 Facebook 状态。我已经实现了以下 AsyncTask,以便他们可以登录: public class update
这个问题已经有答案了: Can't create handler inside thread that has not called Looper.prepare() (30 个回答) Threadi
我正在使用附加到 Android HandlerThread 的 Looper执行长时间运行的异步任务: HandlerThread handlerThread = new HandlerThread
我已经尝试了 mContext.getMainLooper() 和 Looper.getMainLooper()。两者都返回相同的结果,但我想知道哪种方法正确? 我还从 Android 开发人员链接中
我是一名优秀的程序员,十分优秀!