当我点击btnSubmit时,我的应用程序崩溃了。并给出此错误
java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'android.content.res.Resources $ Theme android.content.Context.getTheme()'。
下面写的代码;
公共类RecycleAdoptor扩展了RecyclerView.Adapter {
Context cont;
DataSource ds;
Double lon = 0.0;
Double lat = 0.0;
RecycleAdoptor(Context paramContext)
{
this.cont = paramContext;
}
private List<Employee> mDocs;
@Override
public RecycleAdoptor.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Log.e("jarvis", "RecylerAdoptor");
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View v = inflater.inflate(R.layout.activity_list_items, parent, false);
// set the view's size, margins, paddings and layout parameters
ViewHolder vh = new ViewHolder(v);
return vh;
}
public RecycleAdoptor(List<Employee> myDataset, Context context) {
mDocs = myDataset;
}
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
final Employee emp = mDocs.get(position);
holder.empID.setText(emp.getID());
holder.empName.setText(emp.getEmpName());
holder.empDesignation.setText(emp.getDesignation());
holder.empSpeciality.setText(emp.getSpeciality());
holder.empAddress.setText(emp.getAddress());
}
@Override
public int getItemCount() {
return mDocs.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView empID;
public TextView docID;
public TextView empName;
public TextView empAddress;
public TextView empDesignation;
public TextView empSpeciality;
public TextView empStatus;
public Button btnSaveAttd;
public Spinner spinner;
public View layout;
public ViewHolder(View v) {
super(v);
layout = v;
empID = (TextView) v.findViewById(R.id.emp_id);
empName = (TextView) v.findViewById(R.id.empName);
empDesignation = (TextView) v.findViewById(R.id.empDesignation);
empSpeciality = (TextView) v.findViewById(R.id.empSpecialist);
empAddress = (TextView) v.findViewById(R.id.empAddress);
spinner = (Spinner) v.findViewById(R.id.spinner1);
btnSaveAttd = (Button) v.findViewById(R.id.btnSaveAttd);
btnSaveAttd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Date currentTime = Calendar.getInstance().getTime();
double lon = ProfileActivity.Lon;
double lat = ProfileActivity.Lat;
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
cont);
alertDialog.setTitle("Leave application?");
alertDialog.setMessage("Are you sure you want to leave the application?");
alertDialog.setIcon(R.drawable.ic_menu_camera);
alertDialog.setPositiveButton("YES",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.setNegativeButton("NO",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.show();
}
});
}
}
}
对不起英语不好。但需要认真的帮助...
答案 0 :(得分:0)
我上传了我的答案。和它的工作很好,我提到了我在哪里做错了。
Context cont;
DataSource ds;
Double lon = 0.0;
Double lat = 0.0;
RecycleAdoptor(Context paramContext)
{
this.cont = paramContext;
}
private List<Employee> mDocs;
@Override
public RecycleAdoptor.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Log.e("jarvis", "RecylerAdoptor");
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View v = inflater.inflate(R.layout.activity_list_items, parent, false);
// set the view's size, margins, paddings and layout parameters
ViewHolder vh = new ViewHolder(v);
return vh;
}
public RecycleAdoptor(List<Employee> myDataset, Context context) {
mDocs = myDataset;
}
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
final Employee emp = mDocs.get(position);
holder.empID.setText(emp.getID());
holder.empName.setText(emp.getEmpName());
holder.empDesignation.setText(emp.getDesignation());
holder.empSpeciality.setText(emp.getSpeciality());
holder.empAddress.setText(emp.getAddress());
}
@Override
public int getItemCount() {
return mDocs.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView empID;
public TextView docID;
public TextView empName;
public TextView empAddress;
public TextView empDesignation;
public TextView empSpeciality;
public TextView empStatus;
public Button btnSaveAttd;
public Spinner spinner;
public View layout;
public ViewHolder(View v) {
super(v);
layout = v;
empID = (TextView) v.findViewById(R.id.emp_id);
empName = (TextView) v.findViewById(R.id.empName);
empDesignation = (TextView) v.findViewById(R.id.empDesignation);
empSpeciality = (TextView) v.findViewById(R.id.empSpecialist);
empAddress = (TextView) v.findViewById(R.id.empAddress);
spinner = (Spinner) v.findViewById(R.id.spinner1);
btnSaveAttd = (Button) v.findViewById(R.id.btnSaveAttd);
btnSaveAttd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Date currentTime = Calendar.getInstance().getTime();
double lon = ProfileActivity.Lon;
double lat = ProfileActivity.Lat;
AlertDialog.Builder alertDialog = new AlertDialog.Builder(v.getContext());//Here I have to use v.getContext() istead of just cont.
alertDialog.setTitle("Leave application?");
alertDialog.setMessage("Are you sure you want to leave the application?");
alertDialog.setIcon(R.drawable.ic_menu_camera);
alertDialog.setPositiveButton("YES",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.setNegativeButton("NO",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.show();
}
});
}
}