微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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().

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。