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

  • Home
  • SEARCH
  • 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 6093613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:34:50+00:00 2026-05-23T12:34:50+00:00

I am trying to get my AsyncTask that parses xml and creates and arraylist

  • 0

I am trying to get my AsyncTask that parses xml and creates and arraylist of objects to pass the arraylist of objects to a setAdapter so it displays a gallery to the user.

However, I am getting this:

06-24 15:24:07.972: ERROR/AndroidRuntime(4402): FATAL EXCEPTION: main
06-24 15:24:07.972: ERROR/AndroidRuntime(4402): java.lang.IllegalStateException: System services not available to Activities before onCreate()
06-24 15:24:07.972: ERROR/AndroidRuntime(4402):     at android.app.Activity.getSystemService(Activity.java:3886)
06-24 15:24:07.972: ERROR/AndroidRuntime(4402):     at android.gallery.ImageAdapter.<init>(ImageAdapter.java:41)
06-24 15:24:07.972: ERROR/AndroidRuntime(4402):     at android.gallery.GalleryActivity$ParseXML.onPostExecute(GalleryActivity.java:150)
06-24 15:24:07.972: ERROR/AndroidRuntime(4402):     at android.gallery.GalleryActivity$ParseXML.onPostExecute(GalleryActivity.java:1)
06-24 15:24:07.972: ERROR/AndroidRuntime(4402):     at android.os.AsyncTask.finish(AsyncTask.java:590)
06-24 15:24:07.972: ERROR/AndroidRuntime(4402):     at android.os.AsyncTask.access$600(AsyncTask.java:149)
06-24 15:24:07.972: ERROR/AndroidRuntime(4402):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:603)
06-24 15:24:07.972: ERROR/AndroidRuntime(4402):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-24 15:24:07.972: ERROR/AndroidRuntime(4402):     at android.os.Looper.loop(Looper.java:132)
06-24 15:24:07.972: ERROR/AndroidRuntime(4402):     at android.app.ActivityThread.main(ActivityThread.java:4025)
06-24 15:24:07.972: ERROR/AndroidRuntime(4402):     at java.lang.reflect.Method.invokeNative(Native Method)
06-24 15:24:07.972: ERROR/AndroidRuntime(4402):     at java.lang.reflect.Method.invoke(Method.java:491)
06-24 15:24:07.972: ERROR/AndroidRuntime(4402):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
06-24 15:24:07.972: ERROR/AndroidRuntime(4402):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
06-24 15:24:07.972: ERROR/AndroidRuntime(4402):     at dalvik.system.NativeStart.main(Native Method)

I left out the parsing part in my GalleryActivity.
Line 41 in ImageAdapter : inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

Line 150 in GalleryActivity: galleryView.setAdapter(new ImageAdapter(new GalleryActivity(), listOfDish));

GalleryActivity.java

public class GalleryActivity extends Activity 
{
    private  Gallery galleryView;
    private ArrayList<Dish >listOfDish;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ParseXML p = new ParseXML();
        p.execute();

        galleryView = (Gallery)findViewById(R.id.galleryid);

