Note that there are some explanatory texts on larger screens.

plurals
  1. POAsyncTask updating UI
    primarykey
    data
    text
    <p>So many hours trying to figure this out, I have read so many questions, forums, answers.. but it still will not update the UI.</p> <p>My end goal is to take a search term from user, and send a httprequest to PHP scrip that replies with JSON. This works fine so I will not delve into it.</p> <p>My problem is then updating the UI after this is done. I can get the HTTP results, but for now, I am just trying to update the UI with a basic string before implementing the big changes such as buttons with listeners etc with all the results. For now, just simple String addition to the UI.</p> <p>The onPreExecute() adds a string to the UI and this works fine somehow, as implemented below.</p> <p>The problem is in onPostExecute.. like I said, for now, just a String, to test (it will be a list or something eventually). It doesn't work at all, always throwing exception.</p> <pre><code>public class DisplaySearchActivity extends ListActivity { ArrayList&lt;String&gt; listItems=new ArrayList&lt;String&gt;(); ArrayAdapter&lt;String&gt; adapter = null; private class updateUI extends AsyncTask&lt;String, Void, String&gt;{ @Override protected void onPreExecute() { listItems.add("Retrieving Results..."); // This actually works just like that } @Override protected String doInBackground(String... message) { final List&lt;HashMap&lt;String,String&gt;&gt; blist = httpSearch.search(message[0]); return "It has done"; } @Override protected void onPostExecute(String b){ Log.v("ok", "shane - "+ b); listItems.add("Business: "+ b); adapter.notifyDataSetChanged(); } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_display_search); final ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;(this,android.R.layout.simple_list_item_1,listItems); setListAdapter(adapter); // final String message = getIntent().getStringExtra("input"); final String message = "third+level"; new updateUI().execute(message); } // end onCreate } </code></pre> <p>Here is the exception</p> <pre><code>03-15 13:38:41.190: V/ok(15732): shane - It has done 03-15 13:38:41.190: D/AndroidRuntime(15732): Shutting down VM 03-15 13:38:41.190: W/dalvikvm(15732): threadid=1: thread exiting with uncaught exception (group=0x4142a2a0) 03-15 13:38:41.200: E/AndroidRuntime(15732): FATAL EXCEPTION: main 03-15 13:38:41.200: E/AndroidRuntime(15732): java.lang.NullPointerException 03-15 13:38:41.200: E/AndroidRuntime(15732): at ie.whereis.DisplaySearchActivity$updateUI.onPostExecute(DisplaySearchActivity.java:34) 03-15 13:38:41.200: E/AndroidRuntime(15732): at ie.whereis.DisplaySearchActivity$updateUI.onPostExecute(DisplaySearchActivity.java:1) 03-15 13:38:41.200: E/AndroidRuntime(15732): at android.os.AsyncTask.finish(AsyncTask.java:631) 03-15 13:38:41.200: E/AndroidRuntime(15732): at android.os.AsyncTask.access$600(AsyncTask.java:177) 03-15 13:38:41.200: E/AndroidRuntime(15732): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644) 03-15 13:38:41.200: E/AndroidRuntime(15732): at android.os.Handler.dispatchMessage(Handler.java:99) 03-15 13:38:41.200: E/AndroidRuntime(15732): at android.os.Looper.loop(Looper.java:137) 03-15 13:38:41.200: E/AndroidRuntime(15732): at android.app.ActivityThread.main(ActivityThread.java:4898) 03-15 13:38:41.200: E/AndroidRuntime(15732): at java.lang.reflect.Method.invokeNative(Native Method) 03-15 13:38:41.200: E/AndroidRuntime(15732): at java.lang.reflect.Method.invoke(Method.java:511) 03-15 13:38:41.200: E/AndroidRuntime(15732): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006) 03-15 13:38:41.200: E/AndroidRuntime(15732): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773) 03-15 13:38:41.200: E/AndroidRuntime(15732): at dalvik.system.NativeStart.main(Native Method) </code></pre> <p>I think it is being thrown by the line:</p> <pre><code>adapter.notifyDataSetChanged(); </code></pre> <p>But I just don't really understand what the adapter does, even after much reading. I don't know why the </p> <pre><code>listItems.add("Retrieving Results..."); </code></pre> <p>in pre Execute works :S but the one in post does not.</p> <p>Any help is so greatly appreciated.</p> <hr> <p>Thank you, I have commented the solution. Apologies for not being able to deal out up votes. &lt;15 karma</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload