BaseAdapter在notifyDataSetChanged更新时崩溃吗?

时间:2018-11-15 20:15:11

标签: java android listview

当尝试根据网络请求更新我的阵列适配器时,该程序崩溃,并显示Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference,我同时看到了其他帖子,但是我尝试的每种方法似乎都只会导致错误,并且崩溃错误。

当然,标题说,我正在使用notifyDataSetChanged()更新列表。

这是我正在使用的代码

import android.app.DownloadManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Arrays;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MyActivity";
    public ArrayList<String> aContacts = new ArrayList<String>();

    public ListView contacts;
    public CustomAdapter customAdapter;

    // User Setup Defaults
    String server = "https://xxxxxxxx";
    String suser = "xxxxxx";
    String spass = "xxxxxx";

    String sreq;

    public void getContacts() {
        // Instantiate the RequestQueue.
        RequestQueue queue = Volley.newRequestQueue(this);
        String url = server+"/xxxxxx/?username="+suser+"&password="+spass+"&getcontacts=yes";

        StringRequest stringRequest = new StringRequest(com.android.volley.Request.Method.GET, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        // Display the first 500 characters of the response string.
                        Log.v(TAG, "I recieved: " + response);
                        try {
                            JSONArray parseContacts = new JSONArray(response);

                            for (int x = 0; x < parseContacts.length(); x++) {
                                JSONArray array = (JSONArray) parseContacts.get(x);
                                for (int j = 0; j < array.length(); j++){
                                    // print: array.get(j).toString();
                                }
                                aContacts.add(array.get(0).toString());

                                customAdapter.notifyDataSetChanged(); // ERROR IS HERE

                            }
                        }catch(JSONException e) {
                            Log.v(TAG, "Error: "+e);
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.v(TAG, "Oops, an error occurred");
            }
        });

// Add the request to the RequestQueue.
        queue.add(stringRequest);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final ListView contacts = (ListView)findViewById(R.id.contacts);

        getContacts();

        CustomAdapter customAdapter = new CustomAdapter();
        contacts.setAdapter(customAdapter);



        contacts.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Object o = contacts.getItemAtPosition(position);
                Toast.makeText(getBaseContext(),aContacts.get(position),Toast.LENGTH_SHORT).show();
            }
        });
    }

    class CustomAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return aContacts.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = getLayoutInflater().inflate(R.layout.row,null);

            TextView username = (TextView) convertView.findViewById(R.id.username);
            username.setText(aContacts.get(position));

            return convertView;
        }
    }
}

我正在尝试解析JSON数据,并且工作正常,我将结果添加到数组中,看起来像这样{"one", "two", "three"},但似乎与之无关,只是崩溃了运行时错误。我现在一直为此奋斗不已,如果有人可以给我一些可以正常工作的代码,那可能会很好,但是可能不会发生,所以请参考该问题,或者您可以做些什么来帮助,谢谢!

0 个答案:

没有答案