Java HashMap错误:java.lang.ClassCastException:java.lang.Integer无法强制转换为java.lang.String

时间:2016-03-13 08:51:37

标签: android json multithreading sqlite

我从JSONArray获取HashMap并将其发送到SQLite。它给了我:   java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String. HashMap conversion issue.

类型是HashMap< Integer,HashMap< String,String>>。

for(int i=0;i<adsarray.length();i++){
      JSONObject obj=adsarray.getJSONObject(i);
      HashMap mapReceivedAdDetails=new HashMap();
      HashMap mapBusinessInfo=new HashMap();    

      //mapBusinessInfo.put
      mapReceivedAdDetails.put("RecAdId", obj.getInt("received_ad_id"));
      mapReceivedAdDetails.put("RecBusinessId",obj.getInt("business_id"));
      mapReceivedAdDetails.put("RecBusinessId",obj.getInt("user_id"));
      mapReceivedAdDetails.put("RecAdtitle",obj.getString("received_ad_title"));
      mapReceivedAdDetails.put("RecAdDetail",obj.getString("received_ad_detail"));
      mapReceivedAdDetails.put("RecBusinessZip",obj.getString("business_zip"));
      mapReceivedAdDetails.put("RecBusinessCountry", obj.getString("business_country"));
      mapReceivedAdDetails.put("AdCreated", obj.getString("adreceived_created"));
      mapReceivedAd.put(i, mapReceivedAdDetails);

     //Log.i("ReceivedId",mapReceivedAd.toString());

      mapBusinessInfo.put("BusinessId",obj.getInt("business_id"));
      mapBusinessInfo.put("BusUserId",obj.getInt("user_id"));
      mapBusinessInfo.put("BusName",obj.getString("business_name"));
      mapBusinessInfo.put("BusSt1",obj.getString("business_street1"));
      mapBusinessInfo.put("BusSt2",obj.getString("business_street2"));
      mapBusinessInfo.put("BusCity",obj.getString("business_city"));
      mapBusinessInfo.put("BusState",obj.getString("business_state"));
      mapBusinessInfo.put("BusCountry",obj.getString("business_country"));
      mapBusinessInfo.put("BusZip",obj.getString("business_zip"));
      mapBusinessInfo.put("BusContactNo",obj.getString("business_contactno"));
      mapBusinessInfo.put("BusWebsite",obj.getString("business_website"));
      mapBusinessInfo.put("BusCreated",obj.getString("business_created"));
      mapBusinessDetail.put(i,mapBusinessInfo);
    // Log.i("BusinessDetail",mapBusinessDetail.toString());



     /* ad_business_id=obj.getInt("business_id");
      ad_user_id=obj.getInt("user_id");
      receive_ad_title=obj.getString("received_ad_title");
      receive_ad_detail=obj.getString("received_ad_detail");
      ad_business_zip=obj.getString("business_zip");
      ad_business_country=obj.getString("business_country");*/
      //ad_date_created=obj.getString("date_created");
      //Log.i("+ad_business_id+"+"+ad_user_id+" "+receive_ad_title+" "+receive_ad_detail+" "+ad_business_zip+" "+ad_business_country+" ");

 }

SQLIte代码

while(i<mapBusinessInfo.size())
    {

      insert.bindLong(1, Integer.parseInt(mapBusinessInfo.get(i).get("BusUserId")));
      insert.bindLong(2, Integer.parseInt(mapBusinessInfo.get(i).get("BusinessId")));
      insert.bindString(3, mapBusinessInfo.get(i).get("BusName"));
      insert.bindString(4, mapBusinessInfo.get(i).get("BusSt1"));
      insert.bindString(5, mapBusinessInfo.get(i).get("BusSt2"));
      insert.bindString(6, mapBusinessInfo.get(i).get("BusCity"));
      insert.bindString(7, mapBusinessInfo.get(i).get("BusState"));
      insert.bindString(8, mapBusinessInfo.get(i).get("BusCountry"));
      insert.bindString(9, mapBusinessInfo.get(i).get("BusZip"));
      insert.bindString(10, mapBusinessInfo.get(i).get("BusContactNo"));
      insert.bindString(11, mapBusinessInfo.get(i).get("BusWebsite"));
      insert.bindLong(12, Long.parseLong(mapBusinessInfo.get(i).get("BusCreated")));
      insert.execute();
      i++;

    }
    db.setTransactionSuccessful();
    Log.i("Transaction", "Successful");
}

