0
我正在實現我自己的適配器類,它使用AA擴展BaseAdapter,但我需要構造函數中的多個參數才能正確實例化它。我想知道是否有辦法做到這一點?AndroidAnnotations @EBean類實例化問題
@EBean
public class MomentViewAdapter extends BaseAdapter {
protected LayoutInflater mInflater;
protected Context mContext;
protected List<FavoriteInfo> mDatas;
protected int mItemLayoutId;
public MomentViewAdapter(Context context) {
this.mContext = context;
// this.mInflater = LayoutInflater.from(mContext);
// this.mDatas = mDatas;
// this.mItemLayoutId = itemLayoutId;
}
public void setUp(List<FavoriteInfo> mDatas, int itemLayoutId) {
this.mInflater = LayoutInflater.from(mContext);
this.mDatas = mDatas;
this.mItemLayoutId = itemLayoutId;
}
@Override
public int getCount()
{
return mDatas.size();
}
@Override
public FavoriteInfo getItem(int position)
{
return mDatas.get(position);
}
@Override
public long getItemId(int position)
{
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if(convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(mItemLayoutId, parent, false);
}
ImageView img = (ImageView) convertView.findViewById(R.id.detail_moment_camera_picture);
if (mDatas.get(position).isVideoFavorite()) {
Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(mDatas.get(position).getVideoURL(), MediaStore.Video.Thumbnails.MINI_KIND);
img.setImageBitmap(bitmap);
} else {
getBitmapFromURL(mDatas.get(position).getImageURL(), img);
// img.setImageBitmap(getBitmapFromURL(mDatas.get(position).getImageURL()));
}
img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(mContext, GeneratedClassUtils.get(MomentDetailActivity.class));
Bundle mBundle = new Bundle();
mBundle.putSerializable(FavoriteInfo.KEY, mDatas.get(position));
mBundle.putBoolean(CollectionInfo.KEY_WATCH_FLAG, false);
intent.putExtras(mBundle);
mContext.startActivity(intent);
}
});
return convertView;
}
@Background
protected void getBitmapFromURL(String src, ImageView img) {
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
updateImageView(BitmapFactory.decodeStream(input), img);
return;
} catch (IOException e) {
System.out.println("************** network exception: download image failed");
return;
}
}
@UiThread
protected void updateImageView(Bitmap bitmap, ImageView img) {
img.setImageBitmap(bitmap);
return;
}
}
當前我正在使用setUp方法來啓動其他參數。但它似乎有問題。我用它在我的活動是這樣的:
@EActivity
public class DeviceDetailActivity extends BaseActivity {
@Bean
MomentViewAdapter mGridViewAdapter;
/* some other code like @ViewById */
@Afterviews
public void afterViews(){
init();
}
void init(){
afterInject();
}
@AfterInject // injected beans are only available from here
public void afterInject() {
mGridViewAdapter.setUp(mMomentDatas, R.layout.device_detail_moment_listitem_simple);
mGridView.setAdapter(mGridViewAdapter);
}
}
當我跑了我的電話,我得到空指針異常:
unable to start activity ComponentInfo{com.bloomsky.bloomsky/com.bloomsky.android.activities.common.DeviceDetailActivity_}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.GridView.setAdapter(android.widget.ListAdapter)' on a null object reference
它出現在安裝完成後()方法得到,雖然叫,這對我來說真的很混亂。所以我想也許這個對象沒有正確實例化。
請發表完整的密碼。將適配器設置爲網格視圖時發生異常,但問題不包含該代碼。 – WonderCsabo