setText()方法在适配器类中返回null

时间:2016-11-13 16:51:32

标签: android nullpointerexception recycler-adapter android-viewholder

我在地图片段上实现了一个卡片视图,所以在实现它的时候,我使用了两个扩展视图持有者类的子类。当我为文本视图调用 .setText()方法时其中一个子类来自主类的构造函数,它返回null我知道它由于它无法访问的范围而是如何解决这个问题。这是代码

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

    String a121[] = new String[10];
    RecyclerView recyclerView;
    RecyclerView.LayoutManager layoutManager;
    RecyclerView.Adapter adapter;
    FloatingActionButton a1s, a2s, a3s;
    TextView ajk;
     public static final int WeatherVe = 0;
    public static final int ChronoVe = 1;
    int jh=0,moj=0;
    double lat1,lat2,lon1,lon2,degree,s1,speed,l1,l2,lat,lon;


    public Adap1(String a121[])
    {
        this.a121 = a121;
    }


    public Adap1(double l1,double l2,double lat1,double lat2)
    {
        this.l1=l1;
        this.lat1=lat1;
        this.l2=l2;
        this.lat2=lat2;
        s1 = getDistanceFromLatLonInKm(l1, l2, lat1, lat2);
        System.out.println(l1+" "+l2);
        System.out.println(lat1+""+lat2);
        speed=s1%(0.0019);
        ajk.setText(d);//error is coming here when i am accesing text view of clima class   
    }
    public static class ViewHolder extends RecyclerView.ViewHolder
    {
        public ViewHolder(View v)
        {
            super(v);
        }
    }
    public class WeatherVe extends ViewHolder {
        public TextView itemTitle;
        ImageView acv;
        public TextView itemg;
        public WeatherVe(View v)
        {
            super(v);
            itemTitle = (TextView) v.findViewById(R.id.ass);
            itemg = (TextView) v.findViewById(R.id.ass1);
        }
    }
    public class Clima extends ViewHolder
    {
        public Clima(View v)
        {
            super(v);
            a1s = (FloatingActionButton) v.findViewById(R.id.as);
            a2s = (FloatingActionButton) v.findViewById(R.id.as1);
           ajk=(TextView)v.findViewById(R.id.olx);
            System.out.println(ajk);
            System.out.println("Initialization");
        }
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        View v;
        if (viewType == WeatherVe) {
            v = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.card_lay12, parent, false);
            return new WeatherVe(v);
        }
        else
        {
            v = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.card123, parent, false);
            return new Clima(v);
        }
    }
    @Override
    public void onBindViewHolder(ViewHolder holder, int position)
    {
        if (holder.getItemViewType() == WeatherVe) {
            WeatherVe hsolder = (WeatherVe) holder;
            hsolder.itemTitle.setText(a121[position]);
            avd = hsolder.itemTitle.getContext();
            hsolder.itemg.setText(a121[position + 1]);
        } else if(holder.getItemViewType()==ChronoVe)
        {
            Clima hags = (Clima) holder;
        }
        else
        {
        }
    }
    }

在logcat中显示

  

尝试在null上调用虚方法'void android.widget.TextView.setText(java.lang.CharSequence)   对象参考

请告诉我如何从该构造函数访问该文本视图。谢谢!

1 个答案:

答案 0 :(得分:0)

从适配器的构造函数中删除它。

ajk.setText(d);

这不是它的去处(因为那里的视图显然是空的,你不能在那时分配它)。还要从适配器类中删除textview的成员变量。这不是必要的。

注意这些线?

WeatherVe hsolder = (WeatherVe) holder;
hsolder.itemTitle.setText(a121[position]);
avd = hsolder.itemTitle.getContext();
hsolder.itemg.setText(a121[position + 1]);

看起来和你想要做的相似,是吗?

将TextView移动到Clima类后,设置Clima hags持有者的文本

此外,适配器应该不知道这些,所以删除它们

RecyclerView recyclerView;
RecyclerView.LayoutManager layoutManager;
RecyclerView.Adapter adapter;

我建议您阅读heterogeneous views in a RecyclerView,因为这就是您所拥有的