错误:

insert.bindLong(1, Integer.parseInt(mapBusinessInfo.get(i).get("BusUserId")));

下面是Logcat:

  

03-13 09:05:23.034 18810-18866 /? W / System.err的:   java.lang.ClassCastException:java.lang.Integer无法强制转换为   java.lang.String 03-13 09:05:23.034 18810-18866 /? W / System.err:at   com.example.SQLITEDatabase.DatabaseHandle.InserBusinessInfo(DatabaseHandle.java:143)   03-13 09:05:23.042 18810-18866 /? W / System.err:at   com.example.deals.NewUserDetailPage $ fetchadvertisements.doInBackground(NewUserDetailPage.java:283)   03-13 09:05:23.042 18810-18866 /? W / System.err:at   com.example.deals.NewUserDetailPage $ fetchadvertisements.doInBackground(NewUserDetailPage.java:255)   03-13 09:05:23.046 18810-18866 /? W / System.err:at   android.os.AsyncTask $ 2.call(AsyncTask.java:287)03-13 09:05:23.062   18810-18866 /? W / System.err:at   java.util.concurrent.FutureTask中$ Sync.innerRun(FutureTask.java:305)   03-13 09:05:23.062 18810-18866 /? W / System.err:at   java.util.concurrent.FutureTask.run(FutureTask.java:137)03-13   09:05:23.066 18810-18866 /? W / System.err:at   android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:230)03-13   09:05:23.070 18810-18866 /? W / System.err:at   java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)   03-13 09:05:23.070 18810-18866 /? W / System.err:at   java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:569)   03-13 09:05:23.086 18810-18866 /? W / System.err:at   java.lang.Thread.run(Thread.java:856)03-13 09:05:23.138 18810-18866 /?   W / System.err:java.lang.RuntimeException:无法在里面创建处理程序   没有调用过的线程Looper.prepare()03-13 09:05:23.170   18810-18866 /? W / System.err:at   android.os.Handler。(Handler.java:121)03-13 09:05:23.170   18810-18866 /? W / System.err:at   android.widget.Toast $ TN。(Toast.java:322)03-13 09:05:23.174   18810-18866 /? W / System.err:at   android.widget.Toast。(Toast.java:91)03-13 09:05:23.186   18810-18866 /? W / System.err:at   android.widget.Toast.makeText(Toast.java:238)03-13 09:05:23.186   18810-18866 /? W / System.err:at   com.example.deals.NewUserDetailPage $ fetchadvertisements.doInBackground(NewUserDetailPage.java:285)   03-13 09:05:23.194 18810-18866 /? W / System.err:at   com.example.deals.NewUserDetailPage $ fetchadvertisements.doInBackground(NewUserDetailPage.java:255)   03-13 09:05:23.206 18810-18866 /? W / System.err:at   android.os.AsyncTask $ 2.call(AsyncTask.java:287)03-13 09:05:23.214   18810-18866 /? W / System.err:at   java.util.concurrent.FutureTask中$ Sync.innerRun(FutureTask.java:305)   03-13 09:05:23.218 18810-18866 /? W / System.err:at   java.util.concurrent.FutureTask.run(FutureTask.java:137)03-13   09:05:23.222 18810-18866 /? W / System.err:at   android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:230)03-13   09:05:23.254 18810-18866 /? W / System.err:at   java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)   03-13 09:05:23.274 18810-18866 /? W / System.err:at   java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:569)   03-13 09:05:23.282 18810-18866 /? W / System.err:at   java.lang.Thread.run(Thread.java:856)03-13 09:05:23.454 320-320 /?   W / InputMethodManagerService:窗口已经聚焦,忽略了焦点   收益:   com.android.internal.view.IInputMethodClient$Stub$Proxy@535c3eec   属性=空

1 个答案:

答案 0 :(得分:1)

您可以自由地将Hashmap转换为String,根本不会有任何冒犯。但你正在做的是你有一个名为mapReceivedAdDetails的Hasmap,你正在尝试转换另一个未在片段mapReceivedAd中显示的变量。

所以不要做

mapReceivedAd.toString();

<强>不要

mapReceivedAdDetails.toString();