-4

this is my fragment '''java package com.example.healthapp.ui.profile;

import android.database.Cursor;
import android.os.Bundle;
import android.os.RecoverySystem;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import com.example.healthapp.CustomAdapter;
import com.example.healthapp.MyDatabase;
import com.example.healthapp.R;
import com.example.healthapp.databinding.FragmentSettingsBinding;

import java.util.ArrayList;
import java.util.Objects;

public class ProfileFragment extends Fragment {

    private ProfileViewModel profileViewModel;
    private FragmentSettingsBinding binding;

    public MyDatabase myDB;
    public ArrayList<String> user_id, user_firstName, user_lastName, user_email, user_phone_number, user_age, user_weight;
    public CustomAdapter customAdapter;

    private static final String TAG = "Testing";


    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        profileViewModel =
                new ViewModelProvider(this).get(ProfileViewModel.class);
        //View view = inflater.inflate(R.layout.fragment_profile);
        binding = FragmentSettingsBinding.inflate(inflater, container, false);

        View root = binding.getRoot();

        //getActivity().setContentView();

        Log.i("Root Info: ", root.toString());
        RecyclerView recyclerView = root.findViewById(R.id.recyclerview);

        myDB = new MyDatabase(getActivity());
        user_id = new ArrayList<>();
        user_firstName = new ArrayList<>();
        user_lastName = new ArrayList<>();
        user_email = new ArrayList<>();
        user_phone_number = new ArrayList<>();
        user_age = new ArrayList<>();
        user_weight = new ArrayList<>();

        storeDataInArrays();

        customAdapter = new CustomAdapter(ProfileFragment.this, getActivity(), user_id, user_firstName, user_lastName, user_email,
                        user_phone_number , user_age, user_weight);
        try {
            recyclerView.setAdapter(customAdapter);
            recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        } catch (Exception e) {
            Log.e("Error setting Adapter", "Custom Adapter null: " +
                    (customAdapter == null ? "true" : "false") +
                    ", RecyclerView Null: " + (recyclerView == null ? "true" : "false"), e);
        }



        final TextView textView = binding.textFragment;
        profileViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
                textView.setText(s);
            }
        });
        return root;
    }

    public void storeDataInArrays(){
        Cursor cursor = myDB.readAllData();
        if(cursor.getCount() == 0){
            Toast.makeText(getActivity(), "No data" , Toast.LENGTH_SHORT).show();
            Log.i(TAG, "No Data");
        }else{
            while (cursor.moveToNext()){
                user_id.add(cursor.getString(0));
                user_firstName.add(cursor.getString(1));
                user_lastName.add(cursor.getString(2));
                user_email.add(cursor.getString(3));
                user_phone_number.add(cursor.getString(4));
                user_age.add(cursor.getString(5));
                user_weight.add(cursor.getString(6));
                Log.i(TAG, user_firstName + " " + user_lastName);
            }
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}

'''

fragmant_profile.xml

'''xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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.profile.ProfileFragment">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/RecyclerView"
        android:layout_width="409dp"
        android:layout_height="729dp"
        android:layout_marginTop="1dp"
        android:layout_marginBottom="1dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0" />


</androidx.constraintlayout.widget.ConstraintLayout>

'''

error code E/Error setting Adapter: Custom Adapter null: false, RecyclerView Null: true java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setAdapter(androidx.recyclerview.widget.RecyclerView$Adapter)' on a null object reference at com.example.healthapp.ui.profile.ProfileFragment.onCreateView(ProfileFragment.java:71) at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2963) at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:518) at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282) at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189) at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2106) at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:2002) at androidx.fragment.app.FragmentManager$5.run(FragmentManager.java:524) at android.os.Handler.handleCallback(Handler.java:938) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:223) at android.app.ActivityThread.main(ActivityThread.java:7656) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) **

  • 1
    what's the point of using viewbinding and then also using findViewById ? if it's not being detected by your binding then you need to check why – a_local_nobody Oct 05 '21 at 09:07

1 Answers1

-1

If you use Viewbinding (Line:48) then you should not use findViewById (Line:55) You should add your RecyclerView like:

RecyclerView recyclerView = this.binding.recyclerView

If your RecyclerView isn't in the layout file or has an other android:id the IDE will show an error ;)

John Doe
  • 123
  • 1
  • 2
  • 12
  • it cant find the recycleview but i think that has to do something with the viewmodel because it worked with a normal activity i added the code of the xml to the question – Benjamin Venema Oct 05 '21 at 10:12
  • Your Bindingclass has the Name FragmentSettingBinding your Layout has the name fragment_profile.xml. This should be FragmentProfileBinding – John Doe Oct 05 '21 at 10:47