Android Image按钮从自定义列表视图拨号/调用NullPointerException错误

问题描述 投票:0回答:1

在我的listview中,我添加了一个带有图标/图像的按钮,并尝试使用listview中的字符串编号从该按钮进行调用。但不知何故它不工作或给我看错

enter image description here

 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at com.rupom.sss.EmployeeContactList.onCreate

这是我的java代码

public class EmployeeContactList  extends Activity {


    public EmployeeContactList(){}
    ListView empList;

    String[] EmpName = {
            "ROB","Fredrik","Mihai","Andru","Gob shafin","Andru","Gob shafin","Andru","Gob shafin","Andru","Gob shafin","Andru","Gob shafin"

    };
    String[] EmpDeg = {
            "Senior Officer","Junior Officer","Officer","Junior Officer","Officer","Junior Officer","Officer","Junior Officer","Officer","Junior Officer","Officer","Junior Officer","Officer"

    };
    String[] EmpMobile = {
            "1245454544552","12345678912","12345678912","12345678912","12345678912","12345678912","12345678912","12345678912","12345678912","12345678912","12345678912","12345678912","12345678912"
    };



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_employee_list);
        empList = findViewById(R.id.employeelistView);

        CustomEmployeeListAdapter customEmployeeListAdapter = new CustomEmployeeListAdapter(getApplicationContext(), EmpName, EmpDeg,EmpMobile);
        empList.setAdapter(customEmployeeListAdapter);

    }


}

定制列表适配器

public class CustomEmployeeListAdapter extends BaseAdapter {
    Context context;
    String[] EmpName;
    String[] EmpDeg;
    String[] EmpMobile;


    LayoutInflater inflter;

    public CustomEmployeeListAdapter(Context applicationContext, String[] EmpName, String[] EmpDeg, String[] EmpMobile) {
        this.context = context;
        this.EmpName = EmpName;
        this.EmpDeg = EmpDeg;
        this.EmpMobile = EmpMobile;
        inflter = (LayoutInflater.from(applicationContext));
    }

    @Override
    public int getCount() {
        return EmpName.length;
    }

    @Override
    public Object getItem(int position) {
        return EmpName[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {

        String text = (String) getItem(position);
        //get the layout
        view = inflter.inflate(R.layout.list_employee, null);
        //get the fields of list view
        TextView tvName = (TextView) view.findViewById(R.id.employee_name);
        TextView tvDeg = (TextView) view.findViewById(R.id.list_sub_emp_deg);
        TextView tvMobile = (TextView) view.findViewById(R.id.list_sub_emp_mobile);



        //set value from string
        tvName.setText(EmpName[position]);
        tvDeg.setText(EmpDeg[position]);
        tvMobile.setText(EmpMobile[position]);


        final TextView mobNumber = (TextView)view.findViewById(R.id.list_sub_emp_mobile);
        final ImageView empCall = (ImageView)view.findViewById(R.id.empCallBtn);
        final String mobile_number = mobNumber.getText().toString();
        // Click listener of button
        empCall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent callIntent = new Intent(Intent.ACTION_CALL);
                callIntent.setData(Uri.parse("tel:"+mobile_number));
                //context.startActivity(callIntent);
                Log.i("PHONENUMBER","Clicked !!!!!!! "+ mobile_number);
            }
        });
        return view;
    }

}
java android baseadapter
1个回答
1
投票

您需要为所有BaseAdapter方法返回正确的值。因此,将代码更新为以下内容:

public class CustomEmployeeListAdapter extends BaseAdapter {
    Context context;
    String[] EmpName;
    String[] EmpDeg;
    String[] EmpMobile;

    ...

    @Override
    public Object getItem(int position) {
        return EmpName[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    ...

}

为了改善:

您最好使用包含所有Employee信息的pojo。例如,您可以使用Employee类:

public class Employee {
  private String name;
  private String degree;
  private String mobile;

  // constructor
  // setter
  // getter

}

然后,您可以在适配器中使用它:

public class CustomEmployeeListAdapter extends BaseAdapter {
    Context context;
    List<Employee> mEmployees;

    ...

    public CustomEmployeeListAdapter(Context ctx, List<Employee> employees) {
        this.context = ctx;
        mEmployees employees;

        ...
    }

    @Override
    public int getCount() {
        return mEmployees.size();
    }

    @Override
    public Object getItem(int position) {
        return mEmployees.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {

        //get the layout
        ...

        Employee employee = (Employee) getItem(position);

        tvName.setText(employee.getName());
        tvDeg.setText(employee.getDegree());
        tvMobile.setText(employee.getMobile());

        return view;
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.