listview load more item when get to end
<p>i want to implement a listView that when i reach to the end , it will load more items,</p>

<p>i implemented this class:</p>

<pre><code>public class ProfileActivity extends ListActivity  {

    protected static final int STATIC_INTEGER_VALUE = 500;

    protected static final int STATIC_INTEGER_GROUP = 501;


    private  ListView lv;
    private TextView profileNameText;
    private TextView emailText;
    private TextView phoneText;
    private TextView addressText;

    private Button saleBtn;
    private Button groupBtn;
    private Button deliverBtn;

    private ProfilePictureView profilePic;
    private MyCustomAdapter adapter;



    EditText edittext;

    private List&lt;String&gt; text = new ArrayList&lt;String&gt;();
    private List&lt;Bitmap&gt; listImages;



     int textlength = 0;

     ArrayList&lt;String&gt; text_sort = new ArrayList&lt;String&gt;();
     ArrayList&lt;Bitmap&gt; image_sort = new ArrayList&lt;Bitmap&gt;();



        public void onScroll(AbsListView view,
                int firstVisible, int visibleCount, int totalCount) {

                boolean loadMore = /* maybe add a padding */
                    firstVisible + visibleCount &gt;= totalCount;

                if(loadMore) {
                    adapter.count += visibleCount; // or any other amount
                    adapter.notifyDataSetChanged();
                }
            }


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
                  setContentView(R.layout.profile_page);


         lv = getListView();
         lv.setOnScrollListener(new OnScrollListener() {

             @Override
             public void onScrollStateChanged(AbsListView view, int scrollState) {
                 // TODO Auto-generated method stub


             }

             @Override
             public void onScroll(AbsListView view, int firstVisibleItem,
                     int visibleItemCount, int totalItemCount) {

                 boolean loadMore = /* maybe add a padding */
                         firstVisibleItem + visibleItemCount &gt;= totalItemCount;

                        if(loadMore) {
                            adapter.count += visibleItemCount; // or any other amount
                            adapter.notifyDataSetChanged();
                        }

             }
         });
         LayoutInflater inflater = getLayoutInflater();
        View header = inflater.inflate(R.layout.profile_header, (ViewGroup) findViewById(R.id.header_layout_root));
        lv.addHeaderView(header, null, false);


         adapter =new MyCustomAdapter(text,listImages);
          lv.setAdapter(adapter);
          edittext= (EditText) findViewById(R.id.EditText01);




    class MyCustomAdapter extends BaseAdapter{

        int count = 1;
          public   List&lt;String&gt; text_array = new ArrayList&lt;String&gt;();
          public   List&lt;Bitmap&gt; image_array = new ArrayList&lt;Bitmap&gt;();
          public int getCount(){
               return text_array.size();
          }

          MyCustomAdapter(List&lt;String&gt; text, List&lt;Bitmap&gt; image)
          {
           text_array = text;
           image_array = image;
          }
          public long getItemId(int position){
               return position;
          }
          public String getItem(int position){
               return null;
          }
          public View getView(final int position, View convertView, ViewGroup parent) {
            LayoutInflater inflate = getLayoutInflater();
            View v = inflate.inflate(R.layout.listview, parent, false);
            final ImageView image = (ImageView) v.findViewById(R.id.ImageView01);
            TextView txtUnderImage =(TextView) v.findViewById(R.id.TextView01);

             if(listImages.get(position) != null) {
                         Bitmap bitmap = image_array.get(position);
                        image.setImageBitmap(bitmap);
                        ByteArrayOutputStream stream = new ByteArrayOutputStream();
                        bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
                        final byte[] byteArray = stream.toByteArray();

                         image.setOnClickListener(new View.OnClickListener() {

                            public void onClick(View v) {
                                // Switching to Register screen
                             Intent i = new Intent(getApplicationContext(), itemSaleActivity.class);
                             i.putExtra("picture", byteArray);
                            startActivity(i);


                                }
                        });
                         txtUnderImage.setText(text_array.get(position));
                         image.setVisibility(View.VISIBLE);
              } else {
                         image.setVisibility(View.GONE);
                         image.setImageBitmap(null);
                          image.setVisibility(View.VISIBLE);
          }
         return v;

        }
         public void addObject(String text, Bitmap bitmap) {
                text_array.add(text);
                image_array.add(bitmap);
            notifyDataSetChanged();
         } 

         public void addFirst(String text, Bitmap bitmap) {

                image_array.add(0,bitmap);
                text_array.add(0,text);

                notifyDataSetChanged();
             } 
         public void removeObject(String text,Bitmap bitmap) {

                int indexToRemove = image_array.indexOf(bitmap);
                image_array.remove(indexToRemove);
                text_array.remove(indexToRemove);
                notifyDataSetChanged();
         }
         public void deleteAll() {
             image_array.clear();
             text_array.clear();
             notifyDataSetChanged();
         }
    }
    }
</code></pre>

<p>and here is the logcat:</p>

