简体   繁体   English

io.realm.CategoryRealmProxy无法转换为android.widget.ImageView

[英]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. 我想将ImageView保存到RealmDatabase,所以我尝试将其转换为byte [],但似乎仍将其作为ImageView读取。

I wanted to save this image from my spinner the image from spinner 我想保存微调器中的该图像

here is my code onSaveExpense 这是我的代码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 . spCategory.getSelectedItem()返回为Category类型,因此无法将其spCategory.getSelectedItem()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 您正在尝试将getSelectedItem强制转换为不同的类型

EDIT: You can change this in your onSaveExpense() function: 编辑:您可以在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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 LinearLayout无法强制转换为android.widget.ImageView? - LinearLayout cannot be cast to android.widget.ImageView? 无法将ProfilePictureView强制转换为android.widget.ImageView - ProfilePictureView cannot be cast to android.widget.ImageView 无法将Android.widget.RelativeLayout强制转换为android.widget.ImageView - Android.widget.RelativeLayout cannot be cast to android.widget.ImageView android.widget.LinearLayout无法转换为android.widget.ImageView吗? - android.widget.LinearLayout cannot be cast to android.widget.ImageView? android.widget.ImageView无法转换为com.parse.ParseImageView - android.widget.ImageView cannot be cast to com.parse.ParseImageView 错误:android.widget.LinearLayout无法转换为android.widget.ImageView - Error: android.widget.LinearLayout cannot be cast to android.widget.ImageView Android应用程式会抛出已停止运作的错误,无法将android.widget.ImageView转换为android.view.ViewGroup - Android application throws the error that it has stopped working, android.widget.ImageView cannot be cast to android.view.ViewGroup 如何解决错误 androidx.appcompat.widget.AppCompatButton cannot be throw to android.widget.ImageView on the AlertDialog - how to resolve mistakes androidx.appcompat.widget.AppCompatButton cannot be thrown to android.widget.ImageView on the AlertDialog that I made 为什么无法在android中的Widget按钮中投射imageview - why imageview cannot be cast in android widget button in android 原因:android.view.InflateException:二进制XML文件第9行:错误膨胀了类android.widget.ImageView - Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class android.widget.ImageView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM