我正在實現一個地圖片段上的卡片視圖,因此在實現它時,我使用了兩個子類來擴展視圖持有者class.so當我打電話給.setText()方法從主類的構造函數的子類之一的文本查看其返回null我知道它由於它不能訪問,但如何解決這個問題。這範圍是代碼在適配器類中返回null的setText()方法
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
{
}
}
}
在它正在顯示
嘗試在空 調用虛擬方法無效android.widget.TextView.setText(java.lang.CharSequence中)object`參考
請告訴我如何從constructor.Thanks存取權限的文本視圖提前!!
這是因爲您的輸入數據爲空,並且您在textview中設置了該值。放入日誌以檢查是否有一些值重新調整。 –
我沒有正確的看看你的代碼,所以你會更好地瞭解所有的細節,但無論如何'ajk =(TextView)v.findViewById(R.id.olx);'需要在' ajk.setText(d);'。否則,你會得到這個'NullPointerException'。 (一個正確的答案需要仔細查看代碼。) –
該方法沒有返回任何東西。 SetText是一個無效的方法... –