我正在开发一个 NavigationDrawer
与 ListView
。我有一个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/