我有一个简单的应用程序,它可以查看漫画并允许用户将一些漫画标记为“收藏夹”,以后可以访问(它的功能不止于此,但这就是这里的全部相关内容)。当用户将漫画标记为收藏时,会将字符串放入共享偏好中,格式为 key = "# of Comic" Value = "title of Comic"。SharedPreferences 只包含与收藏漫画相关的键/值对。此功能工作正常。问题出现在我实现的一个菜单按钮上,我打算显示一个 ListView,其中包含存储在 SharedPreferences 文件中的所有收藏夹的值。这是用户单击弹出菜单中的收藏夹按钮时执行的操作的代码
case R.id.favorites:
Log.i("Step 1", "Favorites");
favVector.clear(); //Clears string Vector that I want to use to hold the titles
Map<String, ?> allprefs = xkfav.getAll(); //gets map of all Shared Preferences
for (Map.Entry<String, ?> entry : allprefs.entrySet()) {
favVector.add((String) entry.getValue());
}
Log.i("Step 2", "Favorites");
setContentView(R.layout.favlist); //loads Layout with ListView (and nothing else)
Log.i("Step 3", "Favorites");
ListView menuList;
menuList = (ListView) findViewById(R.id.FavListView);
String[] items = new String[favVector.size()]; //creates array with size of Vector
favVector.copyInto(items); //Copies Vector into array
ArrayAdapter<String> adapt = new ArrayAdapter<String>(this, R.layout.fav_item, items);
menuList.setAdapter(adapt); //Puts array contents into list
每次我运行这个,我都会得到一个强制关闭。我什至从未在日志中看到“第 2 步”。忽略这可能不是漂亮或高效的代码这一事实,为什么当用户单击此按钮时会出现强制关闭错误?