Java Fragment中的按钮正在使应用程序崩溃

Java Fragment中的按钮正在使应用程序崩溃,java,android,android-fragments,button,nullpointerexception,Java,Android,Android Fragments,Button,Nullpointerexception,我在片段中放置了一个按钮和相应的侦听器。每次我调用此活动时,应用程序都会崩溃。Logcat显示空指针异常。下面的代码是Fragment类的一个片段 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout

我在片段中放置了一个按钮和相应的侦听器。每次我调用此活动时,应用程序都会崩溃。Logcat显示空指针异常。下面的代码是Fragment类的一个片段

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_carprofile_list, container, false);

    // Set the adapter
    if (view instanceof RecyclerView) {
        Context context = view.getContext();
        RecyclerView recyclerView = (RecyclerView) view;
        if (mColumnCount <= 1) {
            recyclerView.setLayoutManager(new LinearLayoutManager(context));
        } else {
            recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));
        }
        recyclerView.setAdapter(new MyCarProfileRecyclerViewAdapter(carProfileContent.ITEMS, mListener));
    }

    Button imgbtn = (Button) view.findViewById(R.id.editCarProfileButton);
    imgbtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(new Intent(getActivity(),ReserveActivity.class));
        }
    });

    return view;
}
我一直在研究多个类似的问题,但它似乎对我不起作用。我添加按钮和侦听器的方式是否遗漏了什么

fragment\u carprofile\u list.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/list"
    android:name="com.mikepuno.parkeazy.CarProfileFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    app:layoutManager="LinearLayoutManager"
    tools:context="com.mikepuno.parkeazy.CarProfileFragment"
    tools:listitem="@layout/fragment_carprofile" />
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/plateNumberTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:textAppearance="?attr/textAppearanceListItem" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/brandTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:textAppearance="?attr/textAppearanceListItem" />

        <TextView
            android:id="@+id/modelTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:textAppearance="?attr/textAppearanceListItem" />

        <TextView
            android:id="@+id/colorTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:textAppearance="?attr/textAppearanceListItem" />

        <Button
            android:id="@+id/editCarProfileButton"
            android:layout_width="64dp"
            android:layout_height="64dp"
            android:text="Edit" />

    </LinearLayout>
</LinearLayout>

fragment\u carprofile.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/list"
    android:name="com.mikepuno.parkeazy.CarProfileFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    app:layoutManager="LinearLayoutManager"
    tools:context="com.mikepuno.parkeazy.CarProfileFragment"
    tools:listitem="@layout/fragment_carprofile" />
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/plateNumberTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:textAppearance="?attr/textAppearanceListItem" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/brandTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:textAppearance="?attr/textAppearanceListItem" />

        <TextView
            android:id="@+id/modelTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:textAppearance="?attr/textAppearanceListItem" />

        <TextView
            android:id="@+id/colorTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:textAppearance="?attr/textAppearanceListItem" />

        <Button
            android:id="@+id/editCarProfileButton"
            android:layout_width="64dp"
            android:layout_height="64dp"
            android:text="Edit" />

    </LinearLayout>
</LinearLayout>

适配器类

public class MyCarProfileRecyclerViewAdapter extends RecyclerView.Adapter<MyCarProfileRecyclerViewAdapter.ViewHolder> {

    private final List<CarProfile> mValues;
    private final OnListFragmentInteractionListener mListener;

