SwitchCompat 在 RecyclerView 中给出空指针异常

SwitchCompat gives null pointer exception in RecyclerView

我有以下代码将 SwitchCompatRecyclerView 一起使用。

列出 SwitchCompat 所在的项目布局: fragment_notification_settings_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="65dp"
    android:background="@android:color/white">

    <ImageView
        android:id="@+id/icon"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_marginStart="15dp"
        android:src="@drawable/settings_about"
        android:scaleType="fitCenter"
        android:adjustViewBounds="true"
        android:layout_centerVertical="true"
        android:tint="@color/Black"
        tools:ignore="ContentDescription"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toEndOf="@+id/icon"
        android:layout_toStartOf="@+id/on_off_switch"
        android:layout_centerVertical="true"
        android:orientation="vertical">

        <TextView
            android:id="@+id/item_title"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_vertical"
            android:paddingStart="15dp"
            android:paddingEnd="0dp"
            android:text="Kaza Takibi"
            android:textSize="18sp"
            android:textColor="@color/Black"
            tools:ignore="HardcodedText"/>

        <TextView
            android:id="@+id/item_description"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_vertical"
            android:paddingStart="15dp"
            android:paddingEnd="0dp"
            android:text="Ezan vakitlerinde namaz kılma uyarısı"
            android:textSize="16sp"
            android:textColor="@color/DarkGray"
            tools:ignore="HardcodedText"/>
    </LinearLayout>

    <android.support.v7.widget.SwitchCompat
        android:id="@+id/on_off_switch"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_centerVertical="true"
        android:layout_alignParentEnd="true"/>

</RelativeLayout>

片段class:

public class NotificationSettingsFragment extends Fragment{

    @Bind(R.id.close_button)
    ImageView closeButtonImageView;
    @Bind(R.id.recycler_view)
    RecyclerView recyclerView;
    private MainActivity activity;
    private Context context;
    private Fragment parent;
    private NotificationSettingsAdapter adapter;

    public NotificationSettingsFragment() {}

    public void setParent(Fragment parent) {
        this.parent = parent;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activity = (MainActivity)getActivity();
        context = activity.getApplicationContext();
        List<NotificationSettingsItem> settingsItems = new ArrayList<>();
        settingsItems.add(
                new NotificationSettingsItem(
                        "Tercihler", "",
                        NotificationSettingsItem.ItemType.ITEM_TYPE_SECTION_HEADER));
        settingsItems.add(
                new NotificationSettingsItem(
                        "Kaza Takibi",
                        "Ezan bildirimlerinde namaz kılma uyarısı",
                        NotificationSettingsItem.ItemType.ITEM_TYPE_APPLICATION_SETTING));
        settingsItems.add(
                new NotificationSettingsItem(
                        "Konumumu Kullan",
                        "Namaz vakitleri için bulunduğum konumu kullan",
                        NotificationSettingsItem.ItemType.ITEM_TYPE_APPLICATION_SETTING));
        adapter = new NotificationSettingsAdapter(context, settingsItems);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView =  inflater.inflate(R.layout.fragment_notification_settings, container, false);
        ButterKnife.bind(this, rootView);

        closeButtonImageView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                if (parent != null) {
                    FragmentManager fm = activity.getSupportFragmentManager();
                    FragmentTransaction ft = fm.beginTransaction();
                    ft.replace(R.id.top_container, parent);
                    ft.commit();
                }
            }
        });

        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerView.setAdapter(adapter);

        return rootView;
    }

    private class NotificationSettingHolder extends RecyclerView.ViewHolder {

        public TextView itemTitle;
        public TextView itemDescription;
        public SwitchCompat mSwitch;
        public NotificationSettingsItem settingsItem;

        public NotificationSettingHolder(View listItemRootView) {

            super(listItemRootView);
            mSwitch = (SwitchCompat) listItemRootView.findViewById(R.id.on_off_switch);
            itemTitle = (TextView) listItemRootView.findViewById(R.id.item_title);
            itemDescription = (TextView) listItemRootView.findViewById(R.id.item_description);
        }

        public void bindSetting(NotificationSettingsItem settingsItem){

            this.settingsItem = settingsItem;

            if (settingsItem.itemType == NotificationSettingsItem.ItemType.ITEM_TYPE_SECTION_HEADER) {
                itemTitle.setText(settingsItem.itemTitle);
            }
            else if (settingsItem.itemType == NotificationSettingsItem.ItemType.ITEM_TYPE_APPLICATION_SETTING){
                itemTitle.setText(settingsItem.itemTitle);
                itemDescription.setText(settingsItem.itemDescription);
                mSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                    }
                });

                mSwitch.setOnTouchListener(new View.OnTouchListener() {

                    @Override
                    public boolean onTouch(View v, MotionEvent event) {

                        if (event.getAction() == MotionEvent.ACTION_DOWN) {
                            mSwitch.getParent().requestDisallowInterceptTouchEvent(true);
                        }

                        return false;
                    }
                });
            }
        }
    }

    private class NotificationSettingsAdapter extends RecyclerView.Adapter<NotificationSettingHolder> {

        private List<NotificationSettingsItem> settingsItems;
        private LayoutInflater inflater;

        public NotificationSettingsAdapter(Context context, List<NotificationSettingsItem> settingsItems) {
            inflater = LayoutInflater.from(context);
            this.settingsItems = settingsItems;
        }

        @Override
        public NotificationSettingHolder onCreateViewHolder(ViewGroup parent, int viewType) {

            if (viewType == NotificationSettingsItem.ItemType.ITEM_TYPE_SECTION_HEADER.ordinal()) {
                return  new NotificationSettingHolder(
                        inflater.inflate(
                                R.layout.fragment_settings_list_section_header,
                                parent,
                                false));
            }
            else if (viewType == NotificationSettingsItem.ItemType.ITEM_TYPE_APPLICATION_SETTING.ordinal()) {
                return  new NotificationSettingHolder(
                        inflater.inflate(
                                R.layout.fragment_notification_settings_item,
                                parent,
                                false));
            }

            return null;
        }

        @Override
        public void onBindViewHolder(NotificationSettingHolder holder, int position) {
            holder.bindSetting(settingsItems.get(position));
        }

        @Override
        public int getItemCount() {

            return settingsItems.size();
        }

        @Override
        public int getItemViewType(int position) {

            NotificationSettingsItem item = settingsItems.get(position);

            switch (item.itemType) {
                case ITEM_TYPE_APPLICATION_SETTING:
                    return 1;
                case ITEM_TYPE_SECTION_HEADER:
                    return 0;
                default:
                    return -1;
            }
        }
    }
}

