在我的列表视图中,我添加了一个带有图标/图像的按钮,并尝试使用该按钮与列表视图中的字符串进行通话。但以某种方式无法正常工作或显示错误
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;
}
}
答案 0 :(得分: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;
}
...
}
要改进:
最好使用包含所有员工信息的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;
}
}