java.lang.NullPointerException:尝试在空对象引用上调用虚方法

时间:2016-11-05 21:42:16

标签: android listview nullpointerexception

我试图看到关于这个问题的每个答案,但没有与我合作

我知道计数器视图返回null但我不知道为什么

在代码中尝试过类似的东西但是没有用

      public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub



    LayoutInflater inflater=activity.getLayoutInflater();

    if(convertView == null){

        convertView=inflater.inflate(R.layout.column_row, null);

        txtFirst=(TextView) convertView.findViewById(R.id.name);
        txtSecond=(TextView) convertView.findViewById(R.id.gender);
        txtThird=(TextView) convertView.findViewById(R.id.age);
        //txtFourth=(TextView) convertView.findViewById(R.id.status);

        if (txtFirst == null) {
            txtFirst.setText("kkkk");
        }
        if (txtSecond == null) {
            txtSecond.setText("kllll");
        }
        if (txtThird == null) {
            txtThird.setText("llllll");
        }

    }
    return convertView;
   }

我的logcat

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference

我的代码

package com.example.bavly.dollorvalueindifferentboanksegypt;



import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;

import static      
com.example.bavly.dollorvalueindifferentboanksegypt.constants.FIRST_COLUMN;
import static   
com.example.bavly.dollorvalueindifferentboanksegypt.constants.SECOND_COLUMN;
import static   
com.example.bavly.dollorvalueindifferentboanksegypt.constants.THIRD_COLUMN;

public class ListViewAdapters extends BaseAdapter {
public ArrayList<HashMap<String, String>> list;
Activity activity;
TextView txtFirst;
TextView txtSecond;
TextView txtThird;
//TextView txtFourth;
public ListViewAdapters(Activity activity,ArrayList<HashMap<String, String>> list){
    super();
    this.activity=activity;
    this.list=list;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return list.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return list.get(position);
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}



@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub



    LayoutInflater inflater=activity.getLayoutInflater();

    if(convertView == null){

        convertView=inflater.inflate(R.layout.column_row, null);

        txtFirst=(TextView) convertView.findViewById(R.id.name);
        txtSecond=(TextView) convertView.findViewById(R.id.gender);
        txtThird=(TextView) convertView.findViewById(R.id.age);
        //txtFourth=(TextView) convertView.findViewById(R.id.status);


    }

    HashMap<String, String> map=list.get(position);
    txtFirst.setText(map.get(FIRST_COLUMN));
    txtSecond.setText(map.get(SECOND_COLUMN));
    txtThird.setText(map.get(THIRD_COLUMN));
    //txtFourth.setText(map.get(FOURTH_COLUMN));

    return convertView;
}

}

1 个答案:

答案 0 :(得分:1)

listview为null。确认findViewById未返回null。