当我尝试 运行 时,出现了这个错误:

09-09 17:00:07.842  30287-30287/com.yceo.islamatlasi E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.yceo.islamatlasi, PID: 30287
    java.lang.NullPointerException: Attempt to invoke interface method 'int java.lang.CharSequence.length()' on a null object reference
            at android.text.StaticLayout.<init>(StaticLayout.java:49)
            at android.support.v7.widget.SwitchCompat.makeLayout(SwitchCompat.java:606)
            at android.support.v7.widget.SwitchCompat.onMeasure(SwitchCompat.java:526)
            at android.view.View.measure(View.java:17570)
            at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:728)
            at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:464)
            at android.view.View.measure(View.java:17570)
            at android.support.v7.widget.RecyclerView$LayoutManager.measureChildWithMargins(RecyclerView.java:5446)
            at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1328)
            at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1265)
            at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:522)
            at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:1918)
            at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:2155)
            at android.view.View.layout(View.java:15694)
            at android.view.ViewGroup.layout(ViewGroup.java:5039)
            at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1077)
            at android.view.View.layout(View.java:15694)
            at android.view.ViewGroup.layout(ViewGroup.java:5039)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
            at android.view.View.layout(View.java:15694)
            at android.view.ViewGroup.layout(ViewGroup.java:5039)
            at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1077)
            at android.view.View.layout(View.java:15694)
            at android.view.ViewGroup.layout(ViewGroup.java:5039)
            at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1067)
            at android.view.View.layout(View.java:15694)
            at android.view.ViewGroup.layout(ViewGroup.java:5039)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
            at android.view.View.layout(View.java:15694)
            at android.view.ViewGroup.layout(ViewGroup.java:5039)
            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
            at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
            at android.view.View.layout(View.java:15694)
            at android.view.ViewGroup.layout(ViewGroup.java:5039)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
            at android.view.View.layout(View.java:15694)
            at android.view.ViewGroup.layout(ViewGroup.java:5039)
            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
            at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
            at android.view.View.layout(View.java:15694)
            at android.view.ViewGroup.layout(ViewGroup.java:5039)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
            at android.view.View.layout(View.java:15694)
            at android.view.ViewGroup.layout(ViewGroup.java:5039)
            at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2086)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1843)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5891)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
            at android.view.Choreographer.doCallbacks(Choreographer.java:580)
            at android.view.Choreographer.doFrame(Choreographer.java:550)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(L

我该如何处理?

提前致谢。

SwitchCompat 要求您指定 android:textOnandroid:textOff

例子,你可以这样改

<android.support.v7.widget.SwitchCompat
    android:id="@+id/on_off_switch"
    android:textOn="On"
    android:textOff="Off"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:layout_centerVertical="true"
    android:layout_alignParentEnd="true"/>

我知道这是一个老问题但是除了上面接受的解决方案之外,我有同样的情况和原因我没有将 activity 的主题设置为 AppCompat 之一为了解决这个问题,我使用了主题:

<activity
    android:name=".MainActivity"
    android:label="@string/app_name"
    android:theme="@style/Theme.AppCompat.DayNight.NoActionBar" >

您可以使用 AppCompat

的任何主题