title: App crashes on attaching fetched data from Retrofit to adapter
body: <p>I am working with Retrofit. I followed a tutorial and did exactly what he said. Response is working perfectly in a single TextView but not in RecyclerView. When I added an adapter class, it start crashing. I read logcat file too but didn't seem helpful. The app crashes without showing any error or exception.</p>
  <p>Here is my main XML class with a RecyclerView.</p>
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
      xmlns:tools=&quot;http://schemas.android.com/tools&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      tools:context=&quot;.MainActivity&quot;
      android:orientation=&quot;vertical&quot;&gt;
  
  
  &lt;!--    &lt;ScrollView--&gt;
  &lt;!--        android:layout_width=&quot;match_parent&quot;--&gt;
  &lt;!--        android:layout_height=&quot;match_parent&quot;&gt;--&gt;
  &lt;!--        &lt;TextView--&gt;
  &lt;!--            android:layout_width=&quot;match_parent&quot;--&gt;
  &lt;!--            android:layout_height=&quot;wrap_content&quot;--&gt;
  &lt;!--            android:id=&quot;@+id/text1&quot;--&gt;
  &lt;!--            android:layout_marginTop=&quot;5dp&quot;--&gt;
  &lt;!--            android:layout_marginStart=&quot;2dp&quot;&gt;--&gt;
  
  &lt;!--        &lt;/TextView&gt;--&gt;
  
  &lt;!--    &lt;/ScrollView&gt;--&gt;
  
  
      &lt;androidx.recyclerview.widget.RecyclerView
          android:id=&quot;@+id/recview&quot;
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;match_parent&quot;
          app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
          app:layout_constraintEnd_toEndOf=&quot;parent&quot;
          app:layout_constraintStart_toStartOf=&quot;parent&quot;
          app:layout_constraintTop_toTopOf=&quot;parent&quot;&gt;
  
  
      &lt;/androidx.recyclerview.widget.RecyclerView&gt;
  
  
  &lt;/LinearLayout&gt;
  
  </code></pre>
  <p>My main class is working fine with a for loop to print data in one TextView but not for adapter or RecyclerView.</p>
  <pre class="lang-java prettyprint-override"><code>package com.example.apipractice;
  
  import androidx.appcompat.app.AppCompatActivity;
  import androidx.recyclerview.widget.LinearLayoutManager;
  import androidx.recyclerview.widget.RecyclerView;
  
  import android.os.Bundle;
  import android.widget.TextView;
  import android.widget.Toast;
  
  import java.util.ArrayList;
  import java.util.List;
  
  import retrofit2.Call;
  import retrofit2.Callback;
  import retrofit2.Response;
  import retrofit2.Retrofit;
  
  public class MainActivity extends AppCompatActivity {
       apiInterface instanceforAPI;
       LinearLayoutManager linearLayoutManager;
   //TextView txt;
       RecyclerView recview;
      adapter ad;
      List&lt;jsonData&gt; data = new ArrayList&lt;&gt;();
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
   //txt = findViewById(R.id.text1);
          recview = findViewById(R.id.recview);
          linearLayoutManager = new LinearLayoutManager(this);
          recview.setLayoutManager(linearLayoutManager);
  
          instanceforAPI = retrofitInstance.getRetrofit().create(apiInterface.class);
          instanceforAPI.getdata().enqueue(new Callback&lt;List&lt;jsonData&gt;&gt;() {
              @Override
              public void onResponse(Call&lt;List&lt;jsonData&gt;&gt; call, Response&lt;List&lt;jsonData&gt;&gt; response) {
                  if(response.body().size()&gt;0){
                      Toast.makeText(MainActivity.this,&quot;yayy&quot;,Toast.LENGTH_LONG).show();
                      data.addAll(response.body());
  
                      ad = new adapter(data);
                      ad.notifyDataSetChanged();
                      recview.setAdapter(ad);
  //                        for (int index=0;index&lt;data.size();index++){
  //                  txt.setText(txt.getText()+ &quot; SRNo.: &quot;+data.get(index).getTitle()+&quot; \n&quot; + data.size());
  //
  //                  }
  
                  }
                  else
                  {
                      Toast.makeText(MainActivity.this,&quot;No yayy&quot;,Toast.LENGTH_LONG).show();
                  }
  
  
              }
  
              @Override
              public void onFailure(Call&lt;List&lt;jsonData&gt;&gt; call, Throwable t) {
                  Toast.makeText(MainActivity.this,t.getMessage(),Toast.LENGTH_LONG).show();
              }
          });
  
  
      }
  }
  </code></pre>
  <p>SingleRow design XML class (single.xml) is as follows:</p>
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;com.google.android.material.card.MaterialCardView xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;wrap_content&quot;
      android:orientation=&quot;vertical&quot;
      android:rotationX=&quot;8&quot;&gt;
  
      &lt;LinearLayout
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;match_parent&quot;
          android:orientation=&quot;vertical&quot;&gt;
  
          &lt;TextView
              android:id=&quot;@+id/t1&quot;
              android:layout_width=&quot;wrap_content&quot;
              android:layout_height=&quot;wrap_content&quot;
              android:text=&quot;Name&quot;
  
              &gt;&lt;/TextView&gt;
  
          &lt;TextView
              android:id=&quot;@+id/t2&quot;
              android:layout_width=&quot;wrap_content&quot;
              android:layout_height=&quot;wrap_content&quot;
              android:text=&quot;title&quot;
  
              &gt;&lt;/TextView&gt;
  
  
      &lt;/LinearLayout&gt;
  
  &lt;/com.google.android.material.card.MaterialCardView&gt;
  
  </code></pre>
  <p>Adapter class (adapter.java) for fetching data is as follows:</p>
  <pre class="lang-java prettyprint-override"><code>package com.example.apipractice;
  
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.AdapterView;
  import android.widget.TextView;
  
  import androidx.annotation.NonNull;
  import androidx.recyclerview.widget.RecyclerView;
  
  import java.util.List;
  
  public class adapter extends RecyclerView.Adapter&lt;adapter.myviewholder&gt; {
  
      List&lt;jsonData&gt; data;
  
      public  adapter(List&lt;jsonData&gt; data){
          this.data = data;
      }
      @Override
      public myviewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  
      View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.single,parent,false    );
      return new myviewholder(v);
      }
  
      @Override
      public void onBindViewHolder(@NonNull adapter.myviewholder holder, int position) {
           holder.t1.setText((int) data.get(position).getId());
          holder.t2.setText( data.get(position).getTitle());
  
      }
  
      @Override
      public int getItemCount() {
          return data.size();
      }
  
      public class myviewholder extends RecyclerView.ViewHolder
      {
          TextView t1,t2;
  
  
          public myviewholder(@NonNull View itemView) {
              super(itemView);
              t1= itemView.findViewById(R.id.t1);
              t2 = itemView.findViewById(R.id.t2);
          }
  
      }
  }
  </code></pre>
  <p>The following class is for retrofit instance, retrofitInstance.java:</p>
  <pre class="lang-java prettyprint-override"><code>
  package com.example.apipractice;
  
  import retrofit2.Retrofit;
  import retrofit2.converter.gson.GsonConverterFactory;
  
  public class retrofitInstance {
  
      private static Retrofit retrofit;
      private  static final String commonURL = &quot;https://jsonplaceholder.typicode.com&quot;;
  
      public static Retrofit getRetrofit() {
          if(retrofit ==null) {
              retrofit = new Retrofit.Builder().baseUrl(commonURL).addConverterFactory(GsonConverterFactory.create()).build();
          }
          return retrofit;
      }
  }
  </code></pre>
  <p>This interface class (apiInterfce.java) is for the API get call:</p>
  <pre class="lang-java prettyprint-override"><code>
  
  package com.example.apipractice;
  
  import java.util.List;
  
  import retrofit2.Call;
  import retrofit2.http.GET;
  
  public interface apiInterface {
      @GET(&quot;/posts?type=json&quot;)
      Call&lt;List&lt;jsonData&gt;&gt; getdata();
  }
  </code></pre>
  <p>My model class, jsonData.java:</p>
  <pre class="lang-java prettyprint-override"><code>package com.example.apipractice;
  
  public class jsonData {
  
      private float userId;
      private float id;
      private String title;
      private String body;
  
  
      // Getter Methods
  
      public float getUserId() {
          return userId;
      }
  
      public float getId() {
          return id;
      }
  
      public String getTitle() {
          return title;
      }
  
      public String getBody() {
          return body;
      }
  
      // Setter Methods
  
      public void setUserId(float userId) {
          this.userId = userId;
      }
  
      public void setId(float id) {
          this.id = id;
      }
  
      public void setTitle(String title) {
          this.title = title;
      }
  
      public void setBody(String body) {
          this.body = body;
      }
  }
  </code></pre>