CheckBox OnCheckedChangeListener

时间:2015-11-26 14:43:22

标签: android material-design clicklistener

我正在使用navasmdc的Material Design library复选框。

现在,我正在尝试实施OnCheckedChangeListener

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

    setHasOptionsMenu(true);

    mRecyclerView = (RecyclerView) layout.findViewById(R.id.ReviewRecyclerView);

    setData();

    mAdapter = new MainAdapter(getActivity(), data, getIdForSelectedAnlage(), getIdForSelectedMaschine());
    mRecyclerView.setAdapter(mAdapter);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

    mCheckBox = (com.gc.materialdesign.views.CheckBox) layout.findViewById(R.id.review_item_checkbox_simple);
    mCheckBox.setOncheckListener(
        new com.gc.materialdesign.views.CheckBox.OnCheckListener(){
            @Override
            public void onCheck(CheckBox view, boolean check) {
                // Some Action 
            }
        }
    );
}

我收到以下问题:

  

java.lang.NullPointerException:尝试调用虚方法'void   com.gc.materialdesign.views.CheckBox.setOncheckListener(com.gc.materialdesign.views.CheckBox $ OnCheckListener)”   在空对象引用上

我不明白为什么我会得到一个空指针异常..

我的RecyclerView项目的XML布局:

<FrameLayout
    android:id="@+id/anlagen_layout_container"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:padding="2dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:focusable="true"
        android:clickable="true"
        android:foreground="?android:attr/selectableItemBackground"
        android:id="@+id/review_item_cardview_simple">

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="horizontal" >

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="16dp"
                android:id="@+id/review_item_imageview_simple"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"/>

            <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:paddingLeft="16dp"
                android:orientation="vertical" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="#000000"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:text="Maschinenbezeichnung"
                    android:id="@+id/review_item_textview_title_simple"
                    android:layout_gravity="left|top" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="#000000"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:text="Wert für Eigenschaft"
                    android:id="@+id/review_item_textview_subtitle_simple"
                    android:layout_gravity="left|bottom" />

            </LinearLayout>

            <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_gravity="right"
                android:orientation="vertical" >

                <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    xmlns:materialdesign="http://schemas.android.com/apk/res-auto"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">
                    <com.gc.materialdesign.views.CheckBox
                        android:id="@+id/review_item_checkbox_simple"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:background="#1E88E5"/>

                </RelativeLayout>

            </LinearLayout>

        </LinearLayout>

    </android.support.v7.widget.CardView>

</FrameLayout>

0 个答案:

没有答案