java.lang.NullPointerException:';void android.widget.Button.setOnClickListener(android.view.view$OnClickListener)和#x27;关于空对象引用
我有一个问题,我有一个按钮,它位于一个没有活动(没有类)的布局中,该布局称为项目列表产品。该按钮在那里,但我想在我的HomeFragment中使用.setOnClickListener事件。有办法解决这个问题吗?因为这就是它给我错误的原因java.lang.NullPointerException:';void android.widget.Button.setOnClickListener(android.view.view$OnClickListener)和#x27;关于空对象引用,java,android,button,nullpointerexception,Java,Android,Button,Nullpointerexception,我有一个问题,我有一个按钮,它位于一个没有活动(没有类)的布局中,该布局称为项目列表产品。该按钮在那里,但我想在我的HomeFragment中使用.setOnClickListener事件。有办法解决这个问题吗?因为这就是它给我错误的原因 Mi codigo: HomeFragment.class public class HomeFragment extends Fragment { private HomeViewModel homeViewModel;
Mi codigo: HomeFragment.class
public class HomeFragment extends Fragment {
private HomeViewModel homeViewModel;
Button bttn_comprar;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
homeViewModel =
ViewModelProviders.of(this).get(HomeViewModel.class);
View root = inflater.inflate(R.layout.fragment_home, container, false);
bttn_comprar = (Button) root.findViewById(R.id.comprarBttn);
//This is where I get the error, since the button is in a different layout.
bttn_comprar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
}
Mi codigo de layout: item_list_productos
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/idImagen"
android:layout_width="wrap_content"
android:src="@mipmap/ic_launcher"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_marginRight="10dp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/nombreId"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:text="Nombre producto"
android:layout_marginBottom="5dp"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/precioId"
android:text="Precio"
android:textSize="20dp"
android:textColor="#000000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/comprarBttn"
android:layout_width="230dp"
android:layout_height="35dp"
android:textColor="#FFFFFF"
android:background="#248761"
android:text="Comprar"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
Mi-codigo:HomeFragment.class
公共类HomeFragment扩展了片段{
私有HomeViewModel HomeViewModel;
按钮bttn\U COMPAR;
公共视图onCreateView(@NonNull layoutiner充气机,
视图组容器,捆绑包savedInstanceState){
homeViewModel=
ViewModelProviders.of(this.get)(HomeViewModel.class);
视图根=充气机。充气(R.layout.fragment\u home,container,false);
bttn_compar=(按钮)root.findviewbyd(R.id.comparbbtn);
//这就是我得到错误的地方,因为按钮位于不同的布局中。
bttn_compar.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
}
});
}
Mi codigo de布局:项目\列表\产品
您试图在不是片段布局子视图的视图上调用onClicklisterner
public class HomeFragment extends Fragment {
private HomeViewModel homeViewModel;
Button bttn_comprar;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
homeViewModel = ViewModelProviders.of(this).get(HomeViewModel.class);
View root = inflater.inflate(R.layout.item_list_productos, container, false);
bttn_comprar = (Button) root.findViewById(R.id.comprarBttn);
bttn_comprar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
}
将您的片段布局更改为item\u list\u productos.xml或将您的按钮comparbttn添加到fragment\u home.xml您的
fragment\u home
在哪里,而不是fragment\u home
使用item\u list\u productos
作为此布局中的comparbttn
按钮t如果项目列表\u产品
是列表视图
或回收视图
适配器
的布局,您不能像那样设置按钮
的OnClickListener
。它需要在适配器
中准确地处理,感谢您理解我的问题,这正是我试图做的。但是如何在适配器上处理它?这取决于它是在列表视图
中还是在回收视图
中。首先,我要说的是,列表项中通常没有一个按钮
。如果每个项只有一个单击操作,通常的模式是单击整个项本身来处理。如果更改将
从布局中删除后,列表视图只需要在其上设置一个OnItemClickListener
。但是,如果要使用按钮,或者如果这是用于回收视图
,则需要在适配器中处理它。