如何解决 ClassCastException: java.lang.String cannot be cast exception
How to resolve ClassCastException: java.lang.String cannot be cast exception
我需要帮助。我有两种共享偏好的方法。它们是 getList 和 setList。
这是 setList()
public static void setList(Application activity, List<tcmb> mValuesList) {
StringBuilder valuesBuilder = new StringBuilder();
/*Log.d(TAG, "List Size ------ \t: " + mValuesList.size());*/
for (tcmb s : mValuesList) {
valuesBuilder.append(s);
valuesBuilder.append(",");
Log.d(TAG, "setValuestcmb: " + valuesBuilder.toString());
}
SharedPreferences values = activity.getSharedPreferences("dd", MODE_PRIVATE);
SharedPreferences.Editor editor = values.edit();
editor.putString("ss", valuesBuilder.toString());
editor.apply();
}
这是 getList()
public static List<tcmb> getList(Activity a) {
SharedPreferences values = a.getSharedPreferences("dd", MODE_PRIVATE);
String wordsString = values.getString("ss", "");
/* Log.d(TAG, "wordsString \t:" + wordsString);*/
String[] itemWords = wordsString.split(",");
List<String> itemList = new ArrayList<String>();
itemList.addAll(Arrays.asList(itemWords));
dovizList = (List)itemsList;
Log.d(TAG, "getValuestcmb: " + dovizList.size());
return dovizList;
}
对于我得到的值,我 运行 Main Activity 的 asyncTask class 中的 setList 方法分配给共享首选项。
然后为了获取值,我 运行 Fragment 的 asyncTask class 中的 getList 方法。
我得到了对象 values.namely,我可以看到我从 logcat.
中得到的值
[com.example.lscodex.ddddd.Model.tcmb@4ff695, com.example.lscodex.ddddd.Model.tcmb@b6b93aa, com.example.lscodex.ddddd.Model.tcmb@347db9b, com.example.lscodex.ddddd.Model.tcmb@f63eb38, com.example.lscodex.ddddd.Model.tcmb@4866711, com.example.lscodex.ddddd.Model.tcmb@8c74076, com.example.lscodex.ddddd.Model.tcmb@3be9677, com.example.lscodex.ddddd.Model.tcmb@10882e4, com.example.lscodex.ddddd.Model.tcmb@8d9634d, com.example.lscodex.ddddd.Model.tcmb@b5eee02, com.example.lscodex.ddddd.Model.tcmb@b7c2313, com.example.lscodex.ddddd.Model.tcmb@a3ce950, com.example.lscodex.ddddd.Model.tcmb@ee5e749, com.example.lscodex.ddddd.Model.tcmb@fd1e84e, com.example.lscodex.ddddd.Model.tcmb@b7bdd6f, com.example.lscodex.ddddd.Model.tcmb@40f4a7c, com.example.lscodex.ddddd.Model.tcmb@b1caf05, com.example.lscodex.ddddd.Model.tcmb@b683b5a, com.example.lscodex.ddddd.Model.tcmb@f12e18b]
一共19个值。
但是当我在片段 class 中将值投射到 recylerView 的 bindHolder 时,我得到的错误是
java.lang.ClassCastException: java.lang.String cannot be cast to com.example.lscodex.ddddd.Model.tcmb
这是来自片段的异步任务
private class DownloadXmlTask extends AsyncTask<Void, Integer, List<tcmb>> {
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressBar.setVisibility(View.VISIBLE);
}
@Override
protected List<tcmb> doInBackground(Void... voids) {
return SharedPreferenceValues.getList(getActivity());
}
@Override
protected void onPostExecute(List<tcmb> tcmb) {
if (tcmb.size() == 0) {
mtcmbList = tcmb;
setupAdapter();
mRecyclerViewAnim.runLayoutAnimation(getContext(), mRecyclerView);
mProgressBar.setVisibility(View.INVISIBLE);
} else if (mRecyclerView != null) {
mtcmbList.addAll(tcmb);
Log.d(TAG, "onPostExecute: " + mtcmbList.size());
mRecyclerView.getAdapter().notifyDataSetChanged();
dateUpdate();
mRecyclerViewAnim.runLayoutAnimation(getContext(), mRecyclerView);
mProgressBar.setVisibility(View.INVISIBLE);
mSwipeRefreshLayout.setRefreshing(false);
}
}
}
以及我报错的地方
@Override
public void onBindViewHolder(tcmbHolder holder, int position) {
tcmb tcmb= mtcmbList.get(position); ---- the error here
holder.bindTcmb(tcmb);
不知道为什么?
编辑
我尝试使用共享首选项,我从初始屏幕获取 XML 列表数据并将其导入其他 activity。
private class splashTask extends AsyncTask<Void, Integer, List<tcmb>> {
private Activity mContext;
public splashTask(Activity c){
this.mContext = c;
}
@Override
protected void onPreExecute() {
mSplashProgressBar.setProgress(0);
}
@Override
protected List<tcmbDoviz> doInBackground(Void... voids) {
List<tcmb> tcmbList = new ConnectionXmlParser().getXmlFile(getApplicationContext());
try {
int getvalues = ConnectionXmlParser.howLong();
Log.d(TAG, "doInBackground: " +getvalues);
for (int i=0;i<getvalues;i++){
Log.d(TAG, "doInBackground: " +i);
publishProgress(((int)i*100)/getvalues);
Log.d(TAG, "Publishing " + ((int)i*100/getvalues));
}
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
return tcmbList;
}
@Override
protected void onPostExecute(List<tcmbDoviz> list) {
mtcmbList.addAll(list);
SharedPreferenceValues.setValuesList(getApplication(),mtcmbList);
splashScreen();
mSplashProgressBar.setVisibility(View.GONE);
}
@Override
protected void onProgressUpdate(Integer... values) {
mSplashProgressBar.setProgress(values[0]);
super.onProgressUpdate(values);
}
您正在尝试将 List<String>
更改为 List<tcmb>
,问题出在这里
public static List<tcmb> getList(Activity a){
// your code
//
List<String> itemList = new ArrayList<String>();
itemList.addAll(Arrays.asList(itemWords));
dovizList = (List)itemsList;
Log.d(TAG, "getValuestcmb: " + dovizList.size());
return dovizList;
另外,我不明白,你到底想在这里实现什么。
List<String> itemsList = new ArrayList<String>();
dovizList = (List)itemsList;
但是,根据共享代码,您需要更改
List<String> itemsList = new ArrayList<String>();
至
List<tcmb> itemsList = new ArrayList<tcmb>();
而且,你不需要
dovizList = (List)itemsList;
我找到了方法。
将 XML 列表 object 与 splashscreen.activity 中的共享首选项一起保存
public static void setValues(Context context, List<tcmb> curProduct){
Gson gson = new Gson();
String jsonCurProduct = gson.toJson(curProduct);
SharedPreferences sharedPref = context.getSharedPreferences("Prefs", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("dd", jsonCurProduct);
editor.apply();
Log.d(TAG, "setValues: " + jsonCurProduct);
}
然后,在片段
中获取具有共享首选项的列表object
public static List<tcmb> getValuesAltın(Context context){
Gson gson = new Gson();
List<tcmbDoviz> productFromShared = new ArrayList<>();
SharedPreferences sharedPref = context.getSharedPreferences("Prefs", Context.MODE_PRIVATE);
String jsonPreferences = sharedPref.getString("dd", "");
Type type = new TypeToken<List<tcmb>>() {}.getType();
productFromShared = gson.fromJson(jsonPreferences, type);
return productFromShared;
}
最后,谢谢拉维。
我需要帮助。我有两种共享偏好的方法。它们是 getList 和 setList。
这是 setList()
public static void setList(Application activity, List<tcmb> mValuesList) {
StringBuilder valuesBuilder = new StringBuilder();
/*Log.d(TAG, "List Size ------ \t: " + mValuesList.size());*/
for (tcmb s : mValuesList) {
valuesBuilder.append(s);
valuesBuilder.append(",");
Log.d(TAG, "setValuestcmb: " + valuesBuilder.toString());
}
SharedPreferences values = activity.getSharedPreferences("dd", MODE_PRIVATE);
SharedPreferences.Editor editor = values.edit();
editor.putString("ss", valuesBuilder.toString());
editor.apply();
}
这是 getList()
public static List<tcmb> getList(Activity a) {
SharedPreferences values = a.getSharedPreferences("dd", MODE_PRIVATE);
String wordsString = values.getString("ss", "");
/* Log.d(TAG, "wordsString \t:" + wordsString);*/
String[] itemWords = wordsString.split(",");
List<String> itemList = new ArrayList<String>();
itemList.addAll(Arrays.asList(itemWords));
dovizList = (List)itemsList;
Log.d(TAG, "getValuestcmb: " + dovizList.size());
return dovizList;
}
对于我得到的值,我 运行 Main Activity 的 asyncTask class 中的 setList 方法分配给共享首选项。 然后为了获取值,我 运行 Fragment 的 asyncTask class 中的 getList 方法。 我得到了对象 values.namely,我可以看到我从 logcat.
中得到的值[com.example.lscodex.ddddd.Model.tcmb@4ff695, com.example.lscodex.ddddd.Model.tcmb@b6b93aa, com.example.lscodex.ddddd.Model.tcmb@347db9b, com.example.lscodex.ddddd.Model.tcmb@f63eb38, com.example.lscodex.ddddd.Model.tcmb@4866711, com.example.lscodex.ddddd.Model.tcmb@8c74076, com.example.lscodex.ddddd.Model.tcmb@3be9677, com.example.lscodex.ddddd.Model.tcmb@10882e4, com.example.lscodex.ddddd.Model.tcmb@8d9634d, com.example.lscodex.ddddd.Model.tcmb@b5eee02, com.example.lscodex.ddddd.Model.tcmb@b7c2313, com.example.lscodex.ddddd.Model.tcmb@a3ce950, com.example.lscodex.ddddd.Model.tcmb@ee5e749, com.example.lscodex.ddddd.Model.tcmb@fd1e84e, com.example.lscodex.ddddd.Model.tcmb@b7bdd6f, com.example.lscodex.ddddd.Model.tcmb@40f4a7c, com.example.lscodex.ddddd.Model.tcmb@b1caf05, com.example.lscodex.ddddd.Model.tcmb@b683b5a, com.example.lscodex.ddddd.Model.tcmb@f12e18b]
一共19个值。 但是当我在片段 class 中将值投射到 recylerView 的 bindHolder 时,我得到的错误是
java.lang.ClassCastException: java.lang.String cannot be cast to com.example.lscodex.ddddd.Model.tcmb
这是来自片段的异步任务
private class DownloadXmlTask extends AsyncTask<Void, Integer, List<tcmb>> {
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressBar.setVisibility(View.VISIBLE);
}
@Override
protected List<tcmb> doInBackground(Void... voids) {
return SharedPreferenceValues.getList(getActivity());
}
@Override
protected void onPostExecute(List<tcmb> tcmb) {
if (tcmb.size() == 0) {
mtcmbList = tcmb;
setupAdapter();
mRecyclerViewAnim.runLayoutAnimation(getContext(), mRecyclerView);
mProgressBar.setVisibility(View.INVISIBLE);
} else if (mRecyclerView != null) {
mtcmbList.addAll(tcmb);
Log.d(TAG, "onPostExecute: " + mtcmbList.size());
mRecyclerView.getAdapter().notifyDataSetChanged();
dateUpdate();
mRecyclerViewAnim.runLayoutAnimation(getContext(), mRecyclerView);
mProgressBar.setVisibility(View.INVISIBLE);
mSwipeRefreshLayout.setRefreshing(false);
}
}
}
以及我报错的地方
@Override
public void onBindViewHolder(tcmbHolder holder, int position) {
tcmb tcmb= mtcmbList.get(position); ---- the error here
holder.bindTcmb(tcmb);
不知道为什么?
编辑
我尝试使用共享首选项,我从初始屏幕获取 XML 列表数据并将其导入其他 activity。
private class splashTask extends AsyncTask<Void, Integer, List<tcmb>> {
private Activity mContext;
public splashTask(Activity c){
this.mContext = c;
}
@Override
protected void onPreExecute() {
mSplashProgressBar.setProgress(0);
}
@Override
protected List<tcmbDoviz> doInBackground(Void... voids) {
List<tcmb> tcmbList = new ConnectionXmlParser().getXmlFile(getApplicationContext());
try {
int getvalues = ConnectionXmlParser.howLong();
Log.d(TAG, "doInBackground: " +getvalues);
for (int i=0;i<getvalues;i++){
Log.d(TAG, "doInBackground: " +i);
publishProgress(((int)i*100)/getvalues);
Log.d(TAG, "Publishing " + ((int)i*100/getvalues));
}
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
return tcmbList;
}
@Override
protected void onPostExecute(List<tcmbDoviz> list) {
mtcmbList.addAll(list);
SharedPreferenceValues.setValuesList(getApplication(),mtcmbList);
splashScreen();
mSplashProgressBar.setVisibility(View.GONE);
}
@Override
protected void onProgressUpdate(Integer... values) {
mSplashProgressBar.setProgress(values[0]);
super.onProgressUpdate(values);
}
您正在尝试将 List<String>
更改为 List<tcmb>
,问题出在这里
public static List<tcmb> getList(Activity a){
// your code
//
List<String> itemList = new ArrayList<String>();
itemList.addAll(Arrays.asList(itemWords));
dovizList = (List)itemsList;
Log.d(TAG, "getValuestcmb: " + dovizList.size());
return dovizList;
另外,我不明白,你到底想在这里实现什么。
List<String> itemsList = new ArrayList<String>();
dovizList = (List)itemsList;
但是,根据共享代码,您需要更改
List<String> itemsList = new ArrayList<String>();
至
List<tcmb> itemsList = new ArrayList<tcmb>();
而且,你不需要
dovizList = (List)itemsList;
我找到了方法。
将 XML 列表 object 与 splashscreen.activity 中的共享首选项一起保存
public static void setValues(Context context, List<tcmb> curProduct){
Gson gson = new Gson();
String jsonCurProduct = gson.toJson(curProduct);
SharedPreferences sharedPref = context.getSharedPreferences("Prefs", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("dd", jsonCurProduct);
editor.apply();
Log.d(TAG, "setValues: " + jsonCurProduct);
}
然后,在片段
中获取具有共享首选项的列表objectpublic static List<tcmb> getValuesAltın(Context context){
Gson gson = new Gson();
List<tcmbDoviz> productFromShared = new ArrayList<>();
SharedPreferences sharedPref = context.getSharedPreferences("Prefs", Context.MODE_PRIVATE);
String jsonPreferences = sharedPref.getString("dd", "");
Type type = new TypeToken<List<tcmb>>() {}.getType();
productFromShared = gson.fromJson(jsonPreferences, type);
return productFromShared;
}
最后,谢谢拉维。