android:没有这样的实例字段

时间:2017-03-31 14:08:33

标签: java android arraylist android-asynctask

我试图通过Web服务通过AsyncTask获取数据。 我不知道为什么我宣布全局变量' arrayList1'它已被初始化,但我收到错误的数据列表'它跳过了新的AsyncTask。

private Context mContext;
private ArrayList arrayList1;
private ArrayList<List> mData;
private ListView mListView;
private ListViewAdapter mAdapter;

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

mContext = getApplicationContext();
mListView = (ListView)findViewById(R.id.listView1);
mData = new ArrayList<List>();
mData = LoadListView();
}

public ArrayList<List> LoadListView(Void...params) {
    arrayList1 = new ArrayList();
    //here error occurred.
    new AsyncTask<Void, Void, ArrayList<List>>() {
        @Override
        protected ArrayList<List> doInBackground(Void...params) {
            arrayList1 = GetData(); 
            //GetData() is another function will return a arraylist from web service.
            return arrayList1;
        }

        @Override
        protected void onPostExecute(ArrayList<List> arrayList) {
            mAdapter = new ListViewAdapter(arrayList, mContext);
            mAdapter.notifyDataSetChanged();
            mListView.setAdapter(mAdapter);
        }
    }.execute((Void[]) params); //error occurred skipped directly to here.

    return arrayList1;
}

3 个答案:

答案 0 :(得分:1)

如果你正在使用proGuard,请务必检查你的gradle中的minifyEnabled和shrinkResources以找到解决方案

答案 1 :(得分:0)

无需发送void参数。

.execute();

答案 2 :(得分:0)

我猜您在一个参考中创建了两个不同的对象,请尝试以下代码:

private Context mContext;
private ArrayList<List> mData;
private ListView mListView;
private ListViewAdapter mAdapter;

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

    mContext = getApplicationContext();
    mListView = (ListView)findViewById(R.id.listView1);
    mData = new ArrayList<List>();
    LoadListView();
}

public void LoadListView(Void...params) {

    //here error occurred.
    new AsyncTask<Void, Void, ArrayList<List>>() {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected ArrayList<List> doInBackground(Void...params) {
            mData.clear();
            mData.addAll(GetData());
            //GetData() is another function will return a arraylist from web service.
            return mData;
        }

        @Override
        protected void onPostExecute(ArrayList<List> arrayList) {
            mAdapter = new ListViewAdapter(arrayList, mContext);
            mAdapter.notifyDataSetChanged();
            mListView.setAdapter(mAdapter);
        }
    }.execute((Void[]) params); //error occurred skipped directly to here.

}