简体   繁体   中英

Null Pointer Exception when trying to detect end of ListView in Android app

So, right now I have an activity that populates a ListView with dummy entries. I then set an OnScrollListener to the list. This works fine.

Code:

public class MainActivity extends ActionBarActivity {
    ListView listView;
    ArrayList<Location> arrayOfLocations;
    LocationAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        listView = (ListView) findViewById(R.id.listView1);
        // progress = (ProgressBar) findViewById(R.id.progressbar_loading);
        progress = new ProgressDialog(this);
        // Construct the data source
        arrayOfLocations = new ArrayList<Location>();
        // Create the adapter to convert the array to views
        adapter = new LocationAdapter(this, arrayOfLocations);
        adapter.add(new Location(null, "Title", null, null, null));
        adapter.add(new Location(null, "Title", null, null, null));
        adapter.add(new Location(null, "Title", null, null, null));
        adapter.add(new Location(null, "Title", null, null, null));
        adapter.add(new Location(null, "Title", null, null, null));
        adapter.add(new Location(null, "Title", null, null, null));
        adapter.add(new Location(null, "Title", null, null, null));
        listView.setAdapter(adapter);

        listView.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) { 
            }
        });
    }
}

Then, inside of onScroll, I put

if (listView.getLastVisiblePosition() == listView.getAdapter().getCount() - 1
     && listView.getChildAt(listView.getChildCount() - 1).getBottom() <= listView.getHeight()) {
        Toast.makeText(getApplicationContext(), "Bottom!",
        Toast.LENGTH_LONG).show();
}

Here, I am detecting the end of the current displayed entries and then display "Bottom!" but I get an error and my app crashed

Any ideas why? Thanks guys

**EDIT: ** Here is the logcat

06-04 14:47:00.670: W/dalvikvm(12719): threadid=1: thread exiting with uncaught exception (group=0x419397c0)
06-04 14:47:00.670: E/AndroidRuntime(12719): FATAL EXCEPTION: main
06-04 14:47:00.670: E/AndroidRuntime(12719): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test6/com.example.test6.MainActivity}: java.lang.NullPointerException
06-04 14:47:00.670: E/AndroidRuntime(12719):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)
06-04 14:47:00.670: E/AndroidRuntime(12719):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389)
06-04 14:47:00.670: E/AndroidRuntime(12719):    at android.app.ActivityThread.access$600(ActivityThread.java:153)
06-04 14:47:00.670: E/AndroidRuntime(12719):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1269)
06-04 14:47:00.670: E/AndroidRuntime(12719):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-04 14:47:00.670: E/AndroidRuntime(12719):    at android.os.Looper.loop(Looper.java:137)
06-04 14:47:00.670: E/AndroidRuntime(12719):    at android.app.ActivityThread.main(ActivityThread.java:5289)
06-04 14:47:00.670: E/AndroidRuntime(12719):    at     java.lang.reflect.Method.invokeNative(Native Method)
06-04 14:47:00.670: E/AndroidRuntime(12719):    at java.lang.reflect.Method.invoke(Method.java:525)
06-04 14:47:00.670: E/AndroidRuntime(12719):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
06-04 14:47:00.670: E/AndroidRuntime(12719):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
06-04 14:47:00.670: E/AndroidRuntime(12719):    at dalvik.system.NativeStart.main(Native Method)
06-04 14:47:00.670: E/AndroidRuntime(12719): Caused by: java.lang.NullPointerException
06-04 14:47:00.670: E/AndroidRuntime(12719):    at com.example.test6.MainActivity.onCreate(MainActivity.java:62)
06-04 14:47:00.670: E/AndroidRuntime(12719):    at android.app.Activity.performCreate(Activity.java:5133)
06-04 14:47:00.670: E/AndroidRuntime(12719):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-04 14:47:00.670: E/AndroidRuntime(12719):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2293)

I'm not exactly sure where it occurs...but it only happens when I add the second piece of code I listed in the original post

I would add some null pointer checks to be safe. I suspect it's this part that is null:

listView.getChildAt(listView.getChildCount() - 1)

Try this:

if (listView.getLastVisiblePosition() == listView.getAdapter().getCount() - 1
     && listView.getChildAt(listView.getChildCount() - 1) != null
     && listView.getChildAt(listView.getChildCount() - 1).getBottom() <= listView.getHeight()) {
        Toast.makeText(getApplicationContext(), "Bottom!", Toast.LENGTH_LONG).show();
}

Just in case you want to take the extra leap of null pointer checks:

if (listView != null && listView.getAdapter() != null
     && listView.getChildAt(listView.getChildCount() - 1) != null
     && listView.getLastVisiblePosition() == listView.getAdapter().getCount() - 1
     && listView.getChildAt(listView.getChildCount() - 1).getBottom() <= listView.getHeight()) {
        Toast.makeText(getApplicationContext(), "Bottom!", Toast.LENGTH_LONG).show();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM