在使用位图错误向下滚动到ListView中的第3项后,Android应用程序崩溃了

时间:2017-06-20 02:49:22

标签: java android

RecommendItem.Java

public class RecommendItem {
    private String name;
    private String type;
    private String address;
    private Bitmap avatar;

    public RecommendItem(String name, String type, String address, Bitmap avatar) {
        this.name = name;
        this.type = type;
        this.address = address;
        this.avatar = avatar;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Bitmap getAvatar() {
        return avatar;
    }

    public void setAvatar(Bitmap avatar) {
        this.avatar = avatar;
    }
}

FoodRecommendAdapter.Java     公共类FoodRecommendAdapter扩展了ArrayAdapter {

    private Context context;
    private int reID;
    private ArrayList<RecommendItem> recommendItems;

    public FoodRecommendAdapter(Context context, int resource, ArrayList<RecommendItem> data) {
        super(context, resource, data);
        this.context = context;
        this.reID = resource;
        this.recommendItems = data;
        }

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = null;
        if (convertView == null) {
        LayoutInflater layoutInflater = ((Activity) this.context).getLayoutInflater();
        convertView = layoutInflater.inflate(this.reID, parent, false);
        return convertView;
        }
        row = convertView;
        RecommendItem item = recommendItems.get(position);

        ImageView imageView = (ImageView) row.findViewById(R.id.iv_recommend);
        imageView.setImageBitmap(item.getAvatar());
        TextView textViewName =(TextView) row.findViewById(R.id.tv_name);
        textViewName.setText(item.getName());
        TextView textViewType =(TextView) row.findViewById(R.id.tv_type);
        textViewType.setText(item.getType());
        TextView textViewAddress =(TextView) row.findViewById(R.id.tv_name);
        textViewAddress.setText(item.getName());
        return row;
    }
}

FoodRecommed.Java

public class FoodRecommend extends AppCompatActivity {
    ListView listViewFood;
    ArrayList<RecommendItem> recommendItems;
    FoodRecommendAdapter foodRecommendAdapter;
    Button btnMap, btnMyList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_food_recommend);
        initComponents();
        loadData();

    }

        private void loadData() {
            recommendItems = new ArrayList<>();
            recommendItems.add(new RecommendItem("Bánh Mì","Food","26 Lê Thị Riêng, Ben Thanh, District 1",
                    BitmapFactory.decodeResource(getResources(),R.drawable.banhmi)));
            recommendItems.add(new RecommendItem("Phở","Food","19 Nguyễn Thị Minh Khai, District 1",
                    BitmapFactory.decodeResource(getResources(),R.drawable.pho)));
            recommendItems.add(new RecommendItem("Bún Thịt Nướng","Food","195 Cô Giang, District 1",


            foodRecommendAdapter = new FoodRecommendAdapter(this, R.layout.layout_item_recommend, recommendItems);
            listViewFood.setAdapter(foodRecommendAdapter);
        }

        private void initComponents() {
            listViewFood = (ListView) this.findViewById(R.id.food_recommend_list);
            btnMap = (Button) findViewById(R.id.btn_map);
            btnMyList = (Button) findViewById(R.id.btn_mylists);
        }

        public void mapActivity(View view) {
            Intent intent = new Intent(FoodRecommend.this, MapsActivity.class);
            startActivity(intent);
        }

        public void mylistActivity(View view) {
            Intent intent = new Intent(FoodRecommend.this, MyList.class);
            startActivity(intent);
        }
    }

监视器出错

06-20 09:30:23.735 9834-9834/com.example.phy.hochiminhcity E/InputEventReceiver: Exception dispatching input event.
06-20 09:30:23.736 9834-9834/com.example.phy.hochiminhcity D/AndroidRuntime: Shutting down VM
06-20 09:30:23.738 9834-9834/com.example.phy.hochiminhcity E/AndroidRuntime: FATAL EXCEPTION: main
                                                                             Process: com.example.phy.hochiminhcity, PID: 9834
                                                                             java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)' on a null object reference
                                                                                 at com.example.phy.hochiminhcity.FoodRecommendAdapter.getView(FoodRecommendAdapter.java:43)
                                                                                 at android.widget.AbsListView.obtainView(AbsListView.java:2363)
                                                                                 at android.widget.ListView.makeAndAddView(ListView.java:1970)
                                                                                 at android.widget.ListView.fillDown(ListView.java:704)
                                                                                 at android.widget.ListView.fillGap(ListView.java:668)
                                                                                 at android.widget.AbsListView.trackMotionScroll(AbsListView.java:5186)
                                                                                 at android.widget.ListView.trackMotionScroll(ListView.java:1889)
                                                                                 at android.widget.AbsListView.scrollIfNeeded(AbsListView.java:3568)
                                                                                 at android.widget.AbsListView.onTouchMove(AbsListView.java:3977)
                                                                                 at android.widget.AbsListView.onTouchEvent(AbsListView.java:3802)
                                                                                 at android.view.View.dispatchTouchEvent(View.java:10023)
                                                                                 at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2626)
                                                                                 at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2307)
                                                                                 at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632)
                                                                                 at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2321)
                                                                                 at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632)
                                                                                 at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2321)
                                                                                 at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632)
                                                                                 at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2321)
                                                                                 at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632)
                                                                                 at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2321)
                                                                                 at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632)
                                                                                 at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2321)
                                                                                 at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632)
                                                                                 at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2321)
                                                                                 at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:413)
                                                                                 at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1808)
                                                                                 at android.app.Activity.dispatchTouchEvent(Activity.java:3061)
                                                                                 at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:71)
                                                                                 at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:375)
                                                                                 at android.view.View.dispatchPointerEvent(View.java:10243)
                                                                                 at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4438)
                                                                                 at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4306)
                                                                                 at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3853)
                                                                                 at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3906)
                                                                                 at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3872)
                                                                                 at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3999)
                                                                                 at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3880)
                                                                                 at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4056)
                                                                                 at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3853)
                                                                                 at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3906)
                                                                                 at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3872)
                                                                                 at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3880)
                                                                                 at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3853)
                                                                                 at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6246)
                                                                                 at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6220)
                                                                                 at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6181)
                                                                                 at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6349)
                                                                                 at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
                                                                                 at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
