0

I'm trying to get data thru AsyncTask with web service. I have no idea why as I declared global variable 'arrayList1' and it has been initialized, but I'm getting error for the 'arrayList1' and it skipped doin the new 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;
}
2
  • You declared an ArrayList<List> , is that right? a list of lists? Commented Mar 31, 2017 at 14:13
  • yes, is ArrayList<List>, that was a mistake, but i had correct it, but the error still occurred. Commented Mar 31, 2017 at 14:18

3 Answers 3

2

If you're using proGuard make sure to check minifyEnabled and shrinkResources in your gradle to find a solution

Sign up to request clarification or add additional context in comments.

Comments

0

No need to send void parameter.

.execute();

Comments

0

I guess you are creating two different object's in one reference, try this code:

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.

}

3 Comments

Where you are getting this Error "No such instance field" ?
i saw the error at bottom panel when i debug. i cant embed image unless i earned 10 reputation, but here is the link for the error image. i.sstatic.net/CGyls.png
Edited my answer, plz check.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.