ClassCastException:无法将java.lang.Object []强制转换为java.lang.String [] android

在我的应用程序中,我需要将arraylist转换为数组的字符串。 但是,我得到一个错误:

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

在与listofurls的线上出现错误:listofurls = (String[])image_urls.toArray();

这是完整的代码:

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);
  }
Shweta asked 2020-01-24T10:11:48Z
5个解决方案
108 votes

尝试

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

注意:我建议将listofurls重命名为arrayOfURLs

Evgeniy Dorofeev answered 2020-01-24T10:12:20Z
28 votes

您应该如上所述使用toArray(),但不能那样使用。

首先初始化数组并填充它:

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

之后,toArray()将包含image_urls中的所有字符串,或者传入长度为零的字符串数组:

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

请参阅toArray()的文档。

kcoppock answered 2020-01-24T10:12:54Z
3 votes

您只需要获取数组中arraylist的内容,对吗?

你不能这样吗?

       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 answered 2020-01-24T10:13:18Z
2 votes

尝试这个:

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

直接从这里获取:链接

crazylpfan answered 2020-01-24T10:13:42Z
1 votes
listofurls = image_urls.toArray(new String[0]);

即使您不知道结果数组的大小,也应该可以解决所有情况。

Samarth S answered 2020-01-24T10:14:02Z
translate from https://stackoverflow.com:/questions/15264182/classcastexception-java-lang-object-cannot-be-cast-to-java-lang-string-andr