我收到此错误:
试图调用虚拟方法'android.view.Window $ Callback 空对象引用上的android.view.Window.getCallback()'
public class AdaptadorCarrito extends
RecyclerView.Adapter<AdaptadorCarrito.ViewHolderCarrito> {
ArrayList<CarritoVo> listaCarrito;
AdaptadorCarrito adapter;
RecyclerView.Adapter madapter;
int i = 1;
public AdaptadorCarrito(ArrayList<CarritoVo> listaCarrito) {
this.listaCarrito = listaCarrito;
}
@NonNull
@Override
public ViewHolderCarrito onCreateViewHolder(@NonNull final ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list_carrito, null, false);
final ViewHolderCarrito myHolder = new ViewHolderCarrito(view);
final CafeteriaDB cafeteriaDB = new CafeteriaDB(parent.getContext());
myHolder.item_list_carrito.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int newPosition = myHolder.getAdapterPosition();
cafeteriaDB.eliminarProducto(myHolder.etiNombre.getText().toString(), myHolder.etiInfo.getText().toString());
eliminarProducto(newPosition);
// i++;
// Toast.makeText(parent.getContext(),"Producto Eliminado "+i+" eliminado",Toast.LENGTH_SHORT).show();
CarritoCompras obj = new CarritoCompras();
TextView tv = obj.getTextView(); //Error Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
tv.setText(String.valueOf(cafeteriaDB.getTotal()));
}
});
return myHolder;
}
private void eliminarProducto(int position) {
listaCarrito.remove(position);
notifyItemRemoved(position);
}
@Override
public void onBindViewHolder(@NonNull ViewHolderCarrito holder, int position) {
CarritoVo positions = listaCarrito.get(position);
holder.etiNombre.setText(listaCarrito.get(position).getNombre());
holder.etiPrecio.setText(String.valueOf(listaCarrito.get(position).getPrecio()));
holder.etiInfo.setText(listaCarrito.get(position).getInfo());
holder.etiCantidades.setText(String.valueOf(listaCarrito.get(position).getCantidad()));
holder.etiFoto.setImageBitmap(positions.getFoto());
}
@Override
public int getItemCount() {
//return listaCarrito.size();
return (listaCarrito == null) ? 0 : listaCarrito.size();
}
public class ViewHolderCarrito extends RecyclerView.ViewHolder {
LinearLayout item_list_carrito;
TextView etiNombre, etiPrecio, etiInfo, etiCantidades, txtTotalB, txtTotalF;
ImageView etiFoto;
public ViewHolderCarrito(@NonNull View itemView) {
super(itemView);
item_list_carrito = (LinearLayout) itemView.findViewById(R.id.id_carrito_item);
etiFoto = (ImageView) itemView.findViewById(R.id.fotoC);
etiNombre = (TextView) itemView.findViewById(R.id.nombreC);
etiPrecio = (TextView) itemView.findViewById(R.id.precioC);
etiInfo = (TextView) itemView.findViewById(R.id.infoC);
etiCantidades = (TextView) itemView.findViewById(R.id.cantidadC);
}
}
}
活动
public class CarritoCompras extends AppCompatActivity implements Carrito.OnFragmentInteractionListener{
TextView txtTotalF;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_carrito_compras);
CafeteriaDB db = new CafeteriaDB(this);
getTextView();
txtTotalF = (TextView)findViewById(R.id.totalF);
txtTotalF.setText(String.valueOf(db.totalFilas()));
}
public TextView getTextView(){
TextView txtTotalB = (TextView)findViewById(R.id.totalB); //Error Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
return txtTotalB;
}
@Override
public void onFragmentInteraction(Uri uri) {
}
}
答案 0 :(得分:0)
您可以按照以下方式做
第1步:
一次加载TextView
TextView txtTotalB; // declare class level
内部onCreate()
txtTotalB = (TextView)findViewById(R.id.totalB);
第2步:
创建方法
public TextView getTextView(){
return txtTotalB
}
第3步:
在适配器类内部访问如下的活动方法
TextView tv = ((CarritoCompras)context).getTextView();
tv.setText(String.valueOf(cafeteriaDB.getTotal())); // now here you done
答案 1 :(得分:0)
您可以使用Interfaces进行通信。
创建如下所示的界面:
outer_decorator
在适配器类中,创建声明类型为public interface RecyclerItemClickListener {
void onItemClick(String text);
}
的变量,并修改适配器的构造函数以接受“ RecyclerItemClickListener”的实例作为参数。即按如下所示修改您的适配器类:
RecyclerItemClickListener
现在在要实例化public class AdaptadorCarrito extends
RecyclerView.Adapter<AdaptadorCarrito.ViewHolderCarrito> {
RecyclerItemClickListener recyclerItemClickListener; //Here we are declaring a variable of the created interface
ArrayList<CarritoVo> listaCarrito;
AdaptadorCarrito adapter;
RecyclerView.Adapter madapter;
int i = 1;
//Modifying the constructor to initialize the above declared listener
public AdaptadorCarrito(ArrayList<CarritoVo> listaCarrito, RecyclerItemClickListener listener) {
this.listaCarrito = listaCarrito;
this.recyclerItemClickListener = listener;
}
@NonNull
@Override
public ViewHolderCarrito onCreateViewHolder(@NonNull final ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list_carrito, null, false);
final ViewHolderCarrito myHolder = new ViewHolderCarrito(view);
final CafeteriaDB cafeteriaDB = new CafeteriaDB(parent.getContext());
myHolder.item_list_carrito.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int newPosition = myHolder.getAdapterPosition();
cafeteriaDB.eliminarProducto(myHolder.etiNombre.getText().toString(), myHolder.etiInfo.getText().toString());
eliminarProducto(newPosition);
// i++;
// Toast.makeText(parent.getContext(),"Producto Eliminado "+i+" eliminado",Toast.LENGTH_SHORT).show();
CarritoCompras obj = new CarritoCompras();
/*TextView tv = obj.getTextView(); //Error Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
tv.setText(String.valueOf(cafeteriaDB.getTotal()));*/
//Here we are passing the test to be displayed in the textview to your activity
recyclerItemClickListener.onItemClick(String.valueOf(cafeteriaDB.getTotal()));
}
});
return myHolder;
}
private void eliminarProducto(int position) {
listaCarrito.remove(position);
notifyItemRemoved(position);
}
@Override
public void onBindViewHolder(@NonNull ViewHolderCarrito holder, int position) {
CarritoVo positions = listaCarrito.get(position);
holder.etiNombre.setText(listaCarrito.get(position).getNombre());
holder.etiPrecio.setText(String.valueOf(listaCarrito.get(position).getPrecio()));
holder.etiInfo.setText(listaCarrito.get(position).getInfo());
holder.etiCantidades.setText(String.valueOf(listaCarrito.get(position).getCantidad()));
holder.etiFoto.setImageBitmap(positions.getFoto());
}
@Override
public int getItemCount() {
//return listaCarrito.size();
return (listaCarrito == null) ? 0 : listaCarrito.size();
}
public class ViewHolderCarrito extends RecyclerView.ViewHolder {
LinearLayout item_list_carrito;
TextView etiNombre, etiPrecio, etiInfo, etiCantidades, txtTotalB, txtTotalF;
ImageView etiFoto;
public ViewHolderCarrito(@NonNull View itemView) {
super(itemView);
item_list_carrito = (LinearLayout) itemView.findViewById(R.id.id_carrito_item);
etiFoto = (ImageView) itemView.findViewById(R.id.fotoC);
etiNombre = (TextView) itemView.findViewById(R.id.nombreC);
etiPrecio = (TextView) itemView.findViewById(R.id.precioC);
etiInfo = (TextView) itemView.findViewById(R.id.infoC);
etiCantidades = (TextView) itemView.findViewById(R.id.cantidadC);
}
}
}
对象的Activity中,需要将AdaptadorCarrito
的实例作为参数传递给适配器的构造函数。
示例:
RecyclerItemClickListener
答案 2 :(得分:0)
感谢您的回答,我得以解决自己的问题,即更新了活动中的文本视图,但需要另一个班级中的事件的帮助。
首先在适配器的构造函数中添加一个活动
Activity activity;
public AdaptadorCarrito(ArrayList<CarritoVo> nlistaCarrito, Activity nactivity) {
nListCarrito = nlistaCarrito;
this.activity = nactivity;
}
然后以这种方式调用TextView:
public ViewHolderCarrito onCreateViewHolder(@NonNull final ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list_carrito,null,false);
final ViewHolderCarrito myHolder = new ViewHolderCarrito(view);
final CafeteriaDB cafeteriaDB = new CafeteriaDB(parent.getContext());;
final CarritoCompras carrito = new CarritoCompras();
---->final TextView txtTotalB = (TextView)this.activity.findViewById(R.id.totalB);
txtTotalB.setText(String.valueOf(cafeteriaDB.getTotal()));
myHolder.item_list_carrito.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int newPosition = myHolder.getAdapterPosition();
cafeteriaDB.eliminarProducto(myHolder.etiNombre.getText().toString(), myHolder.etiInfo.getText().toString());
eliminarProducto(newPosition);
Toast.makeText(parent.getContext(),"Producto Eliminado",Toast.LENGTH_SHORT).show();
-----> txtTotalB.setText(String.valueOf(cafeteriaDB.getTotal()));
}
});
return myHolder;
}
,最后在活动中创建适配器的实例
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_carrito_compras);
ArrayList<CarritoVo> nLlistaCarrito = null;
------>AdaptadorCarrito adapter = new AdaptadorCarrito(nLlistaCarrito,this);
CafeteriaDB cafeteriaDB = new CafeteriaDB(this);
TextView txtTotalB = (TextView)findViewById(R.id.totalB);
txtTotalB.setText(String.valueOf(cafeteriaDB.totalFilas()));
TextView txtTotalF = (TextView)findViewById(R.id.totalF);
txtTotalF.setText(String.valueOf(cafeteriaDB.totalFilas()));
}