gpt4 book ai didi

java - Android 图像按钮 从自定义 ListView 拨号/调用 错误并出现 NullPointerException

转载 作者:行者123 更新时间:2023-12-02 10:12:32 28 4
gpt4 key购买 nike

在我的 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;
}

}

最佳答案

您需要为所有 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;
}

}

关于java - Android 图像按钮 从自定义 ListView 拨号/调用 错误并出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54905475/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com