我正在尝试在主动性片段中使用recyclerView,但出现此错误:
尝试在空对象引用上调用虚拟方法'void android.widget.CheckBox.setOnCheckedChangeListener(android.widget.CompoundButton $ OnCheckedChangeListener)'。
我不明白为什么它为空。
这是我的片段(ProceduresFragment)
package com.example.ddd;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.ddd.Models.PerformedMessage;
import com.example.ddd.Models.Procedure;
import com.example.ddd.Models.VisitPlace;
import com.example.ddd.dummy.DummyContent;
import com.example.ddd.dummy.DummyContent.DummyItem;
import com.google.gson.Gson;
import com.google.gson.internal.LinkedTreeMap;
import com.google.gson.reflect.TypeToken;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
/**
* A fragment representing a list of Items.
* <p/>
* Activities containing this fragment MUST implement the {@link OnListFragmentInteractionListener}
* interface.
*/
public class ProceduresFragment extends Fragment {
// TODO: Customize parameter argument names
private static final String ARG_COLUMN_COUNT = "column-count";
// TODO: Customize parameters
private int mColumnCount = 1;
private OnListFragmentInteractionListener mListener;
/**
* Mandatory empty constructor for the fragment manager to instantiate the
* fragment (e.g. upon screen orientation changes).
*/
public ProceduresFragment() {
}
// TODO: Customize parameter initialization
@SuppressWarnings("unused")
public static ProceduresFragment newInstance(int columnCount) {
ProceduresFragment fragment = new ProceduresFragment();
Bundle args = new Bundle();
args.putInt(ARG_COLUMN_COUNT, columnCount);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT);
}
}
private RecyclerView recyclerView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_procedures_list, container, false);
procedures = new ArrayList<>();
//get procedures
if(!getProcedures())
Helper.ShowAlert("Error",getContext());
recyclerView = view.getRootView().findViewById(R.id.procedures_list);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
DividerItemDecoration itemDecoration =
new DividerItemDecoration(recyclerView.getContext(), layoutManager.getOrientation());
recyclerView.addItemDecoration(itemDecoration);
//adapter
MyProceduresRecyclerViewAdapter adapter = new MyProceduresRecyclerViewAdapter(procedures, mListener);
recyclerView.setAdapter(adapter);
/*
// 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 MyProceduresRecyclerViewAdapter(procedures, mListener));
}
*/
return view;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnListFragmentInteractionListener) {
mListener = (OnListFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnListFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnListFragmentInteractionListener {
// TODO: Update argument type and name
void onListFragmentInteraction(Procedure item);
}
这是fragment_procedures.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<android.support.v7.widget.RecyclerView
android:id="@+id/procedures_list_recycler"
android:layout_width="390dp"
android:layout_height="512dp">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
这是fragment_procedures_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/procedures_list"
android:name="com.example.ddd.ProceduresFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:layoutManager="android.support.v7.widget.GridLayoutManager"
tools:context=".ProceduresFragment"
tools:listitem="@layout/recycler_view_item" />
这是recycler_view_item.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/procedure_select"
android:layout_width="30dp"
android:layout_height="35dp"
android:layout_marginStart="16dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/procedure_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="4dp"
android:textColor="@android:color/black"
android:textSize="20sp"
app:layout_constraintStart_toEndOf="@+id/procedure_select"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
这是MyProceduresRecyclerViewAdapter.java:
package com.example.ddd;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;
import com.example.ddd.Models.Procedure;
import com.example.ddd.ProceduresFragment.OnListFragmentInteractionListener;
import com.example.ddd.dummy.DummyContent.DummyItem;
import java.util.List;
/**
* {@link RecyclerView.Adapter} that can display a {@link DummyItem} and makes a call to the
* specified {@link OnListFragmentInteractionListener}.
* TODO: Replace the implementation with code for your data type.
*/
public class MyProceduresRecyclerViewAdapter extends RecyclerView.Adapter<MyProceduresRecyclerViewAdapter.ViewHolder> {
private final List<Procedure> mValues;
private final OnListFragmentInteractionListener mListener;
public MyProceduresRecyclerViewAdapter(List<Procedure> 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_procedures, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.mItem = mValues.get(position);
holder.procedureName.setText(mValues.get(position).getName());
if(holder.mItem.isChecked()){
holder.selectionState.setChecked(true);
} else {
holder.selectionState.setChecked(false);
}
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 implements View.OnClickListener {
public final View mView;
public TextView procedureName;
public CheckBox selectionState;
public Procedure mItem;
public ViewHolder(View view) {
super(view);
mView = view;
procedureName = (TextView) view.findViewById(R.id.procedure_name);
selectionState = (CheckBox) view.findViewById(R.id.procedure_select);
view.setOnClickListener(this);
selectionState.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
Toast.makeText(procedureName.getContext(),"Selected Procedure is" +
" " + procedureName.getText(), Toast.LENGTH_LONG).show();
}
else{
}
}
});
}
@Override
public String toString() {
return super.toString() + " '" + procedureName.getText() + "'";
}
@Override
public void onClick(View v) {
}
}
}
这是logcat:
--------- beginning of crash
2018-10-09 18:11:51.106 3086-3086/com.example.ddd E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ddd, PID: 3086
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.CheckBox.setOnCheckedChangeListener(android.widget.CompoundButton$OnCheckedChangeListener)' on a null object reference
at com.example.ddd.MyProceduresRecyclerViewAdapter$ViewHolder.<init>(MyProceduresRecyclerViewAdapter.java:84)
at com.example.ddd.MyProceduresRecyclerViewAdapter.onCreateViewHolder(MyProceduresRecyclerViewAdapter.java:37)
at com.example.ddd.MyProceduresRecyclerViewAdapter.onCreateViewHolder(MyProceduresRecyclerViewAdapter.java:23)
at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6685)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5869)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5752)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5748)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2232)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1559)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1519)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:614)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3812)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3529)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:4082)
at android.view.View.layout(View.java:17702)
at android.view.ViewGroup.layout(ViewGroup.java:5631)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:325)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:17702)
at android.view.ViewGroup.layout(ViewGroup.java:5631)
at android.support.constraint.ConstraintLayout.onLayout(ConstraintLayout.java:1858)
at android.view.View.layout(View.java:17702)
at android.view.ViewGroup.layout(ViewGroup.java:5631)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:325)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:17702)
at android.view.ViewGroup.layout(ViewGroup.java:5631)
at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:443)
at android.view.View.layout(View.java:17702)
at android.view.ViewGroup.layout(ViewGroup.java:5631)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:325)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:17702)
at android.view.ViewGroup.layout(ViewGroup.java:5631)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1762)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1606)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1515)
at android.view.View.layout(View.java:17702)
at android.view.ViewGroup.layout(ViewGroup.java:5631)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:325)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:774)
at android.view.View.layout(View.java:17702)
at android.view.ViewGroup.layout(ViewGroup.java:5631)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2519)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2234)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1372)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6774)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:926)
at android.view.Choreographer.doCallbacks(Choreographer.java:735)
at android.view.Choreographer.doFrame(Choreographer.java:667)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:912)
2018-10-09 18:11:51.106 3086-3086/com.example.ddd E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:761)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:6523)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)
答案 0 :(得分:0)
您夸大了错误的布局
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.fragment_procedures, parent, false);
将R.layout.fragment_procedures
替换为R.layout.recycler_view_item