        galleryView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView parent, View v, int position, long id) {
                Toast.makeText(GalleryActivity.this, "" + position, Toast.LENGTH_SHORT).show();
            }
        });   
    }

    private class ParseXML extends AsyncTask<String, Void, ArrayList<Dish>>
    {
        protected ArrayList<Dish> doInBackground(String... arg0) 
        {
                listOfDish = new ArrayList<Dish>();

                    ...parse xml (this works)

                listOfDish.add(dish);
            }

            return listOfDish;  
        }

        @Override
        protected void onPostExecute(ArrayList<Dish> result)
        {
            galleryView.setAdapter(new ImageAdapter(new GalleryActivity(), listOfDish));
        }
    }

    public static String getElement(Element a, String name)
    {
        NodeList elementList = a.getElementsByTagName(name);
        Element b = (Element)elementList.item(0);

        if (b != null)
        {
            NodeList list = b.getChildNodes();
            System.out.println(name + ": " + ((Node)list.item(0)).getNodeValue().trim() );
            return ((Node)list.item(0)).getNodeValue().trim();
        }

        return null;
    }

    public static String getXML(int num){    
        String line = null;

        try 
        {   
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet("http://reddit.com/.xml");

            HttpResponse httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            line = EntityUtils.toString(httpEntity);

        } catch (UnsupportedEncodingException e) {
            line = "<results status=\"error\"><msg>Can't connect to server1</msg></results>";
        } catch (MalformedURLException e) {
            line = "<results status=\"error\"><msg>Can't connect to server2</msg></results>";
        } catch (IOException e) {
            line = "<results status=\"error\"><msg>Can't connect to server3</msg></results>";
        }

        return line;

    }

    public final static Document XMLfromString(String xml){

        Document doc = null;

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        try {

            DocumentBuilder db = dbf.newDocumentBuilder();

            InputSource is = new InputSource();
            is.setCharacterStream(new StringReader(xml));
            doc = db.parse(is); 

        } catch (ParserConfigurationException e) {
            System.out.println("XML parse error: " + e.getMessage());
            return null;
        } catch (SAXException e) {
            System.out.println("Wrong XML file structure: " + e.getMessage());
            return null;
        } catch (IOException e) {
            System.out.println("I/O exeption: " + e.getMessage());
            return null;
        }

        return doc;

    }
}

ImageAdapter

public class ImageAdapter extends BaseAdapter {

    private Activity activity;
    private static LayoutInflater inflater = null;
    private ArrayList<Dish> listOfDish;
    private int[] data;
    private String[] name;

    public ImageAdapter(Activity a, ArrayList<Dish> listOfDish) {
        activity = a;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.listOfDish = listOfDish;
        data = new int[listOfDish.size()];
        name = new String[listOfDish.size()];

        for (int i = 0; i < data.length; i++)
        {
            data[i] = R.drawable.sample_0;
            name[i] = "test";
        }
    }

    public int getCount() {
        return data.length;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public static class ViewHolder{
        public TextView text, dssnum;
        public ImageView image;
    }

    public View getView(int position, View convertView, ViewGroup parent) 
    {
        View vi = convertView;
        ViewHolder holder;

        if (convertView == null)
        {
            vi = inflater.inflate(R.layout.caption, null);
            holder = new ViewHolder();
            holder.text = (TextView) vi.findViewById(R.id.textView1);
            holder.dssnum = (TextView) vi.findViewById(R.id.dss_num);
            holder.image = (ImageView) vi.findViewById(R.id.image);
            vi.setTag(holder);
        }
        else
            holder = (ViewHolder) vi.getTag();

        holder.text.setText(name[position]);
        holder.dssnum.setText("DSS " + listOfDish.get(position).getSubsystems().get(0).getDSSNum());

        final int stub_id = data[position];
        holder.image.setImageResource(stub_id);

        return vi;
    }
}
  • 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-05-23T12:34:50+00:00Added an answer on May 23, 2026 at 12:34 pm

    Don’t use activity.getSystemService(...) instead pass the context of the GalleryActivity…

    In onPostExecute(...) do this…

    @Override
    protected void onPostExecute(ArrayList<Dish> result)
    {
        galleryView.setAdapter(new ImageAdapter(GalleryActivity.this, listOfDish));
    }
    

    …and change the ImageAdapter to…

    public ImageAdapter(Context context, ArrayList<Dish> listOfDish) {
        inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to get comfortable with jQuery and I have encountered some sample code that
I'm trying to get html content. Everything works in AsyncTask thread. This is my
I'm trying to get xml data and parse it with an async task. Here
I am trying to design a helper class that implements methods using AsyncTask. public
I'm trying get values from a GridView using the following code: foreach (GridViewRow row
Trying to get my css / C# functions to look like this: body {
Trying to get an ASP application deployed; it worked for a while but then
Trying to get this example working from http://www.munna.shatkotha.com/blog/post/2008/10/26/Light-box-effect-with-WPF.aspx However, I can't seem to get
Trying to get parameters from a PUT request using HttpServlet#doPut: public void doPut(HttpServletRequest request,
Trying to get this to work, with no luck: [DataMember] public Type ParameterType {

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.