【问题标题】:io.realm.CategoryRealmProxy cannot be cast to android.widget.ImageViewio.realm.CategoryRealmProxy 无法转换为 android.widget.ImageView
【发布时间】:2019-02-15 15:54:13
【问题描述】:

我想将 ImageView 保存到 RealmDatabase,所以我尝试将其转换为 byte[] 但它似乎仍将其读取为 ImageView 或其他内容。

我想从我的微调器中保存这张图片 the image from spinner

这是我关于SaveExpense的代码

public void onSaveExpense() {

    //TODO - BITMAP EXPENSE ICONS


    if (mCategoriesSpinnerAdapter.getCount() > 0 ) {
        if (!Util.isEmptyField(etTotal)) {
            Category currentCategory = (Category) spCategory.getSelectedItem();
            String total = etTotal.getText().toString();
            String description = etDescription.getText().toString();
            ImageView theicon =(ImageView) spCategory.getSelectedItem();
            Bitmap bitmap = ((BitmapDrawable)theicon.getDrawable()).getBitmap();
            ByteArrayOutputStream imgbyte = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, imgbyte);
            byte[] expenseIcons = imgbyte.toByteArray();

            if (mUserActionMode == IUserActionsMode.MODE_CREATE) {

                //TODO - CONSTRUCTOR expenseIcons
                RealmManager.getInstance().save(new Expense(description, selectedDate,expenseIcons,mExpenseType, currentCategory, Float.parseFloat(total)), Expense.class);
            } else {
                Expense editExpense = new Expense();
                editExpense.setId(mExpense.getId());
                editExpense.setTotal(parseFloat(total));
                editExpense.setDescription(description);
                editExpense.setIconViewer(expenseIcons);
                editExpense.setCategory(currentCategory);
                editExpense.setDate(selectedDate);
                RealmManager.getInstance().update(editExpense);
            }
            // update widget if the expense is created today
            if (DateUtils.isToday(selectedDate)) {
                Intent i = new Intent(getActivity(), ExpensesWidgetProvider.class);
                i.setAction(ExpensesWidgetService.UPDATE_WIDGET);
                getActivity().sendBroadcast(i);
            }
            getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, null);
            dismiss();
        } else {
            DialogManager.getInstance().showShortToast(getString(string.error_total));
        }
    } else {
        DialogManager.getInstance().showShortToast(getString(string.no_categories_error));
    }
}

这是我的微调器适配器

public class CategoriesSpinnerAdapter extends ArrayAdapter<Category> {


Category[] categoriesList = null;
LayoutInflater inflater;

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    return getCustomView(position, convertView, parent);
}

public View getCustomView(int position, View convertView, ViewGroup parent) {
    View row = inflater.inflate(R.layout.spinner_ui, parent, false);
    Category category = categoriesList[position];
    Bitmap bitmap = BitmapFactory.decodeByteArray(category.getImgico(),0,category.getImgico().length);
    ImageView imgview = (ImageView)row.findViewById(R.id.spinnerimg);
    TextView title = (TextView)row.findViewById(R.id.spinnertext);
    title.setText(category.getName());
    imgview.setImageBitmap(bitmap);
    return row;
}

这是我点击保存按钮时的错误

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.jmnapps.expensetracker, PID: 9610
java.lang.ClassCastException: io.realm.CategoryRealmProxy cannot be cast to android.widget.ImageView
    at com.jmnapps.expensetracker.ui.expenses.NewExpenseFragment.onSaveExpense(NewExpenseFragment.java:199)
    at com.jmnapps.expensetracker.ui.expenses.NewExpenseFragment.onClick(NewExpenseFragment.java:169)
    at android.view.View.performClick(View.java:6294)
    at android.view.View$PerformClick.run(View.java:24770)
    at android.os.Handler.handleCallback(Handler.java:790)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6494)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

【问题讨论】:

    标签: java android


    【解决方案1】:

    spCategory.getSelectedItem() 返回为Category 类型,因此您不能将其转换为ImageView。在此处查看您的代码:

    Category currentCategory = (Category) spCategory.getSelectedItem();
    ImageView theicon =(ImageView) spCategory.getSelectedItem();
    

    你看到问题了吗?您正在尝试将 getSelectedItem 转换为不同的类型

    编辑: 您可以在 onSaveExpense() 函数中更改此设置:

    Category currentCategory = (Category) spCategory.getSelectedItem();
    String total = etTotal.getText().toString();
    String description = etDescription.getText().toString();
    ImageView theicon =(ImageView) spCategory.getSelectedItem();
    Bitmap bitmap = BitmapFactory.decodeByteArray(currentCategory.getImgico(),0,currentCategory.getImgico().length);
    ByteArrayOutputStream imgbyte = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, imgbyte);
    byte[] expenseIcons = imgbyte.toByteArray();
    

    【讨论】:

    • 我看到了,非常感谢.. 现在如果我一次只使用一个微调器,我怎么能包括 ImageView。 List&lt;Category&gt; categoriesList = Category.getCategoriesExpense(); Category[] categoriesArray = new Category[categoriesList.size()]; categoriesArray = categoriesList.toArray(categoriesArray); mCategoriesSpinnerAdapter = new CategoriesSpinnerAdapter(getActivity(), categoriesArray); mCategoriesSpinnerAdapter.setDropDownViewResource(spinner_ui); spCategory.setAdapter(mCategoriesSpinnerAdapter); 那是我的Category。抱歉问了很多问题。
    • 为什么不直接赋值呢? byte[] expenseIcons = currentCategory.getImgico() 并删除 onSaveExpense 中的位图/字节
    • 因为它只会得到总计和描述,所以保存时图像不会出现。
    • hmm,currentCategory.getImgico() 已经是ByteArray 对了吗?
    • 相同的结果 :( 它只保存总数和描述。我只是发布一个新问题,然后将链接发送给您以获取更多详细信息。
    猜你喜欢
    • 2017-02-22
    • 1970-01-01
    • 2012-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-02
    • 2016-06-30
    • 2016-01-14
    相关资源
    最近更新 更多