06-20 09:30:23.739 9834-9834/com.example.phy.hochiminhcity E/AndroidRuntime:     at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:176)
                                                                                 at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:6320)
                                                                                 at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:6372)
                                                                                 at android.view.Choreographer$CallbackRecord.run(Choreographer.java:874)
                                                                                 at android.view.Choreographer.doCallbacks(Choreographer.java:686)
                                                                                 at android.view.Choreographer.doFrame(Choreographer.java:615)
                                                                                 at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)
                                                                                 at android.os.Handler.handleCallback(Handler.java:751)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                 at android.os.Looper.loop(Looper.java:154)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
06-20 09:30:23.748 1696-3221/system_process W/ActivityManager:   Force finishing activity com.example.phy.hochiminhcity/.FoodRecommend

我不知道为什么它在“空对象引用”上说“尝试调用虚方法'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)',而我已经在FoodRecommend中初始化了位图类

2 个答案:

答案 0 :(得分:2)

像这样更改你的代码

@Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
        holder = new ViewHolder();
            LayoutInflater layoutInflater = ((Activity) 
                  this.context).getLayoutInflater();
            convertView = layoutInflater.inflate(this.reID, parent, false);
            holder.imageView = (ImageView) convertView .findViewById(R.id.iv_recommend);
            holder.textViewName =(TextView) convertView .findViewById(R.id.tv_name);
            holder.textViewType =(TextView) convertView .findViewById(R.id.tv_type);
            holder.textViewAddress =(TextView) convertView .findViewById(R.id.tv_name);
        convertView.setTag(holder);
            } else {
        holder = (ViewHolder)convertView.getTag();
             }
        RecommendItem item = recommendItems.get(position);

            holder.imageView.setImageBitmap(item.getAvatar());
            holder.textViewName.setText(item.getName());
            holder.textViewType.setText(item.getType());
            holder.textViewAddress.setText(item.getName());
            return convertView;
        }

        class ViewHolder {
         public TextView textViewType;
        public ImageView imageView;
        public TextView textViewType;
        public TextView textViewAddress;

    }

答案 1 :(得分:1)

您必须没有正确初始化ImageView。您必须从不正确的布局中提供引用ID。确保xml布局中的引用ID与预期的ImageView匹配。