title: How to update ListView from BroadcastReceiver?
body: <p>It was perfectly fine to update just a <code>TextView</code>, but when I tried to update a <code>ListView</code> from <code>onReceive</code>, I kept getting <code>NullPointerException</code> from <code>getView</code> method (<code>BaseAdapter</code>). I have tried
  both 
  <code>handler.post</code> &amp; <code>runOnUiThread</code> but the problem remains the same. Did I miss something?</p>
  
  <pre><code>public class MainActivity extends Activity {
  
      private SimpleBroadcastReceiver receiver;
      private List&lt;User&gt; users;
      private UserItemAdapter adapter;
      private ListView listview;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_test_service);
          setup();
          setupControls();
          setupViews();
      }
  
      private void setupViews() {
          users = new ArrayList&lt;User&gt;();
          User chan = new User();
          chan.setUsername("Username");
          users.add(chan);
          adapter = new UserItemAdapter(this, users);
          listview = (ListView) findViewById(R.id.listview);
          listview.setAdapter(adapter);
      }
  
      private void setupControls() {
          Button b = (Button) findViewById(R.id.button);
          b.setOnClickListener(new View.OnClickListener() {
              public void onClick(View v) {
                  Intent intent = new Intent(MainActivity.this, SimpleIntentService.class);
                  Toast.makeText(MainActivity.this, "Start Service", Toast.LENGTH_SHORT).show();
                  startService(intent);
              }
          });
      }
  
      private void setup() {
          IntentFilter filter = new IntentFilter(SimpleBroadcastReceiver.ACTION_RESPONSE);
          filter.addCategory(Intent.CATEGORY_DEFAULT);
          receiver = new SimpleBroadcastReceiver(new Handler());
          registerReceiver(receiver, filter);
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          getMenuInflater().inflate(R.menu.activity_main, menu);
          return true;
      }
  
      private void updateUi(final List&lt;User&gt; data) {
          this.runOnUiThread(new Runnable() {
              public void run() {
                  for (User u : data) {
                      users.add(u);
                  }
                  adapter.notifyDataSetChanged();
              }
          });
  
      }
  
      public class SimpleBroadcastReceiver extends BroadcastReceiver {
          public static final String ACTION_RESPONSE = "com.csun.intent.action.MESSAGE_PROCESSED";
          private Handler handler;
  
          public SimpleBroadcastReceiver(Handler handler) {
              this.handler = handler;
          }
  
          @Override
          public void onReceive(Context context, Intent intent) {
              TextView t = (TextView) findViewById(com.csun.tutorplus.R.id.textview);
              t.setText("Hello");
              final ArrayList&lt;User&gt; data = (ArrayList&lt;User&gt;) intent.getSerializableExtra("users");
              updateUi(data);
          }
      }
  
  }
  </code></pre>
  
  <p><strong>UserItemAdapter</strong>   </p>
  
  <pre><code>public class UserItemAdapter extends BaseAdapter {
      private static final String TAG = UserItemAdapter.class.getSimpleName();
      private List&lt;User&gt; users;
      private Context context;
      private LayoutInflater inflater;
  
      static class ViewHolder {
          ImageView photo;
          TextView username;
      }
  
      public UserItemAdapter(Context context, List&lt;User&gt; users) {
          this.context = context;
          this.users = users;
          this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      }
  
      public int getCount() {
          return users.size();
      }
  
      public Object getItem(int position) {
          return users.get(position);
      }
  
      public long getItemId(int position) {
          return 0;
      }
  
      public View getView(int position, View convertView, ViewGroup parent) {
          ViewHolder holder;
          if (convertView == null) {
              convertView = inflater.inflate(R.layout.item_user, null);
              holder = new ViewHolder();
              holder.photo = (ImageView) convertView.findViewById(R.id.item_user_XML_imageview_userphoto);
              holder.username = (TextView) convertView.findViewById(R.id.item_user_XML_textview_username);
          } else {
              holder = (ViewHolder) convertView.getTag();
          }
          holder.username.setText(users.get(position).getUsername());
          return convertView;
      }
  
  }
  </code></pre>
  
  <p><strong>Error Log</strong>  </p>
  
  <pre><code>02-16 02:18:27.994: E/AndroidRuntime(26343): FATAL EXCEPTION: main
  02-16 02:18:27.994: E/AndroidRuntime(26343): java.lang.NullPointerException
  02-16 02:18:27.994: E/AndroidRuntime(26343):    at com.csun.tutorplus.UserItemAdapter.getView(UserItemAdapter.java:54)
  02-16 02:18:27.994: E/AndroidRuntime(26343):    at android.widget.AbsListView.obtainView(AbsListView.java:1409)
  02-16 02:18:27.994: E/AndroidRuntime(26343):    at android.widget.ListView.makeAndAddView(ListView.java:1793)
  02-16 02:18:27.994: E/AndroidRuntime(26343):    at android.widget.ListView.fillSpecific(ListView.java:1338)
  02-16 02:18:27.994: E/AndroidRuntime(26343):    at android.widget.ListView.layoutChildren(ListView.java:1624)
  02-16 02:18:27.994: E/AndroidRuntime(26343):    at android.widget.AbsListView.onLayout(AbsListView.java:1260)
  02-16 02:18:27.994: E/AndroidRuntime(26343):    at android.view.View.layout(View.java:7277)
  02-16 02:18:27.994: E/AndroidRuntime(26343):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
  02-16 02:18:27.994: E/AndroidRuntime(26343):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
  02-16 02:18:27.994: E/AndroidRuntime(26343):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
  02-16 02:18:27.994: E/AndroidRuntime(26343):    at android.view.View.layout(View.java:7277)
  02-16 02:18:27.994: E/AndroidRuntime(26343):    at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
  02-16 02:18:27.994: E/AndroidRuntime(26343):    at android.view.View.layout(View.java:7277)
  02-16 02:18:27.994: E/AndroidRuntime(26343):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
  02-16 02:18:27.994: E/AndroidRuntime(26343):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
  02-16 02:18:27.994: E/AndroidRuntime(26343):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
  02-16 02:18:27.994: E/AndroidRuntime(26343):    at android.view.View.layout(View.java:7277)
  02-16 02:18:27.994: E/AndroidRuntime(26343):    at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
  02-16 02:18:27.994: E/AndroidRuntime(26343):    at android.view.View.layout(View.java:7277)
  02-16 02:18:27.994: E/AndroidRuntime(26343):    at android.view.ViewRoot.performTraversals(ViewRoot.java:1203)
  02-16 02:18:27.994: E/AndroidRuntime(26343):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1957)
  02-16 02:18:27.994: E/AndroidRuntime(26343):    at android.os.Handler.dispatchMessage(Handler.java:99)
  02-16 02:18:27.994: E/AndroidRuntime(26343):    at android.os.Looper.loop(Looper.java:150)
  02-16 02:18:27.994: E/AndroidRuntime(26343):    at android.app.ActivityThread.main(ActivityThread.java:4369)
  02-16 02:18:27.994: E/AndroidRuntime(26343):    at java.lang.reflect.Method.invokeNative(Native Method)
  02-16 02:18:27.994: E/AndroidRuntime(26343):    at java.lang.reflect.Method.invoke(Method.java:507)
  02-16 02:18:27.994: E/AndroidRuntime(26343):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:846)
  02-16 02:18:27.994: E/AndroidRuntime(26343):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:604)
  02-16 02:18:27.994: E/AndroidRuntime(26343):    at dalvik.system.NativeStart.main(Native Method)
  </code></pre>