Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8642665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:50:00+00:00 2026-06-12T11:50:00+00:00

I think I messed up with my Context. Could somebody correct my codes? I

  • 0

I think I messed up with my Context. Could somebody correct my codes? I cant seems to figure out how to implement Context into my lazyadapter. please take a look at my LOGCAT. Thanks.

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);
}

I got this error :

Type mismatch: cannot convert from Context to Application
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T11:50:02+00:00Added an answer on June 12, 2026 at 11:50 am

    Remove the (Activity) cast you use in the constructor. You already used inflator.getContext.getApplicationContext when you created an instance and you cannot cast it to an Activity context again. Either you pass the inflator.getContext() only (if you use the Activity context, you may get a memory leak), or you use the Application context, so you remove the Activity cast.

    Edit:

    You already have the ApplicationContext passed to the constructor, so there is no need to cast it again to Activity:

    activity = (Activity) appContext;
    

    Then get the Application context again:

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

    Just set activity = appContext; (the variable should be named Application now) and pass it directly imageLoader=new MainPageImageLoader(activity);

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In the code below, I think I messed up this part: if ( (1
I think I've figured out the problem. I'm using a IP webcam stream, doing
I think I have run into a bug in Mojarra 2.1.0. Maybe I missed
How the I repair broken symlinks? / Delete 'dead' files? Context: Since I messed
I just stumbled on Protothreads. They seems superior to native threads since context switch
I'm trying to follow a tutorial on NodeJS. I don't think I missed anything
Think about doing this: import matplotlib.pyplot as plt plt.plot(x_A,y_A,'g--') plt.plot(x_B,y_B,'r-o') plt.show() How would you
Think I have an integer array like this: a[0]=60; a[1]=321; a[2]=5; now I want
Think my problem is I am trying to sum a count in the same
Think about the following: Your ISP offers you a dynamic ip-address (for example 123.123.123.123).

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.