gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 02:51:41 25 4
gpt4 key购买 nike

所以,现在我有一个 Activity 用虚拟条目填充 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();
}

在这里,我正在检测当前显示条目的末尾,然后显示“底部!”但是我收到一个错误,我的应用程序崩溃了

有什么想法吗?谢谢大家

**编辑:**这是日志

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

为了安全起见,我会添加一些空指针检查。我怀疑这部分是空的:

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

关于java - 尝试在 Android 应用程序中检测 ListView 的结尾时出现空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24044847/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com