我正在尝试使用algolia API访问firestore表,但是我遇到了一个错误
public void iniciarMetodos() {
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference productsRef = rootRef.collection("products");
Client client = new Client(YourApplicationID, YourAPIKey);
final Index index = client.getIndex(your_index_name);
productsRef.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
List<String> list = new ArrayList<>();
for (DocumentSnapshot document : task.getResult()) {
list.add(document.getString("productName"));
}
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(ActivityTest.this, android.R.layout.simple_list_item_1, list);
listView.setAdapter(arrayAdapter);
} else {
Log.w(TAG, "Error getting documents.", task.getException());
}
}
});
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
Query query = new Query(s.toString()).setAttributesToRetrieve("productName")
.setHitsPerPage(50);
index.searchAsync(query, new CompletionHandler() {
@Override
public void requestCompleted(JSONObject jsonObject, AlgoliaException e) {
try {
JSONArray hits = jsonObject.getJSONArray("hits");
List<String> list = new ArrayList<>();
for (int i = 0; i < hits.length(); i++){
JSONObject jsonObject1 = hits.getJSONObject(i);
String productName = jsonObject1.getString("productName");
}
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(ActivityTest.this, android.R.layout.simple_list_item_1, list);
listView.setAdapter(arrayAdapter);
} catch (JSONException e1) {
e1.printStackTrace();
}
}
});
}
});
}
错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: homologa.cappta.com.br.capptahomologa, PID: 16490
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:445)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:407)
at android.widget.AbsListView.obtainView(AbsListView.java:2379)
at android.widget.ListView.makeAndAddView(ListView.java:2052)
at android.widget.ListView.fillDown(ListView.java:786)
at android.widget.ListView.fillFromTop(ListView.java:847)
at android.widget.ListView.layoutChildren(ListView.java:1826)
at android.widget.AbsListView.onLayout(AbsListView.java:2171)
at android.view.View.layout(View.java:19602)
at android.view.ViewGroup.layout(ViewGroup.java:6057)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
at android.view.View.layout(View.java:19602)
at android.view.ViewGroup.layout(ViewGroup.java:6057)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:19602)
at android.view.ViewGroup.layout(ViewGroup.java:6057)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
at android.view.View.layout(View.java:19602)
at android.view.ViewGroup.layout(ViewGroup.java:6057)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:19602)
at android.view.ViewGroup.layout(ViewGroup.java:6057)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
at android.view.View.layout(View.java:19602)
at android.view.ViewGroup.layout(ViewGroup.java:6057)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:758)
at android.view.View.layout(View.java:19602)
at android.view.ViewGroup.layout(ViewGroup.java:6057)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2512)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2228)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1414)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6827)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
at android.view.Choreographer.doCallbacks(Choreographer.java:723)
at android.view.Choreographer.doFrame(Choreographer.java:658)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6592)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:769)
E/UncaughtException: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:445)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:407)
at android.widget.AbsListView.obtainView(AbsListView.java:2379)
at android.widget.ListView.makeAndAddView(ListView.java:2052)
at android.widget.ListView.fillDown(ListView.java:786)
at android.widget.ListView.fillFromTop(ListView.java:847)
at android.widget.ListView.layoutChildren(ListView.java:1826)
at android.widget.AbsListView.onLayout(AbsListView.java:2171)
at android.view.View.layout(View.java:19602)
at android.view.ViewGroup.layout(ViewGroup.java:6057)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
at android.view.View.layout(View.java:19602)
at android.view.ViewGroup.layout(ViewGroup.java:6057)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:19602)
at android.view.ViewGroup.layout(ViewGroup.java:6057)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
at android.view.View.layout(View.java:19602)
at android.view.ViewGroup.layout(ViewGroup.java:6057)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:19602)
at android.view.ViewGroup.layout(ViewGroup.java:6057)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
at android.view.View.layout(View.java:19602)
at android.view.ViewGroup.layout(ViewGroup.java:6057)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:758)
at android.view.View.layout(View.java:19602)
at android.view.ViewGroup.layout(ViewGroup.java:6057)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2512)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2228)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1414)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6827)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
at android.view.Choreographer.doCallbacks(Choreographer.java:723)
at android.view.Choreographer.doFrame(Choreographer.java:658)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6592)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:769)
Application terminated.