From ca428d6a6395e753b103edb3c03d0d24ec2930e1 Mon Sep 17 00:00:00 2001 From: Eric House Date: Tue, 3 Dec 2013 08:30:47 -0800 Subject: [PATCH] fix NPEs occurring when loading in a games DB (debug feature) whose groups were different: recreate GameListAdapter and make it correctly deal with having no group selection data (as that's stored in prefs) --- .../eehouse/android/xw4/GameListAdapter.java | 15 ++++++++---- .../org/eehouse/android/xw4/GamesList.java | 23 +++++++++++++------ 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/GameListAdapter.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/GameListAdapter.java index dde66fcd6..2116f1599 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/GameListAdapter.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/GameListAdapter.java @@ -329,7 +329,8 @@ public class GameListAdapter implements ExpandableListAdapter { long[] positions = getGroupPositions(); String[] names = new String[ positions.length ]; for ( int ii = 0; ii < names.length; ++ii ) { - names[ii] = info.get(positions[ii]).m_name; + GameGroupInfo ggi = info.get( positions[ii] ); + names[ii] = ggi.m_name; } return names; } @@ -486,10 +487,14 @@ public class GameListAdapter implements ExpandableListAdapter { long[] result = positions; if ( null != positions ) { Set posns = gameInfo().keySet(); - for ( long id : positions ) { - if ( ! posns.contains( id ) ) { - result = null; - break; + if ( positions.length != posns.size() ) { + result = null; + } else { + for ( long id : positions ) { + if ( ! posns.contains( id ) ) { + result = null; + break; + } } } } diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/GamesList.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/GamesList.java index ca264b87a..7f52fc85c 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/GamesList.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/GamesList.java @@ -385,15 +385,9 @@ public class GamesList extends XWExpandableListActivity } PreferenceManager.setDefaultValues( this, R.xml.xwprefs, isUpgrade ); - String field = CommonPrefs.getSummaryField( this ); - long[] positions = XWPrefs.getGroupPositions( this ); - m_adapter = new GameListAdapter( this, listview, new Handler(), - this, positions, field ); - setListAdapter( m_adapter ); + m_adapter = makeNewAdapter(); listview.setOnItemLongClickListener( this ); - m_adapter.expandGroups( listview ); - NetUtils.informOfDeaths( this ); tryStartsFromIntent( getIntent() ); @@ -786,6 +780,8 @@ public class GamesList extends XWExpandableListActivity case R.id.games_menu_loaddb: DBUtils.loadDB( this ); + XWPrefs.clearGroupPositions( this ); + m_adapter = makeNewAdapter(); changeContent = true; break; case R.id.games_menu_storedb: @@ -1349,6 +1345,19 @@ public class GamesList extends XWExpandableListActivity } } + private GameListAdapter makeNewAdapter() + { + ExpandableListView listview = getExpandableListView(); + String field = CommonPrefs.getSummaryField( this ); + long[] positions = XWPrefs.getGroupPositions( this ); + GameListAdapter adapter = + new GameListAdapter( this, listview, new Handler(), + this, positions, field ); + setListAdapter( adapter ); + adapter.expandGroups( listview ); + return adapter; + } + public static void onGameDictDownload( Context context, Intent intent ) { intent.setClass( context, GamesList.class );