Not to beat a dead horse but lv
is null
since you changed the layout
with setContentView()
. Maybe I can explain a little better why this is because I'm not sure you quite understand how Views
work in the Activity
.
When you call setContentView()
it inflates the xml layout
file that you set in this function. Initializing any View
that is not in that layout
file will return null
which will give a NPE
when you try to set a method on it such as setAdapter()
.
It appears that you are under the assumption that you can still initialize the ListView
which is in another layout
file...you cannot. You can only use Views
inflated with setContentView()
or by inflating the layout
file which holds that View
and adding it to the currently inflated `layout.
One way around this is to call setContentView()
again in onPostExecute()
then initialize your ListView
and set the Adapter
. I wouldn't normally recommend calling setContentView()
more than once in a single Activity
but in your case it may be the easiest for what you currently have.
So it might look like this
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
prog.dismiss();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,aa);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.listView1);
lv.setAdapter(adapter);