    public MyCarProfileRecyclerViewAdapter(List<CarProfile> items, OnListFragmentInteractionListener listener) {
        mValues = items;
        mListener = listener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.fragment_carprofile, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        holder.mItem = mValues.get(position);
        holder.mPlateNumberTextView.setText(mValues.get(position).getPlateNumber());
        holder.mBrandTextView.setText(mValues.get(position).getBrand());
        holder.mModelTextView.setText(mValues.get(position).getModel());
        holder.mColorTextView.setText(mValues.get(position).getColor());

        holder.mView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != mListener) {
                    // Notify the active callbacks interface (the activity, if the
                    // fragment is attached to one) that an item has been selected.
                    mListener.onListFragmentInteraction(holder.mItem);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mValues.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public final View mView;
        public final TextView mPlateNumberTextView;
        public final TextView mBrandTextView;
        public final TextView mModelTextView;
        public final TextView mColorTextView;
        public CarProfile mItem;

        public ViewHolder(View view) {
            super(view);
            mView = view;
            mPlateNumberTextView = (TextView) view.findViewById(R.id.plateNumberTextView);
            mBrandTextView = (TextView) view.findViewById(R.id.brandTextView);
            mModelTextView = (TextView) view.findViewById(R.id.modelTextView);
            mColorTextView = (TextView) view.findViewById(R.id.colorTextView);
        }

        @Override
        public String toString() {
            return super.toString() + " '" + mPlateNumberTextView.getText() + "'";
        }
    }
}
公共类MyCarproFileRecycleServiceAdapter扩展了RecyclerView.Adapter{ 私人最终清单价值; 私有final OnListFragmentInteractionListener MLListener; 公共MyCarpRofileRecycleServiceAdapter(列表项,OnListFragmentInteractionListener){ M值=项目; mListener=监听器; } @凌驾 public ViewHolder onCreateViewHolder(视图组父级,int-viewType){ View=LayoutInflater.from(parent.getContext()) .充气(R.layout.fragment_carprofile,父级,假); 返回新的ViewHolder(视图); } @凌驾 公共无效onBindViewHolder(最终ViewHolder,内部位置){ holder.mItem=mValues.get(位置); holder.mPlateNumberTextView.setText(mValues.get(position.getPlateNumber()); holder.mBrandTextView.setText(mValues.get(position.getBrand()); holder.mModelTextView.setText(mValues.get(position.getModel()); holder.mColorTextView.setText(mValues.get(position.getColor()); holder.mView.setOnClickListener(新视图.OnClickListener(){ @凌驾 公共void onClick(视图v){ if(null!=mListener){ //通知活动回调接口(活动,如果 //片段被附加到一个)上,表示已选择项目。 mListener.onListFragmentInteraction(holder.mItem); } } }); } @凌驾 public int getItemCount(){ 返回mValues.size(); } 公共类ViewHolder扩展了RecyclerView.ViewHolder{ 公共最终视图mView; 公共最终文本视图mPlateNumberTextView; 公共最终文本视图和文本视图; 公共最终文本视图mModelTextView; 公共最终文本视图mColorTextView; 公共车库螨; 公共视图持有者(视图){ 超级(视图); mView=视图; mPlateNumberTextView=(TextView)view.findviewbyd(R.id.plateNumberTextView); mBrandTextView=(TextView)view.findViewById(R.id.brandTextView); mModelTextView=(TextView)view.findViewById(R.id.modelTextView); mColorTextView=(TextView)view.findViewById(R.id.colorTextView); } @凌驾 公共字符串toString(){ 返回super.toString(); } } }
在膨胀视图后

View view = inflater.inflate(R.layout.fragment_carprofile_list, container, false);
您假设视图是RecyclerView。这意味着根元素是recyclerview

然后尝试通过执行以下操作在recyclerview中找到一个按钮

Button imgbtn = (Button) view.findViewById(R.id.editCarProfileButton);
它当然返回null。然后尝试在空对象上设置单击侦听器

看起来您的xml文件是错误的。若您的每一行都有您试图查找的按钮,那个么您需要在适配器类中捕获它,然后通知fragment执行您的过程

解决方案(编辑):

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/list"
    android:name="com.mikepuno.parkeazy.CarProfileFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    app:layoutManager="LinearLayoutManager"
    tools:context="com.mikepuno.parkeazy.CarProfileFragment"
    tools:listitem="@layout/fragment_carprofile" />
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/plateNumberTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:textAppearance="?attr/textAppearanceListItem" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/brandTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:textAppearance="?attr/textAppearanceListItem" />

        <TextView
            android:id="@+id/modelTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:textAppearance="?attr/textAppearanceListItem" />

        <TextView
            android:id="@+id/colorTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:textAppearance="?attr/textAppearanceListItem" />

        <Button
            android:id="@+id/editCarProfileButton"
            android:layout_width="64dp"
            android:layout_height="64dp"
            android:text="Edit" />

    </LinearLayout>
</LinearLayout>
从片段中的
onCreateView
中删除这些行

Button imgbtn = (Button) view.findViewById(R.id.editCarProfileButton);
    imgbtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(new Intent(getActivity(),ReserveActivity.class));
        }
    });
您已经有了一个通知片段的接口。 所以我们要用它来编辑

因此,最终的适配器类应该是

public class MyCarProfileRecyclerViewAdapter extends RecyclerView.Adapter<MyCarProfileRecyclerViewAdapter.ViewHolder> {

private final List<CarProfile> mValues;
private final OnListFragmentInteractionListener mListener;

public MyCarProfileRecyclerViewAdapter(List<CarProfile> items, OnListFragmentInteractionListener listener) {
    mValues = items;
    mListener = listener;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.fragment_carprofile, parent, false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
    holder.mItem = mValues.get(position);
    holder.mPlateNumberTextView.setText(mValues.get(position).getPlateNumber());
    holder.mBrandTextView.setText(mValues.get(position).getBrand());
    holder.mModelTextView.setText(mValues.get(position).getModel());
    holder.mColorTextView.setText(mValues.get(position).getColor());
}

@Override
public int getItemCount() {
    return mValues.size();
}

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

    public final TextView mPlateNumberTextView;
    public final TextView mBrandTextView;
    public final TextView mModelTextView;
    public final TextView mColorTextView;
    public CarProfile mItem;

    public ViewHolder(View view) {
        super(view);

        view.setClickable(true); // This is important
        view.setOnClickListener(this);

        mPlateNumberTextView = (TextView) view.findViewById(R.id.plateNumberTextView);
        mBrandTextView = (TextView) view.findViewById(R.id.brandTextView);
        mModelTextView = (TextView) view.findViewById(R.id.modelTextView);
        mColorTextView = (TextView) view.findViewById(R.id.colorTextView);
    }

    public void onClick(View view) {
       if (null != mListener) {
            mListener.onListFragmentInteraction(mValues.get(getAdapterPosition()));
       }
    }

    @Override
    public String toString() {
        return super.toString() + " '" + mPlateNumberTextView.getText() + "'";
    }
   }
您可以通过单击按钮上的NotifyFragment来改进代码。因此,您的按钮单击侦听器和通知代码也必须在这里

View view = inflater.inflate(R.layout.fragment_carprofile_list, container, false);

您正在膨胀
fragment\u carprofile\u list.xml
,其中包含RecyclerView。XML中没有提到
按钮
,这就是为什么会出现
NullPointerException

您可以发布完整的错误日志吗?@EmreAktürk请查看更新的日志。请同时发布您的适配器类、片段布局和行布局文件。@EmreAktürk请查看XML文件和适配器类。您好,你有什么建议我可以解决这个问题吗?如果我从充气机中更改fragment\u carprofile\u list.xml,则我要显示的项目将不复存在。感谢您的帮助,帮助我解决了问题。我应该将公共void onListFragmentInteraction放在何处。。。代码块?到实现该方法的类?因为如果是按钮,则不起作用,但空指针错误消失了。我可以通过设置
button btn=view.findViewById(R.id.editCarProfileButton)使按钮起作用;btn.setOnClickListener(此)
;通过学习