Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

In my application I need to convert my arraylist to a string of an array. However, I am getting an error:

ClassCastException: java.lang.Object[] cannot be cast to java.lang.String[] android

At the line with listofurls I am getting the error: listofurls = (String[])image_urls.toArray();

This is the full code:

public class Test2 extends AsyncTask<Void, Void, Void> 
{
  String[] listofurls ;
  private static final String url = "http://www.tts.com/album_pro/array_to_encode";
  JSONParser jParser = new JSONParser();
  ArrayList<String> image_urls = new ArrayList<String>();

  protected void onPreExecute() {
    //Log.e(LOG_CLASS, "in side assyntask");
  }

  protected Void doInBackground(Void... voids) {
    Log.v("Async","Async");
    JSONObject json = jParser.getJSONFromUrl(url);

    try {
      JSONObject seo = json.getJSONObject("SEO");
      JSONArray folio = seo.getJSONArray("Folio");

      // JSONArray image_urls1 = new JSONArray();
      //String s1=seo.getString("Folio");

      for(int i=0;i<folio.length();++i) {
        String m = folio.getString(i);
        Log.v("M"+i,m);
        image_urls.add(m);
        Log("test-url"+image_urls);
      }
    } catch(Exception e) {
      e.printStackTrace();
    }

    listofurls = (String[])image_urls.toArray();  //ERROR OCCURS HERE

    return null;
  }

  private void Log(String string) {
    Log.v("Test",string);
  }

  protected void onProgressUpdate(Integer... progress) { }

  protected void onPostExecute(Void result) {
    mAdapter = new ImagePagerAdapter(getSupportFragmentManager(),listofurls.length );
    mAdapter.setImageurls(listofurls);
    mPager.setAdapter(mAdapter);
  }
share|improve this question
    
@MiserableVariable Better wording, yes .. –  user166390 Mar 7 '13 at 6:09
    
actually opposite meaning, but you get the point :) –  Miserable Variable Mar 7 '13 at 6:21

4 Answers 4

up vote 17 down vote accepted

try

listofurls = image_urls.toArray(new String[image_urls.size()]);

Note: I suggest to rename listofurls to arrayOfURLs

share|improve this answer
    
still getting the error.it says, Caused by: java.lang.ClassCastException: java.lang.Object[] cannot be cast to java.lang.String[] –  Shweta Mar 7 '13 at 6:05
    
you mean to say : String[] listofurls ; listofurls = image_urls.toArray(new String[image_urls.size()]); , instead of "listofurls" i should use "arrayOFURLs"?? but how does it make a difference –  Shweta Mar 7 '13 at 6:06
    
i suggest to try and localize the problem, try this: List<String> image_urls = new ArrayList<String>(); String[] listofurls = image_urls.toArray(new String[image_urls.size()]); –  Evgeniy Dorofeev Mar 7 '13 at 6:09
1  
@Shweta Naming conventions. If you're calling your array a list, it's confusing. –  kcoppock Mar 7 '13 at 6:09

You should use toArray as mentioned above, but not in that way.

Either initialize the array first and fill it:

String[] urlArray = new String[image_urls.size()];
image_urls.toArray(urlArray);

After which, urlArray will contain all the Strings from image_urls, or pass in a zero-length String array:

listofurls = (String[]) image_urls.toArray(new String[0]);

See the documentation for toArray().

share|improve this answer

You just need to get the contents of arraylist in an array, right??

Can't u do like this?

       for(int i=0;i<folio.length();++i)
        {
            String m = folio.getString(i);
            Log.v("M"+i,m);
            image_urls.add(m);
            Log("test-url"+image_urls);


            listofurls[i] = m ;
        }
share|improve this answer

Try this:

ArrayList<String> stock_list = new ArrayList<String>();
stock_list.add("stock1");
stock_list.add("stock2");
String[] stockArr = new String[stock_list.size()];
stockArr = stock_list.toArray(stockArr);
for(String s : stockArr)
    System.out.println(s);

Taken directly from here: link

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.