我正在尝试从 CategoriesSpinnerAdapter 获得位图值。Java,但工作室说位图为空。当我放置 android:src =“ @ drawable / defaultimg” 时,错误消失了,但是他获取的值是我的可绘制对象中的“ defaultimg”吗?如何在 CategoriesSpinnerAdapter 中获得位图值?顺便说一下,我解码来自realmdatabase的图像,因为它仅支持byte []。
NewExpenseFragment.java
public void onSaveExpense() {
ImageView imageViewer;
View viewer;
LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
viewer = inflater.inflate(R.layout.spinner_ui,null);
//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();
imageViewer = (ImageView) viewer.findViewById(R.id.spinnerimg);
Bitmap bitmap = ((BitmapDrawable)imageViewer.getDrawable()).getBitmap();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] expenseIcons = baos.toByteArray();
if (mUserActionMode == IUserActionsMode.MODE_CREATE) {
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(Float.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(R.string.error_total));
}
} else {
DialogManager.getInstance().showShortToast(getString(R.string.no_categories_error));
}
}
新更新---这是我的错误日志
Process: com.jmnapps.expensetracker, PID: 6610
java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.Bitmap android.graphics.drawable.BitmapDrawable.getBitmap()' on a null object reference
at com.jmnapps.expensetracker.ui.expenses.NewExpenseFragment.onSaveExpense(NewExpenseFragment.java:195)
at com.jmnapps.expensetracker.ui.expenses.NewExpenseFragment.onClick(NewExpenseFragment.java:163)
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)
spinner_ui
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<ImageView
android:layout_marginLeft="10dp"
android:id="@+id/spinnerimg"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center_vertical"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/dimen_20dp"
android:text="text"
android:id="@+id/spinnertext"
android:padding="5dip"
android:fontFamily="sans-serif-light"/>
</LinearLayout>
CategoriesSpinnerAdapter
public class CategoriesSpinnerAdapter extends ArrayAdapter<Category> {
Category[] categoriesList = null;
LayoutInflater inflater;
public CategoriesSpinnerAdapter(Activity context, Category[] categoriesList) {
super(context, R.layout.spinner_ui, categoriesList);
this.categoriesList = categoriesList;
this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
@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;
}
}