ListView getChildrenCount 为 0

ListView getChildrenCount is 0

我正在用 ListView 开发 NavigationDrawer。我有一个 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 可能是提前调用,return 是 0,但这不是问题。之后,在onPrepareOptionsMenu,这个调用return 7 好的。

但是当我旋转屏幕时,会重新创建 activity,并构建新的 ArrayList 和新的 ListView,但是现在调用 getChildCount() 时,这return一直都是 0。 这是输出:

ArrayList_size: 7
ListView_child_1: 0
ListView_child_2: 0

我不明白为什么。

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

logcat:

    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.access0(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) return 无效,然后崩溃。

有什么解决办法吗?

getChildCount 不会 return 列表视图中的项目数。它 return 是 listView 中行视图的数量。它在 onCreate 中为 0,因为它还没有生成那些子项——直到它需要绘制时才会生成。

当您旋转屏幕时,除非您将其关闭,否则您会经历 Activity 娱乐。所以它将回到 0,因为 activity 将被销毁并重新创建。然后它会在重绘时返回到一个非零数字。

而不是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;
}