简体   繁体   中英

io.realm.CategoryRealmProxy cannot be cast to android.widget.ImageView

I want to save the ImageView to RealmDatabase so i try to convert it to byte[] but it seems it still read it as an ImageView or something.

I wanted to save this image from my spinner the image from spinner

here is my code onSaveExpense

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

here is my spinner adapter

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

here is my error when clicking save button

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)

spCategory.getSelectedItem() returns as Category type so you cannot cast it to ImageView . Look at your code here:

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

Have you seen the problem? YOu are trying to cast getSelectedItem to different types

EDIT: You can change this in your onSaveExpense() function:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM