61

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);
  }
Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
Shweta
  • 807
  • 3
  • 11
  • 24

5 Answers5

126

try

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

Note: I suggest to rename listofurls to arrayOfURLs

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
  • 1
    still getting the error.it says, Caused by: java.lang.ClassCastException: java.lang.Object[] cannot be cast to java.lang.String[] – Shweta Mar 07 '13 at 06: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 07 '13 at 06:06
  • i suggest to try and localize the problem, try this: List image_urls = new ArrayList(); String[] listofurls = image_urls.toArray(new String[image_urls.size()]); – Evgeniy Dorofeev Mar 07 '13 at 06:09
  • 1
    @Shweta Naming conventions. If you're calling your array a list, it's confusing. – Kevin Coppock Mar 07 '13 at 06:09
30

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

Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274
3

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 ;
        }
Deepzz
  • 4,573
  • 1
  • 28
  • 52
3
listofurls = image_urls.toArray(new String[0]);

that should do the trick for all cases, even if you don't know the size of the resulting array.

Samarth S
  • 313
  • 4
  • 5
2

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

Community
  • 1
  • 1
crazylpfan
  • 1,038
  • 7
  • 9