简体   繁体   中英

NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference

i get that this exception points out that in some part of the imageLens pipeline there is a null object, but cant pinpoint it. it signals line 48 and the setImageResource method but still cant fix it. Also looked at other questions with similar problem and couldn't apply it to this specific code.

This is the Activity with the error

    package com.komorebiestudio.cam_report_funcionality;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

public class LensAdapter extends RecyclerView.Adapter<LensAdapter.LensViewHolder> {
    private ArrayList<LensItem> mLensList;

    public static class LensViewHolder extends RecyclerView.ViewHolder{

        public ImageView mImageLens;
        public TextView mTextViewLens;
        public TextView mTextViewBrand;

        public LensViewHolder(View itemView) {
            super(itemView);
            mImageLens = itemView.findViewById(R.id.lensimage);
            mTextViewLens = itemView.findViewById(R.id.LensTextView);
            mTextViewBrand = itemView.findViewById(R.id.LensBrandTextView);
        }
    }

    //Constructor para ingresar la informacion del recycler al arraylist
    public LensAdapter(ArrayList<LensItem> lensList){
        mLensList = lensList;

    }

    //Crea el constructor para recibir la informacion del recyclerView
    @Override
    public LensViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.lens_activity, parent, false);
        LensViewHolder lvh = new LensViewHolder(v);
        return lvh;
    }

    //recibe la informacion del recycleView y la muestra el los TextViews correspondientes
    @Override
    public void onBindViewHolder(LensViewHolder holder, int position) {
        LensItem currentItem = mLensList.get(position);

        holder.mImageLens.setImageResource(currentItem.getImageLens());
        holder.mTextViewLens.setText(currentItem.getLens());
        holder.mTextViewBrand.setText(currentItem.getBrandLens());

    }

    //Define cuantos items hay en la lista, por default es 0, pero con el cambio por .size
    //es del tamaño del Array
    @Override
    public int getItemCount() {
        return mLensList.size();
    }
}

Here i declared the get method

package com.komorebiestudio.cam_report_funcionality;

public class LensItem {
    private int mImageLens;
    private String mLens;
    private String mBrandLens;

    public LensItem( int imageLens, String lens, String brandLens){
        mImageLens = imageLens;
        mLens = lens;
        mBrandLens = brandLens;

    }

    public int getImageLens(){
        return mImageLens;
    }

    public String getLens() {
        return mLens;
    }

    public String getBrandLens() {
        return mBrandLens;
    }
}

And this is the activity which add it all together

    package com.komorebiestudio.cam_report_funcionality;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.ArrayList;

public class LensActivity extends AppCompatActivity {
    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lens_activity);

        ArrayList <LensItem> lensItem = new ArrayList<>();
        lensItem.add(new LensItem(R.drawable.ic_camera,"24mm","Zeiss Compact Prime"));
        lensItem.add(new LensItem(R.drawable.ic_camera2,"35mm","Angenioux"));
        lensItem.add(new LensItem(R.drawable.ic_camera3,"50mm","Cooke S5I"));

        mRecyclerView = findViewById(R.id.lens_list);
        mRecyclerView.setHasFixedSize(true);
        mLayoutManager = new LinearLayoutManager(this);
        mAdapter = new LensAdapter(lensItem);

        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setAdapter(mAdapter);
    }

}

I identified the error, i was calling the wrong layout on the inflater. Specifically i was calling the RecyclerView layout and should be calling the CardView layout.

It should look like this.

//Crea el constructor para recibir la informacion del recyclerView
@Override
public LensViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.lens_list, parent, false);
    LensViewHolder lvh = new LensViewHolder(v);
    return lvh;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question I got Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference Android Studio: Attempt to invoke virtual method 'void android.widget.ImageButton.setImageResource(int)' on a null object reference Void android.widget.imageview.setimageresource(int)' on a Null Reference(fatal error message in the log cat) java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.CardView.setVisibility(int)' on a null object reference java.lang.NullPointerException: Attempt to invoke virtual method FloatingActionButton.setImageResource(int)' on a null object reference Attempt to invoke virtual method 'void android.widget.TextView.setBackgroundColor(int)' on a null object reference Attempt to invoke virtual method 'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)' on a null object reference java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ArrayAdapter.clear()' on a null object reference Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setVisibility(int)' on a null object reference Attempt to invoke virtual method 'android.view.View android.widget.ImageView.findViewById(int)' on a null object reference
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM