尝试在Android应用中检测ListView的结尾时出现空指针异常

时间:2014-06-04 18:25:04

标签: java android listview

所以,现在我有一个用虚拟条目填充ListView的活动。然后我将OnScrollListener设置为列表。这很好。

代码:

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

然后,在onScroll里面,我把

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

在这里,我正在检测当前显示条目的结束,然后显示“Bottom!”但我收到错误,我的应用程序崩溃了

任何想法为什么?谢谢你们

**编辑:** 这是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)

1 个答案:

答案 0 :(得分:1)

  

我不确定它发生的位置......但只有在我添加时才会发生   我在原帖中列出的第二段代码

我会添加一些空指针检查以确保安全。我怀疑这部分是空的:

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

试试这个:

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();
}

以防你想要进行空指针检查的额外跳跃:

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();
}