我在side by side
上有两个片段MainActivity
LeftFragment
有一个RecyclerView
和一个colors
,并且我想在点击Right Fragment
该应用程序因错误null object reference
而崩溃。
这是我的Github Repo。但是,添加一些代码以供参考。
public class LeftFragment extends Fragment {
public LeftFragment() {
// Required empty public constructor
}
public static LeftFragment newInstance() {
return new LeftFragment();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_left, container, false);
// private OnColorChooseListener mOnColorChooseListener;
ArrayList<Color> mColor = new ArrayList<>();
mColor.add(new Color(getResources().getColor(R.color.colorRed), getResources().getString(R.string.red)));
mColor.add(new Color(getResources().getColor(R.color.colorOrange), getResources().getString(R.string.orange)));
mColor.add(new Color(getResources().getColor(R.color.colorYellow), getResources().getString(R.string.yellow)));
mColor.add(new Color(getResources().getColor(R.color.colorLime), getResources().getString(R.string.lime)));
mColor.add(new Color(getResources().getColor(R.color.colorGreen), getResources().getString(R.string.green)));
mColor.add(new Color(getResources().getColor(R.color.colorCyan), getResources().getString(R.string.cyan)));
mColor.add(new Color(getResources().getColor(R.color.colorBlue), getResources().getString(R.string.blue)));
mColor.add(new Color(getResources().getColor(R.color.colorIndigo), getResources().getString(R.string.indigo)));
mColor.add(new Color(getResources().getColor(R.color.colorViolet), getResources().getString(R.string.violet)));
RecyclerView mRecyclerView = view.findViewById(R.id.color_list_view);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
RecyclerView.Adapter mAdapter = new ColorAdapter(mColor, getActivity());
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
return view;
}
}
public class RightFragment extends Fragment {
ColorAdapter colorAdapter;
ArrayList<Color> color;
public RightFragment() {
// Required empty public constructor
}
public static RightFragment newInstance() {
return new RightFragment();
}
@Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState
) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_right, container, false);
final ConstraintLayout constraintLayout = view.findViewById(R.id.right_color_block);
final TextView textView = view.findViewById(R.id.right_color_text);
colorAdapter.SetItemClickListener(new ColorAdapter.OnItemClickListener() {
@Override
public void OnItemClick(int position) {
constraintLayout.setBackgroundColor(color.get(position).getmColor());
textView.setText(color.get(position).getmLabel());
}
});
return view;
}
}
public class ColorAdapter extends RecyclerView.Adapter<ColorAdapter.ColorViewHolder> {
private List<Color> mColor;
private Context mContext;
private OnItemClickListener mOnItemClickListener;
public ColorAdapter(List<Color> mColor, Context mContext) {
this.mColor = mColor;
this.mContext = mContext;
}
public void SetItemClickListener(OnItemClickListener listener) {
this.mOnItemClickListener = listener;
}
@NonNull
@Override
public ColorViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.color_list_item, viewGroup, false);
return new ColorViewHolder(view, mOnItemClickListener);
}
@Override
public void onBindViewHolder(@NonNull final ColorViewHolder colorViewHolder, final int i) {
final Color color = mColor.get(i);
colorViewHolder.itemColorText.setText(color.getmLabel());
colorViewHolder.itemColorText.setBackgroundColor(color.getmColor());
colorViewHolder.itemColorText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mContext, "Item clicked: " + i, Toast.LENGTH_SHORT).show();
// colorViewHolder.feedbackBlock.setBackgroundColor(color.getmColor());
colorViewHolder.feedbackText.setText(color.getmLabel());
}
});
}
@Override
public int getItemCount() {
return mColor.size();
}
public interface OnItemClickListener {
void OnItemClick(int position);
}
class ColorViewHolder extends RecyclerView.ViewHolder {
public ConstraintLayout itemColorBlock;
public TextView itemColorText;
public ConstraintLayout feedbackBlock;
public TextView feedbackText;
public ColorViewHolder(@NonNull View itemView, final OnItemClickListener listener) {
super(itemView);
itemColorBlock = itemView.findViewById(R.id.color_block);
itemColorText = itemView.findViewById(R.id.color_text);
feedbackBlock = itemView.findViewById(R.id.right_color_block);
feedbackText = itemView.findViewById(R.id.right_color_text);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (listener != null) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION) {
listener.OnItemClick(position);
}
}
}
});
}
}
}
public class Color {
private int mColor;
private String mLabel;
public Color(@ColorInt int color, String label) {
this.mColor = color;
this.mLabel = label;
}
public String getmLabel() {
return mLabel;
}
public void setmLabel(String mLabel) {
this.mLabel = mLabel;
}
public int getmColor() {
return mColor;
}
public void setmColor(@ColorInt int mColor) {
this.mColor = mColor;
}
@NonNull
@Override
public String toString() {
return super.toString();
}
}
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LeftFragment leftFragment = LeftFragment.newInstance();
getSupportFragmentManager().beginTransaction().replace(R.id.left_container, leftFragment).commit();
RightFragment rightFragment = RightFragment.newInstance();
getSupportFragmentManager().beginTransaction().replace(R.id.right_container, rightFragment).commit();
}
}
答案 0 :(得分:0)
RightFragment.class上的应用程序崩溃,因为您像** RightFragment colorAdapteris null **那样调用colorAdapter.SetItemClickListener(第43行),您在不同的片段上设置了适配器,并在不同的片段上实现了侦听器,所以这就是为什么崩溃发生。
您可以将此侦听器应用于LeftFragment(最初在其中设置适配器的地方),然后单击该侦听器(在侦听器回调中),然后打开右侧片段(将选定的数据从LeftFragment发送到RightFragment)This SO Answer can help you in this片段打开后,您将获得选定的颜色数据,在这里可以在RightFragment上使用它。