0

Here is my Layout (fragment_hadees.xml) :

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Hadees">


    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:id="@+id/myrecyclerview"
        android:layout_height="match_parent"/>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/fab"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:elevation="40dp"
        android:layout_margin="30dp"
       android:backgroundTint="#ffffff"
        android:src="@drawable/favorited"/>


</RelativeLayout>

Here is the Hadees.java (root java file) :

   @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    View root = inflater.inflate(R.layout.fragment_hadees, container, false);
   ((MainActivity) getActivity()).getSupportActionBar().setTitle("Hadees");

   HadeesAdapter hadeesAdapter;
   fab= root.findViewById(R.id.fab);
    ImageView copybutton= root.findViewById(R.id.copybutton);
    final TextView textView=root.findViewById(R.id.textView);
    ImageView whatsapText=root.findViewById(R.id.whatsApp_txt);


    toggleModelList= new ArrayList<>();

    fab.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View view) {
          Toast.makeText(getActivity(), "FAB", Toast.LENGTH_SHORT).show();


       }
    });


    myrecyclerview=(RecyclerView)root.findViewById(R.id.myrecyclerview);
    myrecyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));

Here is my hadees_contents.xml:

 <LinearLayout
    android:id="@+id/l1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_margin="10dp"
        android:textIsSelectable="true"
        android:textColor="#000000"
        android:gravity="center"
        android:maxLines="100"
        android:fontFamily="sans-serif-condensed-medium"
        android:text="This is Arabic Text"
        android:textSize="20sp" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginTop="2dp"
        android:layout_marginBottom="2dp"
        android:background="@android:color/darker_gray" />

    <LinearLayout
        android:id="@+id/l2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="6dp"
        android:layout_marginBottom="6dp"
        android:weightSum="4">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/copybutton"
            android:src="@drawable/ic_content_copy_black_24dp" />

How can I get Access to the the Views in the hadees_contents.xml in Hadees.java ? I have the access to FAB but not to the contents of Recycler's View inflated layout ,which is hadees_contents.xml

Zibran
  • 81
  • 2
  • 14

2 Answers2

1

You're trying to access the fields of hadees_contents from Hadees.java which doesn't inflate the xml hadees_contents. It inflates the xml fragment_hadees which is why you will get a null pointer exception if you try to access hadees_contents from Hadees.java. I can think of a way to solve this but I don't know if its practical in your situation. You must have an activity which adds the fields of hadees_contents into the recyclerview, you can send an intent from that addActivity which inflates hadees_contents to your Hadees.java class and receive it when you call the OnActivityResult method from Hadees.java.

Thaer97
  • 13
  • 3
1

By calling the following method, no instances of hadees_contents.xml are inflated. It only inflates the RecyclerView itself which is an empty ViewGroup.

inflater.inflate(R.layout.fragment_hadees, container, false);

RecyclerViews need an adapter to tell them what to inflate and what data to bind to. Please refer to Simple Android RecyclerView example for further information and an example.

frogatto
  • 28,539
  • 11
  • 83
  • 129