0

我想我搞砸了我的上下文。有人可以纠正我的代码吗?我似乎无法弄清楚如何在我的lazyadapter中实现上下文。请看一下我的LOGCAT。谢谢。

AndroidFragment.java

public class AndroidFragment extends SherlockListFragment  implements ActionBar.TabListener{

    static final String URL = "https://myxml.xml";
    static final String KEY_SONG = "song";
    static final String KEY_ID = "id";
    static final String KEY_TITLE = "title";
    static final String KEY_CAT_ARTIST = "artistcat";
    static final String KEY_DURATION = "duration";
    static final String KEY_THUMB_URL = "thumb_url";
    static final String KEY_CAT_URL = "cat_url";
    ArrayList<HashMap<String, String>> menuItems;
    ListAdapter adapter;
    Context appContext;
    ListView list;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        appContext = inflater.getContext().getApplicationContext();

        new loadListView().execute();   

    return super.onCreateView(inflater, container, savedInstanceState);
    }


    public class loadListView extends AsyncTask<Integer, String, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected String doInBackground(Integer... args) {
            // updating UI from Background Thread
            menuItems = new ArrayList<HashMap<String, String>>();


            XMLParser parser = new XMLParser();
            String xml = parser.getXmlFromUrl(URL); // getting XML
            Document doc = parser.getDomElement(xml); // getting DOM element

            NodeList nl = doc.getElementsByTagName(KEY_SONG);
            // looping through all item nodes <item>
            for (int i = 0; i < nl.getLength(); i++) {
                // creating new HashMap
                HashMap<String, String> map = new HashMap<String, String>();
                Element e = (Element) nl.item(i);
                // adding each child node to HashMap key => value
                map.put(KEY_ID, parser.getValue(e, KEY_ID));
                map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
                map.put(KEY_CAT_ARTIST, parser.getValue(e, KEY_CAT_ARTIST));
                map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
                map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));
                map.put(KEY_CAT_URL, parser.getValue(e, KEY_CAT_URL));

                // adding HashList to ArrayList
                menuItems.add(map);
            }
                return null;
            }   


        @Override
        protected void onPostExecute(String args) {


            adapter=new MainPageLazyAdapter(appContext, menuItems);   
             list.setAdapter(adapter);
         }
    }


    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        ft.add(android.R.id.content, this);

        ft.attach(this);
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        ft.detach(this);
    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
    }

}

MainPageLazyAdapter.java

public class MainPageLazyAdapter extends BaseAdapter {

private Application activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public MainPageImageLoader imageLoader; 

public MainPageLazyAdapter(Context appContext, ArrayList<HashMap<String, String>> d) {
    activity = appContext;
    data=d;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader=new MainPageImageLoader(activity);
}

我收到了这个错误:

Type mismatch: cannot convert from Context to Application
4

2 回答 2

1

删除(Activity)您在构造函数中使用的演员表。您在创建实例时已经使用inflator.getContext.getApplicationContext过,并且不能Activity再次将其转换为上下文。要么你传递inflator.getContext()唯一的(如果你使用Activity上下文,你可能会出现内存泄漏),或者你使用Application上下文,所以你删除了Activity强制转换。

编辑:

您已经将 ApplicationContext 传递给构造函数,因此无需再次将其转换为 Activity:

activity = (Activity) appContext;

然后再次获取 Application 上下文:

imageLoader=new MainPageImageLoader(activity.getApplicationContext());

只需设置activity = appContext;(该变量现在应该命名为Application)并直接传递它imageLoader=new MainPageImageLoader(activity);

于 2012-10-06T08:21:44.130 回答
1

查看MainPageLazyAdapter构造函数代码:activity = (Activity) appContext;
从另一方面 - 类AndroidFragment.onCreateView:appContext = inflater.getContext().getApplicationContext();


并且在AndroidFragment.onPostExecute
adapter=new MainPageLazyAdapter(appContext, menuItems);
所以 - 您正在将 Application Context 转换为 Activity 并且它会导致转换错误。

为避免这种情况,您可以覆盖AndroidFragment.onAttach方法并将一个成员添加到AndroidFragment

protected Activity fragmentActivity;    
@Override public void onAttach (Activity activity){
    fragmentActivity = activity;
}


然后替换
adapter=new MainPageLazyAdapter(appContext, menuItems);

adapter=new MainPageLazyAdapter(fragmentActivity, menuItems);


和...考虑imageLoader=new MainPageImageLoader(activity.getApplicationContext());
搬到onAttach()

于 2012-10-06T08:33:16.963 回答