AndroidAnnotations @EBean类实例化问题

时间:2015-09-02 23:06:54

标签: android android-annotations

我实现了自己的使用AA扩展BaseAdapter的适配器类,但是我需要在构造函数中使用多个参数来正确实例化它。我想知道是否有任何方法可以做到这一点?

@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

在setUp()方法被调用之后出现了,这对我来说真的很困惑。所以我想也许对象没有正确实例化。

1 个答案:

答案 0 :(得分:2)

mGridView为空,因为在@AfterInject之前调用了@AfterViews。 Android Annotations尚未限制您的视图。所以请改用@AfterViews