AutoCompleteTextView выдает ошибку "on a null object reference" при любом исходе
Все перерыл. Даже не знаю в чем проблема. Пробовал не во фрагменте, а просто в чистом проекте. Все равно вылетает эта ошибка. Помогите плиз.
XML код
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
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="match_parent"
tools:context=".ui.gallery.GalleryFragment">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" >
<AutoCompleteTextView
android:id="@+id/brandActv"
android:layout_width="0sp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:clickable="true"
android:dropDownAnchor="@+id/button_change"
android:focusable="true"
android:gravity="center"
android:hint="@string/brands"
android:textColor="#a39d9d" />
</TableRow>
</TableLayout>
Далее код:
package com.example.bull_application_01.ui.gallery;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import com.example.bull_application_01.MainActivity;
import com.example.bull_application_01.R;
import com.example.bull_application_01.databinding.FragmentGalleryBinding;
import com.example.bull_application_01.ui.home.HomeFragment;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
public class GalleryFragment extends Fragment {
AutoCompleteTextView brandActv;
ArrayAdapter<String> adapter;
TextView testTV;
final String[] mCats = { "Мурзик", "Рыжик", "Барсик", "Борис",
"Мурзилка", "Мурка" };
private GalleryViewModel galleryViewModel;
private FragmentGalleryBinding binding;
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
galleryViewModel = new ViewModelProvider(this).get(GalleryViewModel.class);
View root = inflater.inflate(R.layout.fragment_gallery, null);
testTV = root.findViewById(R.id.testedTV);
brandActv = (AutoCompleteTextView) root.findViewById(R.id.brandActv);
adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, mCats);
brandActv.setThreshold(1); //will start working from first character
brandActv.setAdapter(adapter);
return root;
}
// @Override
public void onDestroyView() {
super.onDestroyView();
binding = null;
}
}
Ответы (1 шт):
Автор решения: Гасайни Рабаданов
→ Ссылка
Оказывается ошибка была в XML коде. нужно убрать: android:dropDownAnchor="@+id/button_change"
и все будет работать!