У меня есть следующее CardView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/coordinatorLayout">
<fragment
android:id="@+id/mapActivity"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Map.MyMap" />
<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:visibility="gone"
android:id="@+id/cardViewData"
android:backgroundTint="@color/colorPrimary"
app:cardCornerRadius="8dp">
...
Теперь я называю это CardView в своей Деятельности таким образом, чтобы получить его идентификатор и передать это CardView рабочему классу для обработки некоторой логики с ним(Мне просто нужно установить его видимым)
private CardView mCardViewData;
внутри onCreate (), после setContentView
mCardViewData = findViewById(R.id.cardViewData);
new MapsUtils(this,mCardViewData);
Как вы можете видеть, я посылаю это представление карты рабочему классу, где я получаюэто так:
private CardView mCardViewData;
public MapsUtils(Context context,CardView cardView{
this.mCardViewData = cardView;
this.mContext = context;
}
И затем я использую его внутри MapsUtil в таком методе, как этот
private void initRun(){
mCardViewData.setVisibility(View.VISIBLE);
}
Мне нужно привязать это представление карты к своему рабочему классу, потому что я управляю логикойкарта в рабочем классе, а не в представлении.
Теперь я получаю следующее
java.lang.NullPointerException: попытка вызвать виртуальный метод void androidx.cardview.widget.CardView.setVisibility (int) 'для ссылки на нулевой объект в com.utils.MapsUtils.initRun (MapsUtils.java:182) в com.utils.MapsUtils.access $ 000 (MapsUtils.java:62) в com.utils.MapsUtils $ 2.onClick (MapsUtils.java:219)
Точки ошибок здесь
mCardViewData.setVisibility (View.VISIBLE);
И я пытаюсь понять, где я не прав, потому что ятолько что определил все, что мне нужно для работы с этим CardView в моем рабочем классе