简体   繁体   English

Android适配器getView null

[英]Android Adapter getView null

When it starts run , there is no error in first 5 row and follower 3 row. 开始运行时,前5行和关注者3行没有错误。 But then it gives null pointer exception. 但是随后它给出了空指针异常。 I can't solve why it is happening ? 我无法解决为什么会这样?

Code snippet: 程式码片段:

public View getView(int position, View convertView, ViewGroup viewGroup) {
    PaylasilanHolder holder;
    final int pozisyon = position;
    Object currentpaylasilan = getItem(position);
    Paylasilanlar paylasilan = (Paylasilanlar) currentpaylasilan;
    if (convertView == null) {
        Log.i("tago", "convertview null");
        holder = new PaylasilanHolder();
        if (paylasilan.cesit.equals("text")) {

            convertView = lala.inflate(R.layout.paylasilanyazi,null);
            holder.tv1 = (TextView) convertView.findViewById(R.id.textView11);
            holder.tv2 = (TextView) convertView.findViewById(R.id.textView12);
            holder.tv3 = (TextView) convertView.findViewById(R.id.textView13);
            holder.image1 = (ImageButton) convertView.findViewById(R.id.imageButton11);
            Log.i("tago", "texttexttext");
        } else if (paylasilan.cesit.equals("teklifoto")) {

            convertView = lala.inflate(R.layout.paylasilanresim,null);
            holder.tv1 = (TextView) convertView.findViewById(R.id.textView11);
            holder.tv2 = (TextView) convertView.findViewById(R.id.textView12);
            holder.tv3 = (TextView) convertView.findViewById(R.id.textView13);
            holder.image1 = (ImageButton) convertView.findViewById(R.id.imageButton11);
            Log.i("tago", "tekliteklitekli");
        } else if (paylasilan.cesit.equals("ikilifoto")) {
            Log.i("tago", "ikiliikiliikili");
        } else if (paylasilan.cesit.equals("anket")) {

            convertView = lala.inflate(R.layout.paylasilananket, null);
            holder.etv1 = (EditText) convertView.findViewById(R.id.editText5);
            holder.etv2 = (EditText) convertView.findViewById(R.id.editText6);
            holder.etv3 = (EditText) convertView.findViewById(R.id.editText7);
            holder.etv4 = (EditText) convertView.findViewById(R.id.editText8);
            Log.i("tago", "anketanketanket");
        }

        convertView.setTag(holder);

    } else {
        holder = (PaylasilanHolder) convertView.getTag();
    }

    if (paylasilan.cesit.equals("text")) {
        holder.tv1.setText(paylasilanlarListesi.get(position).getGonderenid());
        holder.tv2.setText(paylasilanlarListesi.get(position).getYaziveyaurl());
    } else if (paylasilan.cesit.equals("teklifoto")) {
        holder.tv3.setText(paylasilanlarListesi.get(position).getGonderenid());
        new urldenResim(holder.image1).execute(paylasilanlarListesi.get(position).getYaziveyaurl());
    } else if (paylasilan.cesit.equals("ikilifoto")) {

    } else if (paylasilan.cesit.equals("anket")) {
        holder.etv1.setText(paylasilanlarListesi.get(position).getQuestion());
        holder.etv2.setText(paylasilanlarListesi.get(position).getOption1());
        holder.etv3.setText(paylasilanlarListesi.get(position).getOption2());
        holder.etv4.setText(paylasilanlarListesi.get(position).getOption3());
    }
    return convertView;
}

It gives : 它给 :

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

 com.example.bahadir.myapplicationn.PaylasilanlarAdapter.getView(PaylasilanlarAdapter.java:128)

in holder.tv3.settext() method. 在holder.tv3.settext()方法中。

How can i overcome it ? 我该如何克服呢?

You can't mix different views like that. 您不能像这样混合使用不同的视图。 Because if for example you have first 10 items paylasilan.cesit.equals("anj=ket") and after that some other items, adapter views would be already initialized with R.layout.paylasilananket and sure that holder.tv3.setText would give you NullPointerException 因为例如,如果您有前10个项目paylasilan.cesit.equals("anj=ket") ,然后还有其他一些项目,则适配器视图将已经使用R.layout.paylasilananket进行了初始化,并确保holder.tv3.setText将给出您NullPointerException

You should either mix all layouts into one layout and show/hide parts according to item which is pretty bad or use viewTypes like in this tutorial: http://www.survivingwithandroid.com/2014/08/android-listview-with-multiple-row.html 您应该将所有布局混合到一个布局中,然后根据非常糟糕的项目显示/隐藏零件,或者使用本教程中的viewTypes: http : //www.survivingwithandroid.com/2014/08/android-listview-with-multiple -row.html

Since you're using only one kind of a view holder I would think, that you're also handling one kind of view in your adapter. 由于我认为您仅使用一种视图持有者,因此您也在适配器中处理一种视图。 So you're creating a view of one type and reuse it with another. 因此,您正在创建一种类型的视图,然后将其与另一种类型复用。

You have to override getViewTypeCount() and getItemViewType(int position) to return the correct values for your model to work. 您必须重写getViewTypeCount()getItemViewType(int position)才能为模型工作返回正确的值。

You can't do this as you're doing now. 您无法像现在一样这样做。 The crash arises when you try to reuse a cell which is of a different kind, and you're trying to get a reference to a non-existance text-field. 当您尝试重用其他类型的单元格并且试图获取对不存在的文本字段的引用时,就会发生崩溃。

You have to add another field to "PaylasilanHolder" and set that to the type of cell when you inflate a new cell. 您必须在“ PaylasilanHolder”中添加另一个字段,然后在为新单元格充气时将其设置为单元格的类型。

When you are reusing a cell ie: when "convertView != null", you also have to check whether the cell types are the same by referring to the new field you defined in "PaylasilanHolder". 当您重复使用一个单元格时,即:当“ convertView!= null”时,还必须通过引用您在“ PaylasilanHolder”中定义的新字段来检查单元格类型是否相同。 Otherwise, inflate the cell again. 否则,请再次为单元充气。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM