问题描述
当我使用 Spinner 时,该应用运行良好.但是当我尝试使用 AutoComplete Textview 而不是 Spinner 时,当我在上面输入时没有显示任何内容.任何帮助表示赞赏.
The app worked fine when I used Spinner. But when I tried AutoComplete Textview instead of Spinner, nothing is displayed when I type on it. Any help is appreciated.
Logcat 警告:
08-23 14:01:13.485 9542-9542/com.vyshnav.realmexample W/art: Failed to find OatDexFile for DexFile /data/data/com.vyshnav.realmexample/files/instant-run/dex/slice-slice_9-classes.dex ( canonical path /data/data/com.vyshnav.realmexample/files/instant-run/dex/slice-slice_9-classes.dex) with checksum 0x0486ffcc in OatFile /data/data/com.vyshnav.realmexample/cache/slice-slice_9-classes.dex
08-23 14:01:14.237 9542-9542/com.vyshnav.realmexample W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
08-23 14:01:14.769 9542-9595/com.vyshnav.realmexample E/GED: Failed to get GED Log Buf, err(0)
08-23 14:01:14.839 9542-9595/com.vyshnav.realmexample W/MALI: glDrawArrays:714: [MALI] glDrawArrays takes more than 5ms here. Total elapse time(us): 12482
08-23 14:01:20.502 9542-9668/com.vyshnav.realmexample W/Filter: An exception occured during performFiltering()!
java.lang.IllegalStateException: Realm access from incorrect thread. Realm objects can only be accessed on the thread they were created.
at io.realm.BaseRealm.checkIfValid(BaseRealm.java:449)
at io.realm.RealmResults.isLoaded(RealmResults.java:872)
at io.realm.RealmResults.size(RealmResults.java:372)
at java.util.AbstractCollection.toArrayList(AbstractCollection.java:348)
at java.util.AbstractCollection.toArray(AbstractCollection.java:339)
at java.util.ArrayList.<init>(ArrayList.java:97)
at android.widget.ArrayAdapter$ArrayFilter.performFiltering(ArrayAdapter.java:456)
at android.widget.Filter$RequestHandler.handleMessage(Filter.java:234)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.os.HandlerThread.run(HandlerThread.java:61)
08-23 14:01:35.351 9542-9811/com.vyshnav.realmexample W/Filter: An exception occured during performFiltering()!
java.lang.NullPointerException: collection == null
at java.util.ArrayList.<init>(ArrayList.java:94)
at android.widget.ArrayAdapter$ArrayFilter.performFiltering(ArrayAdapter.java:456)
at android.widget.Filter$RequestHandler.handleMessage(Filter.java:234)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.os.HandlerThread.run(HandlerThread.java:61)
08-23 14:01:44.516 9542-9899/com.vyshnav.realmexample W/Filter: An exception occured during performFiltering()!
java.lang.NullPointerException: collection == null
at java.util.ArrayList.<init>(ArrayList.java:94)
at android.widget.ArrayAdapter$ArrayFilter.performFiltering(ArrayAdapter.java:456)
at android.widget.Filter$RequestHandler.handleMessage(Filter.java:234)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.os.HandlerThread.run(HandlerThread.java:61)
MinActivity.java:
MinActivity.java:
package com.vyshnav.realmexample;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Spinner;
import io.realm.Realm;
import io.realm.RealmChangeListener;
import io.realm.RealmConfiguration;
import io.realm.RealmResults;
public class MainActivity extends AppCompatActivity {
private Realm realm;
private RealmConfiguration realmConfig;
private String[] listOfStationNames;
ArrayAdapter adapter;
AutoCompleteTextView autoCompleteTextView1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//toolbar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//fab
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
listOfStationNames = new String[] {"vvv", "rrr"};
autoCompleteTextView1= (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
// field variable
RealmResults<Station> stations;
// field variable
RealmChangeListener<RealmResults<Station>> changeListener = new RealmChangeListener<RealmResults<Station>>() {
@Override
public void onChange(RealmResults<Station> results) {
// handle onSuccess()
}
};
// Create the Realm configuration // In-Memory realm
realmConfig = new RealmConfiguration.Builder(this).name("myrealm.realm").inMemory().build();
// Open the Realm for the UI thread.
realm = Realm.getInstance(realmConfig);
// Call basicWrite
basicWrite(realm);
// Read
stations = realm.where(Station.class).findAll();
stations.addChangeListener(changeListener);
adapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,stations);
autoCompleteTextView1.setAdapter(adapter);
}
private void basicWrite(Realm realm) {
//(incase of using Callback) watch Slidenerd's approach 'AsyncTask transaction = realm.execute.....' and then closing transaction
realm.executeTransactionAsync(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
Station station = new Station();
for(String stationName : listOfStationNames) {
station.setName(stationName);
realm.insert(station);
}
}
});}
@Override
protected void onDestroy() {
super.onDestroy();
realm.close(); // Remember to close Realm when done.
}
@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);
}
}
Station.java:
Station.java:
package com.vyshnav.realmexample;
import io.realm.RealmObject;
public class Station extends RealmObject {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString(){//overriding the toString() method
return name;
}
}
推荐答案
在 publishResults()
中评估过滤器而不是 performFiltering()
,因为应该执行 Realm 查询在要访问结果的线程上.
Evaluate the filter in publishResults()
instead of performFiltering()
, because Realm queries should be executed on the thread where you want to access the results.
您需要覆盖performFiltering
ArrayAdapter
的方法,有点像这个人做的.
You'll need to override the performFiltering
method of ArrayAdapter
, kinda like this guy did.
(另外,现在我考虑了一下,将 ArrayAdapter
更改为 RealmBaseAdapter
~ source,或者至少手动使用 RealmChangeListener
处理自动更新.)
(Also, now that I think about it, it might probably be a good idea for you to change your ArrayAdapter
into a RealmBaseAdapter
~ source, or at least handle automatic updates with a RealmChangeListener
manually.)
这篇关于在 performFiltering() 期间发生 Realm Android 异常!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!