我正在尝试在Postexecute类中设置一个arrayadapter,以便在用户尝试在edittext中输入一些文本时显示建议列表(数组array_topic_names的值)。
虽然我从数据库接收数据,并且已成功将其存储在数组array_topic_names中,但在我设置数组适配器后它显示为NULL。
protected void onPostExecute(JSONObject json) {
int success = 0;
String message = "";
Log.d("post exec", "starting");
Log.d("json", json.toString());
if (pDialog != null && pDialog.isShowing()) {
pDialog.dismiss();
}
if (json != null) {
{
Log.d("json", "not null");
String array_topic_names[]=new String[100];
JSONArray data = null;
ArrayAdapter<String> adapter2;
try {
data = json.getJSONArray("details");
Log.d("JSON result",json.toString());
for(int i=0;i<7;i++) {
JSONObject obj = data.getJSONObject(i);
Log.d("obj_value", obj.toString());
array_topic_names[i]=obj.toString().substring(15).replace("}","").replace("\"","");
Log.d("array_topic_names", array_topic_names[i]);
adapter2 = new ArrayAdapter<String>(context, android.R.layout.simple_dropdown_item_1line, array_topic_names);
edttxt1.setThreshold(1);
//Set adapter to AutoCompleteTextView
edttxt1.setAdapter(adapter2);
edttxt1.setOnItemSelectedListener(this);
edttxt1.setOnItemClickListener(this);
}
//adapter2.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
logcat的
2 22230-22230/com.example.somya.client_feedback_application D/post exec: starting
06-29 18:21:04.752 22230-22230/com.example.somya.client_feedback_application D/json: {"details":[{"topic_name":"abc"},{"topic_name":"xyz"},{"topic_name":"bbb"},{"topic_name":"ccc"},{"topic_name":"1a"},{"topic_name":"2b"},{"topic_name":"3c"}]}
06-29 18:21:04.752 22230-22230/com.example.somya.client_feedback_application D/json: not null
06-29 18:21:04.752 22230-22230/com.example.somya.client_feedback_application D/JSON result: {"details":[{"topic_name":"abc"},{"topic_name":"xyz"},{"topic_name":"bbb"},{"topic_name":"ccc"},{"topic_name":"1a"},{"topic_name":"2b"},{"topic_name":"3c"}]}
06-29 18:21:04.752 22230-22230/com.example.somya.client_feedback_application D/obj_value: {"topic_name":"abc"}
06-29 18:21:04.752 22230-22230/com.example.somya.client_feedback_application D/array_topic_names: abc
06-29 18:21:04.752 22230-22230/com.example.somya.client_feedback_application D/obj_value: {"topic_name":"xyz"}
06-29 18:21:04.752 22230-22230/com.example.somya.client_feedback_application D/array_topic_names: xyz
06-29 18:21:04.752 22230-22230/com.example.somya.client_feedback_application D/obj_value: {"topic_name":"bbb"}
06-29 18:21:04.752 22230-22230/com.example.somya.client_feedback_application D/array_topic_names: bbb
06-29 18:21:04.752 22230-22230/com.example.somya.client_feedback_application D/obj_value: {"topic_name":"ccc"}
06-29 18:21:04.752 22230-22230/com.example.somya.client_feedback_application D/array_topic_names: ccc
06-29 18:21:04.752 22230-22230/com.example.somya.client_feedback_application D/obj_value: {"topic_name":"1a"}
06-29 18:21:04.752 22230-22230/com.example.somya.client_feedback_application D/array_topic_names: 1a
06-29 18:21:04.752 22230-22230/com.example.somya.client_feedback_application D/obj_value: {"topic_name":"2b"}
06-29 18:21:04.752 22230-22230/com.example.somya.client_feedback_application D/array_topic_names: 2b
06-29 18:21:04.752 22230-22230/com.example.somya.client_feedback_application D/obj_value: {"topic_name":"3c"}
06-29 18:21:04.752 22230-22230/com.example.somya.client_feedback_application D/array_topic_names: 3c
06-29 18:21:04.943 720-747/system_process I/ActivityManager: Displayed com.example.somya.client_feedback_application/.create_form: +821ms
06-29 18:21:27.312 22230-22794/com.example.somya.client_feedback_application W/Filter: An exception occured during performFiltering()!
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
at android.widget.ArrayAdapter$ArrayFilter.performFiltering(ArrayAdapter.java:480)
at android.widget.Filter$RequestHandler.handleMessage(Filter.java:234)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.os.HandlerThread.run(HandlerThread.java:61)