-1

I am implementing a card view on a map fragment so on implementing it, i used two sub classes that extends view holder class.so when i am calling .setText() method for the text View of one of the sub classes from the constructor of the main class its returning null i know its due to the scope which it cannot access but how to solve this .This is the code

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
        {
        }
    }
    }

In the logcat it is showing

Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence) on a null object` reference

please tell me how to acces that text view from that constructor.Thanks in advance!!

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
sai pavan
  • 1
  • 4
  • that is beacause your input data is null and you set that in textview. Put Log to check if there is some value retrurning or not. – Divyesh Patel Nov 13 '16 at 16:58
  • I didn't have a proper look at your code, so you'll know all the details better, but in any case `ajk=(TextView)v.findViewById(R.id.olx);` needs to run before `ajk.setText(d);`. Otherwise you get this `NullPointerException`. (A proper answer would require a more careful look at the code.) – Markus Kauppinen Nov 13 '16 at 16:58
  • The method is not returning anything. SetText is a void method... – OneCricketeer Nov 13 '16 at 17:07
  • 1
    Where is this `TextView`? Is there a reason why you are doing `setText` inside the `RecyclerView` adapters constructor? – K Neeraj Lal Nov 13 '16 at 17:09
  • i have passed values from another class and received those from the constructor and i want to print it in text view of clima class – sai pavan Nov 13 '16 at 17:11
  • Where is the variable `d` declared? – K Neeraj Lal Nov 13 '16 at 17:19
  • sry i have deleted it in editing but i have tested it with putting some String variable and printing it but same error – sai pavan Nov 13 '16 at 17:21
  • @saipavan Why don't you do the `setText` in the `onBindViewHolder`? – K Neeraj Lal Nov 13 '16 at 17:31
  • because that value which i want to print in the textView will change every 5sec because it is triggered by location Listener in such a case constructor must me called every time if there is an alternative please tell me – sai pavan Nov 13 '16 at 17:35
  • You could write a method that updates the location (not constructor). Update the value call `notifyDataSetChanged` and set it in `onBindViewHolder`. – K Neeraj Lal Nov 13 '16 at 17:40
  • calling the setText method in onBindViewHolder will fix the problem but i cant call onBindViewHolder continously – sai pavan Nov 13 '16 at 17:42
  • kk i will try ur solution – sai pavan Nov 13 '16 at 17:42
  • u mean there must be a method in onBindViewHolder instead of constructor which i should call – sai pavan Nov 13 '16 at 17:45
  • can u post the code how it should be done plss – sai pavan Nov 13 '16 at 17:53

1 Answers1

0

Remove this from the constructor of the adapter.

ajk.setText(d);

That isn't where it goes (because the view is clearly null there, and you can't assign it at that point). Also remove the member variable for the textview from the adapter class. That isn't necessary.

Notice these lines?

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

Looks similar to what you're trying to do, yes?

Set the text of the Clima hags holder there after you move the TextView into the Clima class


Also, the adapter should know nothing about these, so remove those as well

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

I'd recommend that you read about heterogeneous views in a RecyclerView since that's what you have

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • if i want to print the text for one time i would have used the **.setText()** method in the onBindView holder but i will be updating that text every 5 sec to be more specific the onLocationChanged() method in other class will be sending values to the adapter class through constructor every 5sec – sai pavan Nov 14 '16 at 05:19
  • Sounds like that textview does not deserve to be in the adapter, then, but outside of the ListView – OneCricketeer Nov 14 '16 at 05:23
  • yeah got it instead of putting that text view in the card layout i will be putting it in other layout which will solve the problem – sai pavan Nov 14 '16 at 06:10