初始化后,Recycler List Adapter为null

时间:2018-04-27 08:51:14

标签: java android android-fragments android-recyclerview recycler-adapter

我的应用有一个带有三个片段的标签栏。在onCreate方法期间,我调用了一个名为initPlantList的函数。此函数初始化我的RecyclerListAdapter。

private void initPlantList(){
    RecyclerView rV = findViewById(R.id.plantsRecycler);
    PlantListAdapter pLA = new PlantListAdapter(this);
    Log.d("PLA", pLA.hasContext());
    rV.setAdapter(pLA);
    rV.setLayoutManager(new LinearLayoutManager(this));
}

我尝试拨打java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setAdapter(android.support.v7.widget.RecyclerView$Adapter)' on a null object reference时收到此错误rV.setAdapter(pLA); 这是我的适配器类

public class PlantListAdapter extends 
RecyclerView.Adapter<PlantListAdapter.ViewHolder>{

    private static final String TAG = "PlantListAdapter";

    private Context mContext;

    public PlantListAdapter(Context c){
        this.mContext = c;
    }

    public String hasContext(){
        if(this.mContext != null){
            return mContext.getPackageResourcePath();
        }
        return "false";
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layoutplantlist, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    Log.d(TAG, "onBindViewHolder: called.");

    holder.image.setImageDrawable(MyApplication.myPlantList.get(position).getImage());

holder.plantName.setText(MyApplication.myPlantList.get(position).getName());
    }

    @Override
    public int getItemCount() {
        return MyApplication.myPlantList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{

        CircleImageView image;
        TextView plantName;
        RelativeLayout plantListItem;

        public ViewHolder(View itemView) {
            super(itemView);
            image = itemView.findViewById(R.id.plantImage);
            plantName = itemView.findViewById(R.id.plantName);
            plantListItem = itemView.findViewById(R.id.plantListItem);
        }
    }
}

有关为什么pLA null object reference作为SalesTable salesTable(boolean _update = false) { SalesTable salesTable = SalesTable::find(this.SalesId, _update); SalesTableDelete salesTableDelete; SalesTable_RU salesTableRU; if (!salesTable && this.SalesId && this.SalesType != SalesType::Journal) { salesTableDelete = SalesTableDelete::find(this.SalesId); if (salesTableDelete.SalesTable) { [salesTable] = salesTableDelete.SalesTable; salesTableRU = SalesTable_RU::findBySalesTable(salesTable.RecId); salesTable.packSalesTable_RU(salesTableRU); } } return salesTable; } 进行审核的任何想法?

1 个答案:

答案 0 :(得分:1)

您的适配器不是null。这是您的Recyclerview。您使用的是片段,因此无法使用(findViewById)。 你必须使用你膨胀的视图找到它。 试试这个:

var button1 = document.getElementById("button");
var text1 = document.getElementById("text");
var output = document.getElementById("result");

button1.addEventListener("click", function(){

    var textInput = text1.value;
    var cleared = new clearingUnwantedCharacters(textInput);
    var marked = new subgroupMarking(cleared.output);
    var prioritizedList = new subgroupPrioritization(marked.parenGroups);
    for(var i = 0; i <= 360; i++){
        var splittedPieces = new splittingOperators(prioritizedList.prioritizedGroups);
        var calculatedPieces = new calculatePieces(splittedPieces.seperatedPieces, i, true);
        console.log(calculatedPieces.result);
    }

});

}

当您在片段onCrrateView()中调用该方法时,只需将视图作为参数传递即可。 希望它能帮到你