android – 在fragements中获取资产

我试图在片段内的线程中解析xml文件.

我的代码的部分片段是:

public void onCreate(Bundle savedInstanceState)
    {
  mAdapter = new ListItemNearbyStoresAdapter(getActivity().getApplicationContext(), mStoresByKey);

         setlistadapter(mAdapter);

         // Load the list of stores from hard coded xml
         loadStoresByThread(getActivity().getApplicationContext());

    }

    private void loadStoresByThread(final Context ctx)
        {
            Thread t = new Thread()
            {

                public void run()
                {

                    try
                    {


                        Log.d(TAG, "In the thread");
                        String[] files = ctx.getResources().getStringArray(R.array.storefiles);
                       // String[] files={"s1.xml"};



                        for (int i = 0; i < files.length; i++)
                        {

                            try
                            {
                                InputStream stream = getActivity().getAssets().open(files[i]);

                                 NearbyItemDomFeedParser parser = new NearbyItemDomFeedParser(stream);
                                ArrayList<Store> stores = parser.parse();
                                Log.e("no of fioles read","asd :"+stores.size());
                                mStores.addAll(stores);
                                cache.setItems(mStores);
                            }
                            catch (Exception e)
                            {
                                Log.d(TAG, "Exception caught" + e);
                            }
                        }
}
}

请注意,整个类扩展了一个列表片段.

在片段中正确访问资产?

因为我无法读取文件.

没有例外,所以我无法弄清楚确切的错误.

但最后在使用构建器解析后,我得到了0个项目.

有什么建议 ?

解决方法:

Is accessing the assets in correct within an fragment ?

使用Activity(getActivity()),而不是Application(getApplicationContext()),看看是否有帮助.除非你有一个知道他们在谈论什么的人的非常具体的指示,否则永远不要在你的应用程序中使用getApplicationContext().

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念