<pre><code>03-15 15:12:57.885: E/AndroidRuntime(2027): FATAL EXCEPTION: main
03-15 15:12:57.885: E/AndroidRuntime(2027): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sale/com.example.sale.TabMainActivity}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sale/com.example.sale.ProfileActivity}: java.lang.NullPointerException
03-15 15:12:57.885: E/AndroidRuntime(2027):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
03-15 15:12:57.885: E/AndroidRuntime(2027):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-15 15:12:57.885: E/AndroidRuntime(2027):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-15 15:12:57.885: E/AndroidRuntime(2027):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-15 15:12:57.885: E/AndroidRuntime(2027):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-15 15:12:57.885: E/AndroidRuntime(2027):     at android.os.Looper.loop(Looper.java:137)
03-15 15:12:57.885: E/AndroidRuntime(2027):     at android.app.ActivityThread.main(ActivityThread.java:5041)
03-15 15:12:57.885: E/AndroidRuntime(2027):     at java.lang.reflect.Method.invokeNative(Native Method)
03-15 15:12:57.885: E/AndroidRuntime(2027):     at java.lang.reflect.Method.invoke(Method.java:511)
03-15 15:12:57.885: E/AndroidRuntime(2027):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-15 15:12:57.885: E/AndroidRuntime(2027):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-15 15:12:57.885: E/AndroidRuntime(2027):     at dalvik.system.NativeStart.main(Native Method)
03-15 15:12:57.885: E/AndroidRuntime(2027): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sale/com.example.sale.ProfileActivity}: java.lang.NullPointerException
03-15 15:12:57.885: E/AndroidRuntime(2027):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
03-15 15:12:57.885: E/AndroidRuntime(2027):     at android.app.ActivityThread.startActivityNow(ActivityThread.java:2023)
03-15 15:12:57.885: E/AndroidRuntime(2027):     at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
03-15 15:12:57.885: E/AndroidRuntime(2027):     at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
03-15 15:12:57.885: E/AndroidRuntime(2027):     at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:749)
03-15 15:12:57.885: E/AndroidRuntime(2027):     at android.widget.TabHost.setCurrentTab(TabHost.java:413)
03-15 15:12:57.885: E/AndroidRuntime(2027):     at android.widget.TabHost.addTab(TabHost.java:240)
03-15 15:12:57.885: E/AndroidRuntime(2027):     at com.example.sale.TabMainActivity.addTab(TabMainActivity.java:82)
03-15 15:12:57.885: E/AndroidRuntime(2027):     at com.example.sale.TabMainActivity.setTabs(TabMainActivity.java:57)
03-15 15:12:57.885: E/AndroidRuntime(2027):     at com.example.sale.TabMainActivity.onCreate(TabMainActivity.java:53)
03-15 15:12:57.885: E/AndroidRuntime(2027):     at android.app.Activity.performCreate(Activity.java:5104)
03-15 15:12:57.885: E/AndroidRuntime(2027):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-15 15:12:57.885: E/AndroidRuntime(2027):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-15 15:12:57.885: E/AndroidRuntime(2027):     ... 11 more
03-15 15:12:57.885: E/AndroidRuntime(2027): Caused by: java.lang.NullPointerException
03-15 15:12:57.885: E/AndroidRuntime(2027):     at com.example.sale.ProfileActivity$3.onScroll(ProfileActivity.java:323)
03-15 15:12:57.885: E/AndroidRuntime(2027):     at android.widget.AbsListView.invokeOnItemScrollListener(AbsListView.java:1340)
03-15 15:12:57.885: E/AndroidRuntime(2027):     at android.widget.AbsListView.setOnScrollListener(AbsListView.java:1329)
03-15 15:12:57.885: E/AndroidRuntime(2027):     at com.example.sale.ProfileActivity.onCreate(ProfileActivity.java:306)
03-15 15:12:57.885: E/AndroidRuntime(2027):     at android.app.Activity.performCreate(Activity.java:5104)
03-15 15:12:57.885: E/AndroidRuntime(2027):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-15 15:12:57.885: E/AndroidRuntime(2027):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-15 15:12:57.885: E/AndroidRuntime(2027):     ... 23 more
</code></pre>

<p>as i can see from logcat i get a null pointer exception in this line:</p>

<pre><code>lv.setOnScrollListener(new OnScrollListener() {
</code></pre>

<p>and i dont find the problem...</p>

<p>here is also my xml file:</p>

<pre><code>  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/linearLayout1"
        android:orientation="vertical" &gt;

                &lt;EditText android:id="@+id/EditText01"
                                android:layout_height="wrap_content"
                                android:layout_width="fill_parent"
                                android:hint="Search"&gt;                               
                &lt;/EditText&gt;

                &lt;ListView android:id="@+id/android:list"
                                android:layout_height="wrap_content"
                                android:layout_width="fill_parent"&gt;
                &lt;/ListView&gt;

    &lt;/LinearLayout&gt;
</code></pre>

<p>thanks</p>
