SwitchCompat在RecyclerView中提供空指针exception

我有以下代码将SwitchCompat与RecyclerView一起使用。

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

         

片段类:

 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 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 { private List settingsItems; private LayoutInflater inflater; public NotificationSettingsAdapter(Context context, List 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.(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

例如,你可以这样改变