问题
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 Warn:
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:
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:
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;
}
}
回答1:
Evaluate the filter in publishResults()
instead of performFiltering()
, because Realm queries should be executed on the thread where you want to access the results.
You'll need to override the performFiltering method of ArrayAdapter, kinda like this guy did.
(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.)
来源:https://stackoverflow.com/questions/39096308/realm-android-exception-occured-during-performfiltering