Я сделал приложение Android, в котором я делаю Tab Layout, теоретически фрагмент я использую некий cardView в recyclerView. Я хочу открыть Activity из этого представления карты, но при запуске этого приложения на мобильном телефоне появляется следующая ошибка.
Фрагмент теории, который показывает список элементов для утилизации:
public class Theory extends Fragment {
private static final String TAG = "Theory";
Context mContext ;
ArrayList<String> titleArrayList;
private RecyclerView mRecyclerView;
View view;
@Nullable
public View onCreateView(@NonNull LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.theory,container,false);
mRecyclerView = (RecyclerView) view.findViewById(R.id.title_layout_recyclerView);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity()); ///////////
mRecyclerView.setLayoutManager( layoutManager);
titleArrayList = new ArrayList<String>();
titleArrayList.add(Constants.INTRODUCTION);
titleArrayList.add(Constants.CHAPTER_2);
titleArrayList.add(Constants.PRINT_FUNC);
final TitleAdapter adapter = new TitleAdapter(mContext, titleArrayList, new CustomItemClickListener() {
@Override
public void onItemClick(View v, int position) {
Intent desIntent = new Intent(getContext(), DescriptionActivity.class);
desIntent.putExtra("titles", titleArrayList.get(position));
startActivity(desIntent);
}
});mRecyclerView.setAdapter(adapter);
return view;
}
}
TitleAdapter для recycler Просмотр операции:
public class TitleAdapter extends RecyclerView.Adapter<TitleAdapter.MyViewHolder>{
private Context mContext;
private ArrayList<String> titleList;
private CustomItemClickListener clickListener;
public TitleAdapter(Context mContext, ArrayList<String> titleList, CustomItemClickListener clickListener) {
this.mContext = mContext;
this.titleList = titleList;
this.clickListener = clickListener;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.title_layout, parent , false);
final MyViewHolder viewHolder = new MyViewHolder(view);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clickListener.onItemClick(view, viewHolder.getPosition());
}
});
return viewHolder ;
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
holder.titleText.setText(titleList.get(position).replace("_"," "));
}
@Override
public int getItemCount() {
return titleList.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder{
TextView titleText ;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
titleText = (TextView) itemView.findViewById(R.id.title_text);
}
}
}
CustomItemClickListener для получения позиции titleViewAdapter:
public interface CustomItemClickListener {
public void onItemClick(View v, int position);
}
Это ошибка:
2020-07-11 12:37:49.884 5616-5616/com.example.pythonlearn E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.pythonlearn, PID: 5616
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.widget.Toast.<init>(Toast.java:104)
at android.widget.Toast.makeText(Toast.java:262)
at com.example.pythonlearn.Theory$1.onItemClick(Theory.java:66)
at com.example.pythonlearn.TitleAdapter$1.onClick(TitleAdapter.java:45)
at android.view.View.performClick(View.java:5619)
at android.view.View$PerformClick.run(View.java:22298)
at android.os.Handler.handleCallback(Handler.java:754)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:165)
at android.app.ActivityThread.main(ActivityThread.java:6375)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:802)