0

我有一个简单的应用程序,它可以查看漫画并允许用户将一些漫画标记为“收藏夹”,以后可以访问(它的功能不止于此,但这就是这里的全部相关内容)。当用户将漫画标记为收藏时,会将字符串放入共享偏好中,格式为 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 步”。忽略这可能不是漂亮或高效的代码这一事实,为什么当用户单击此按钮时会出现强制关闭错误?

4

1 回答 1

2

强制关闭通常是应用程序某处运行时异常的结果。

此异常应在日志中可见。如果您使用的是 Eclipse/ADT,请转到 DDMS 透视图并查看 logcat 视图。(我假设您可以轻松重现该错误)。

您应该看到一个堆栈跟踪。调查堆栈跟踪并尝试找出出错的地方。这可以是从 NullPointerException 到另一个 RuntimeException 的任何东西。

另外,尝试在“步骤 1”断点处放置一个断点。在调试模式下启动您的应用程序并逐步执行代码。在堆栈中的某个点,您将确切地看到应用程序何时以及为什么会以强制关闭方式退出。

如果您从未使用过 Eclipse 的调试功能,请参阅以下链接: http ://www.vogella.de/articles/EclipseDebugging/article.html

于 2011-01-02T19:02:03.223 回答