Java 自定义列表视图上的空对象引用出错
我试图通过用户单击在listView上添加相同的值,当用户单击一个按钮时,系统绘制两个Java 自定义列表视图上的空对象引用出错,java,android,Java,Android,我试图通过用户单击在listView上添加相同的值,当用户单击一个按钮时,系统绘制两个EditTexts的值并将其添加到列表中 但当我单击按钮时,错误显示: 01-14 12:46:31.623 21297-21297/com.example.transportor E/AndroidRuntime:致命异常:主 进程:com.example.transportor,PID:21297 java.lang.NullPointerException:尝试调用虚拟方法“java.lang.Objec
EditText
s的值并将其添加到列表中
但当我单击按钮时,错误显示:
01-14 12:46:31.623 21297-21297/com.example.transportor
E/AndroidRuntime:致命异常:主
进程:com.example.transportor,PID:21297
java.lang.NullPointerException:尝试调用虚拟方法“java.lang.Object”
空值上的android.content.Context.getSystemService(java.lang.String)“”
对象引用
位于android.widget.ArrayAdapter.init(ArrayAdapter.java:310)
在android.widget.ArrayAdapter。(ArrayAdapter.java:153)
在com.example.transportor.product\u list\u adapter.(product\u list\u adapter.java:24)
在com.example.transportor.ui.home.HomeFragment$1.onClick(HomeFragment.java:59)上
在android.view.view.performClick上(view.java:4757)
在android.view.view$PerformClick.run(view.java:19757)
位于android.os.Handler.handleCallback(Handler.java:739)
位于android.os.Handler.dispatchMessage(Handler.java:95)
位于android.os.Looper.loop(Looper.java:135)
位于android.app.ActivityThread.main(ActivityThread.java:5235)
位于java.lang.reflect.Method.invoke(本机方法)
位于java.lang.reflect.Method.invoke(Method.java:372)
在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)上
位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
我的家庭片段:
Context mcontext;
Button addListe = (Button)root.findViewById(R.id.addL);
final ListView productListe = (ListView)root.findViewById(R.id.productList);
addListe.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
product_list PL = new product_list(quantity.getText().toString(),produit.getText().toString());
ArrayList<product_list> PLL = new ArrayList<>();
PLL.add(PL);
product_list_adapter p_l_a = new product_list_adapter(mcontext,R.layout.adapter_product_list,PLL);
productListe.setAdapter(p_l_a);
}
});
和产品列表适配器:
private Context mContext;
int mRessource;
public product_list_adapter(@NonNull Context context, int resource, @NonNull ArrayList<product_list> objects) {
super(context, resource, objects);
this.mContext = mContext;
mRessource = resource;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
//get infos
String quatity = getItem(position).getQuantity();
String product = getItem(position).getProduct();
product_list PL = new product_list(quatity,product);
LayoutInflater iniat = LayoutInflater.from(mContext);
convertView = iniat.inflate(mRessource,parent,false);
TextView Tquantity = (TextView)convertView.findViewById(R.id.TextView1);
TextView Tproduct = (TextView)convertView.findViewById(R.id.TextView2);
Tquantity.setText(quatity);
Tproduct.setText(product);
return convertView;
}
私有上下文mContext;
int mRessource;
公共产品列表适配器(@NonNull上下文上下文,int资源,@NonNull ArrayList对象){
超级(上下文、资源、对象);
this.mContext=mContext;
mRessource=资源;
}
@非空
@凌驾
公共视图getView(int位置,@Nullable视图convertView,@NonNull视图组父级){
//获取信息
字符串数量=getItem(位置).getQuantity();
字符串product=getItem(position).getProduct();
产品清单PL=新产品清单(数量、产品);
LayoutFlater iniat=LayoutFlater.from(mContext);
convertView=iniat.inflate(mRessource,parent,false);
TextView Tquantity=(TextView)convertView.findViewById(R.id.TextView1);
TextView Tproduct=(TextView)convertView.findViewById(R.id.TextView2);
Tquantity.setText(数量);
Tproduct.setText(产品);
返回视图;
}
您在产品列表适配器构造函数中错误地设置了mContext,因此您的mContext始终为null。您在产品列表适配器构造函数中错误地设置了mContext,因此您的mContext始终为null。不要在适配器构造函数中传递上下文
只需替换LayoutInflater iniat=LayoutInflater.from(mContext)代码>带有
LayoutInflater-iniat=LayoutInflater.from(parent.getContext())代码>不要在适配器构造函数中传递上下文
只需替换LayoutInflater iniat=LayoutInflater.from(mContext)代码>带有
LayoutInflater-iniat=LayoutInflater.from(parent.getContext())代码>那么如何修复它?因为当我删除constractshoiw上的mcontext mcontext参数时出错*只需在此处传递context而不是mcontext:D:this.mcontext=context;那么如何修复呢?因为当我删除constractshoiw错误*上的mcontext mcontext参数时,只需在此处传递context而不是mcontext:D:this.mcontext=context;顺便说一句,您忘记了在片段FYI中初始化mContext。当我替换LayoutInflater-iniat=LayoutInflater.from(mContext)时代码>带有LayoutInflater-iniat=LayoutInflater.from(parent.getContext())代码>并删除mcontext我收到了dsame错误顺便说一句,您忘记在片段FYI中初始化mcontext。当我替换LayoutInflater iniat=LayoutInflater.from(mcontext)时代码>带有LayoutInflater-iniat=LayoutInflater.from(parent.getContext())
并删除mcontext我得到了dsame错误,您如何分配传递给product\u list\u adapter
构造函数的mcontext
值?如何分配传递给product\u list\u adapter
构造函数的mcontext
值?
private Context mContext;
int mRessource;
public product_list_adapter(@NonNull Context context, int resource, @NonNull ArrayList<product_list> objects) {
super(context, resource, objects);
this.mContext = mContext;
mRessource = resource;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
//get infos
String quatity = getItem(position).getQuantity();
String product = getItem(position).getProduct();
product_list PL = new product_list(quatity,product);
LayoutInflater iniat = LayoutInflater.from(mContext);
convertView = iniat.inflate(mRessource,parent,false);
TextView Tquantity = (TextView)convertView.findViewById(R.id.TextView1);
TextView Tproduct = (TextView)convertView.findViewById(R.id.TextView2);
Tquantity.setText(quatity);
Tproduct.setText(product);
return convertView;
}