Почему фрагмент не добавляется при реализации интерфейса для связи в Tablayout - PullRequest
1 голос
/ 31 октября 2019

Я использовал tabLayout, в котором есть фрагменты. Когда я вызываю Фрагмент, используя интерфейс из действия.

Я не могу получить доступ к экземпляру Listview и getActivity (), которые тоже выдают нулевую ссылку, и я пытался

тестировать, используя метод isAdded, сам фрагментне добавляется при вызове из интерфейса.

PrescriptionHistoryFragment вызывается из PrescriptionActivity

 ListView listView;

CustomAdapter adapter;
@Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View retView = inflater.inflate(R.layout.fragment_history, container, false);



        if (retView != null) {
            listView = retView.findViewById(R.id.listView); 

            adapter = new CustomAdapter;

        }


        return retView;




    }
InterFace implemented in Fragment. Cant access here the listview and also not 

добавлено в действие

@Override
public void onInputPatient(int patientID) {


    Log.d("Patient", "onInputPatient: Recieved" + patientID );

    if(isAdded()){

        Log.d("Tag", "onInputPatient:Attached ");
    }else {
        Log.d("Tag", "onInputPatient: NotAdded"); // called
    }

 //accessing listview here also produces null reference error

   listview.setAdapter(adapter);
}

XML-файл, используемый во фрагменте

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".Activities.Activities.PrescriptionActivity">

    <LinearLayout
        android:id="@+id/history_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:context=".Activities.Activities.PrescriptionActivity"
        >



        <ListView
            android:id="@+id/listView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="5dp"
            android:visibility="visible"
            android:nestedScrollingEnabled="true"
            />
    </LinearLayout>
</LinearLayout>

Активность

private void setupViewPager(ViewPager viewPager) {
         adapter = new ViewPagerAdapter(getSupportFragmentManager());
        PrescriptionFragment prescriptionFragment = new PrescriptionFragment();

prescription_historyFragment prescriptionFragment = new prescription_historyFragment ();

        adapter.addFragment(prescriptionFragment, "PRESCRIPTION");

        adapter.addFragment(prescription_historyFragment, "HISTORY");
        viewPager.setAdapter(adapter);
    }

    class ViewPagerAdapter extends FragmentPagerAdapter {
        private  List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

        public ViewPagerAdapter(FragmentManager manager) {
            super(manager);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

        public void addFragment(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }


        public void removeFragment(Fragment fragment, int position) {
            mFragmentList.remove(position);
            mFragmentTitleList.remove(position);
        }


    }

@Override
    public void onInputPatient(int patientID) {


    prescription_historyFragment.onInputPatient(patientID);



    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...