java - ListView getChildrenCount 为 0

标签 java android listview android-listview android-adapter

我正在开发一个 NavigationDrawerListView 。我有一个ArrayList<Objetct X>ListView 的项目

部分代码:

private ListView mDrawerList;
private ArrayList<NavDrawerItem> navDrawerItems;
private NavDrawerListAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {

    //Get View of ListView
    mDrawerList = (ListView) findViewById(R.id.list_slidermenu);

    //Generate the Objects and add to ArrayList
    navDrawerItems = new ArrayList<NavDrawerItem>();
    for (int i = 0; i < navMenuTitles.length; i++) {
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[i], navMenuIcons.getResourceId(i, -1)));
    }

    ...

    adapter = new NavDrawerListAdapter(this, navDrawerItems);
    System.out.println("ArrayList_size: "+navDrawerItems.size());
    mDrawerList.setAdapter(adapter);
    System.out.println("ListView_child_1:"+mDrawerList.getChildCount());

    ...
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {

    ...

    if(centroComercial==null){
        System.out.println("ListView_child_2: "+mDrawerList.getChildCount());
        ((TextView) mDrawerList.getChildAt(0).findViewById(R.id.title)).setTextColor(this.getResources().getColor(R.color.list_item_title_unselected));
        ((ImageView) mDrawerList.getChildAt(0).findViewById(R.id.icon)).setImageResource(R.drawable.ic_home_des);
    }else{
        ((TextView) mDrawerList.getChildAt(0).findViewById(R.id.title)).setTextColor(this.getResources().getColor(R.color.list_item_title));
        ((ImageView) mDrawerList.getChildAt(0).findViewById(R.id.icon)).setImageResource(R.drawable.ic_home);
    }

}

当我第一次启动应用程序时,一切正常,这是输出:

ArrayList_size: 7
ListView_child_1: 0
ListView_child_2: 7

数组列表包含所有项目。 ListView_child_1: 0可能是早期调用,并返回 0,但这不是问题。之后,在 onPrepareOptionsMenu ,这个调用返回 7 好吧。

但是当我旋转屏幕时, Activity 将被重新创建,并构建新的 ArrayList ,以及新的ListView ,但现在当 getChildCount()被调用时,总是返回 0。 这是输出:

ArrayList_size: 7
ListView_child_1: 0
ListView_child_2: 0

我不明白为什么。

但这并不是在所有版本中都会发生。 在 4.1 和 5.0 中,当我旋转屏幕时 getChildCount() 返回 0 在 4.2 和 4.4 中工作正常。当我旋转屏幕时,getChildCount() 返回 7。

日志猫:

    01-10 04:43:34.716: E/AndroidRuntime(1962): FATAL EXCEPTION: main
01-10 04:43:34.716: E/AndroidRuntime(1962): java.lang.RuntimeException: Unable to start activity ComponentInfo{uva.asimov.findyourmall/uva.asimov.findyourmall.MainActivity}: java.lang.NullPointerException
01-10 04:43:34.716: E/AndroidRuntime(1962):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
01-10 04:43:34.716: E/AndroidRuntime(1962):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
01-10 04:43:34.716: E/AndroidRuntime(1962):     at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3512)
01-10 04:43:34.716: E/AndroidRuntime(1962):     at android.app.ActivityThread.access$700(ActivityThread.java:130)
01-10 04:43:34.716: E/AndroidRuntime(1962):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1201)
01-10 04:43:34.716: E/AndroidRuntime(1962):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-10 04:43:34.716: E/AndroidRuntime(1962):     at android.os.Looper.loop(Looper.java:137)
01-10 04:43:34.716: E/AndroidRuntime(1962):     at android.app.ActivityThread.main(ActivityThread.java:4745)
01-10 04:43:34.716: E/AndroidRuntime(1962):     at java.lang.reflect.Method.invokeNative(Native Method)
01-10 04:43:34.716: E/AndroidRuntime(1962):     at java.lang.reflect.Method.invoke(Method.java:511)
01-10 04:43:34.716: E/AndroidRuntime(1962):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-10 04:43:34.716: E/AndroidRuntime(1962):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-10 04:43:34.716: E/AndroidRuntime(1962):     at dalvik.system.NativeStart.main(Native Method)
01-10 04:43:34.716: E/AndroidRuntime(1962): Caused by: java.lang.NullPointerException
01-10 04:43:34.716: E/AndroidRuntime(1962):     at uva.asimov.findyourmall.MainActivity.onPrepareOptionsMenu(MainActivity.java:204)
01-10 04:43:34.716: E/AndroidRuntime(1962):     at android.app.Activity.onPreparePanel(Activity.java:2494)
01-10 04:43:34.716: E/AndroidRuntime(1962):     at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:421)
01-10 04:43:34.716: E/AndroidRuntime(1962):     at com.android.internal.policy.impl.PhoneWindow.invalidatePanelMenu(PhoneWindow.java:747)
01-10 04:43:34.716: E/AndroidRuntime(1962):     at com.android.internal.policy.impl.PhoneWindow.restorePanelState(PhoneWindow.java:1677)
01-10 04:43:34.716: E/AndroidRuntime(1962):     at com.android.internal.policy.impl.PhoneWindow.restoreHierarchyState(PhoneWindow.java:1627)
01-10 04:43:34.716: E/AndroidRuntime(1962):     at android.app.Activity.onRestoreInstanceState(Activity.java:928)
01-10 04:43:34.716: E/AndroidRuntime(1962):     at android.app.Activity.performRestoreInstanceState(Activity.java:900)
01-10 04:43:34.716: E/AndroidRuntime(1962):     at android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1130)
01-10 04:43:34.716: E/AndroidRuntime(1962):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2037)
01-10 04:43:34.716: E/AndroidRuntime(1962):     ... 12 more

第 204 行:

((TextView) mDrawerList.getChildAt(0).findViewById(R.id.title)).setTextColor(this.getResources().getColor(R.color.list_item_title_unselected));

getChildAt(0) 返回 null,并崩溃。

有什么解决办法吗?

最佳答案

尝试使用此代替 getChildAt()

public View getViewByPosition(int pos, ListView listView) {
    final int firstListItemPosition = listView.getFirstVisiblePosition();
    final int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1;

    if (pos < firstListItemPosition || pos > lastListItemPosition ) {
        return listView.getAdapter().getView(pos, null, listView);
    } /*else {
        final int childIndex = pos - firstListItemPosition;
        return listView.getChildAt(childIndex);
    }*/ // Try with else part un-commented too
    return null;
}

关于java - ListView getChildrenCount 为 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27872270/

相关文章:

c# - 绑定(bind)到派生集合的 ListView 未更新(XAML/Windows 8)

java - 使用 Groovy 修复在 Web 容器内运行的代码

java - 时间表符号(时间范围)

android - 使用标准 Activity 创建预填充联系人

android - AccountManager 添加帐户但未显示在“设置”中的“帐户”下

c++ - 同一系统上的两个版本的 glibc

firebase - flutter Riverpod : Problems fetching data from Firebase using Riverpod

android - 当我在自定义适配器中使用图像按钮时,setOnItemClickListener 在 ListView 中不起作用

java - 比较器不工作,我找不到错误

java - 如何在java中以多个日期作为值对 map 进行排序