Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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);   } 
like image 924
Shweta Avatar asked Mar 07 '13 06:03

Shweta


People also ask

How do you avoid ClassCastException in your code?

To prevent the ClassCastException exception, one should be careful when casting objects to a specific class or interface and ensure that the target type is a child of the source type, and that the actual object is an instance of that type.

What is ClassCastException in Java?

Introduction. ClassCastException is a runtime exception raised in Java when we try to improperly cast a class from one type to another. It's thrown to indicate that the code has attempted to cast an object to a related class, but of which it is not an instance.

What is ClassCastException in Java with example?

It is a runtime exception that occurs when the application code attempts to cast an object to another class of which the original object is not an instance. For example, a String object cannot be cast to an Integer object and attempting to do so will result in a ClassCastException .

Is ClassCastException checked?

ClassCastException is one of the unchecked exception in Java. It can occur in our program when we tried to convert an object of one class type into an object of another class type.


1 Answers

try

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

Note: I suggest to rename listofurls to arrayOfURLs

like image 99
Evgeniy Dorofeev Avatar answered Sep 30 '22 17:09

Evgeniy Dorofeev