-1

Voici le code de mon application où je tente d'obtenir la liste des sites de l'API Foursquare et l'affichage dans un recyclerviewObtenir nulle Erreur d'objet pour une vue à l'intérieur d'un adaptateur

searchRetrofitAcitivity.java

public class searchRetrofitActivity extends AppCompatActivity { 

    private static final String TAG = searchRetrofitActivity.class.getSimpleName(); 

    // TODO - insert your foursquare OAuth KEY here 
    private final static String OAUTH_TOKEN = "L2GTUPPPWMEZ2HUT5D4HHYOOY5YNNNE1UVMJCZAOOFI2NNG3"; 
    private static final String LIMIT_VENUE_RESULT = "5"; 
    String latitudeLongitude = "40.7,-74"; 


    ArrayList<Venue> venues = new ArrayList<Venue>(); 
    String currentDate = searchRetrofitActivity.getCurrentDate(); 
    private RecyclerView recyclerView; 


    public static String getCurrentDate() { 
     Calendar calendar = Calendar.getInstance(); 
     SimpleDateFormat mdformat = new SimpleDateFormat("yyyyMMdd"); 
     String formattedDate = mdformat.format(calendar.getTime()); 
     return formattedDate; 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_search_retrofit); 

     final VenuesAdapter vAdapter = new VenuesAdapter(venues, R.layout.list_venue, getApplicationContext()); 

     //method call to get data from api 
     recyclerView = (RecyclerView) findViewById(R.id.venues_recycler_view); 
     recyclerView.setLayoutManager(new LinearLayoutManager(this)); 
     recyclerView.setAdapter(vAdapter); 


     ApiInterface apiService = 
       FourSquareClient.getClient().create(ApiInterface.class); 


     Call<VenueResponses> call = apiService.getExploredVenues(latitudeLongitude, OAUTH_TOKEN, currentDate); 
     call.enqueue(new Callback<VenueResponses>() { 
      @Override 
      public void onResponse(Call<VenueResponses> call, Response<VenueResponses> response) { 
       int statusCode = response.code(); 
       List<Item_> items; 

       Venue tempVenue; 
       items = response.body().getResponse().getGroups().get(0).getItems(); 


       for (int j = 0; j < items.size(); j++) { 
        tempVenue = new Venue(); 
        tempVenue = items.get(j).getVenue(); 
        venues.add(tempVenue); 
       } 
       recyclerView.setAdapter(new VenuesAdapter(venues, R.layout.list_venue, getApplicationContext())); 
       Log.d(TAG, "No Of venues added " + venues.size()); 
      } 

      @Override 
      public void onFailure(Call<VenueResponses> call, Throwable t) { 
       // Log error here since request failed 
       Log.e(TAG, t.toString()); 
      } 
     }); 
    } 
} 

VenuesAdapter.java

public class VenuesAdapter extends RecyclerView.Adapter<VenuesAdapter.VenueViewHolder> { 

    private ArrayList<Venue> venues; 
    private int rowLayout; 
    private Context context; 

    public VenuesAdapter(ArrayList<Venue> venues, int rowLayout, Context context) { 
     this.venues = venues; 
     this.rowLayout = rowLayout; 
     this.context = context; 
    } 

    public static class VenueViewHolder extends RecyclerView.ViewHolder { 
     LinearLayout venuesLayout; 
     TextView venueName; 
     TextView venueRating; 
     TextView venueCategory; 
     TextView venuePrice; 
     TextView venueDistance; 
     TextView venueAddress; 

     public VenueViewHolder(View view) { 
      super(view); 
      venuesLayout = (LinearLayout) view.findViewById(R.id.venue_item); 
      venueName = (TextView) view.findViewById(R.id.tv_venue_name); 
      venueRating = (TextView) view.findViewById(R.id.tv_rating); 
      venueCategory = (TextView) view.findViewById(R.id.tv_category); 
      venuePrice = (TextView) view.findViewById(R.id.tv_price); 
      venueDistance = (TextView) view.findViewById(R.id.tv_distance); 
      venueAddress = (TextView) view.findViewById(R.id.tv_address); 

     } 
    } 


    @Override 
    public VenueViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View view = LayoutInflater.from(parent.getContext()).inflate(rowLayout,parent,false); 
     return new VenueViewHolder(view); 
    } 

    @Override 
    public void onBindViewHolder(VenueViewHolder holder, int position) { 

     holder.venueName.setText(venues.get(position).getName()); 
     // holder.venueRating.setText(venues.get(position).get); 

    } 

    @Override 
    public int getItemCount() { 
     return venues.size(); 
    } 
} 

activity_search_retrofit.xml

<android.support.v7.widget.RecyclerView 
    android:id="@+id/venues_recycler_view" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:scrollbars="vertical" /> 

list_venue.xml

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="6dp"> 

    <ImageView 
     android:id="@+id/image_view" 
     android:layout_width="113dp" 
     android:layout_height="113dp" /> 

    <TextView 
     android:id="@+id/venue_name" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_marginLeft="10dp" 
     android:layout_marginTop="10dp" 
     android:layout_toRightOf="@id/image_view" 
     android:text="venue name" 
     android:textColor="#1D1D26" 
     android:textSize="16.88sp" /> 

    <TextView 
     android:id="@+id/tv_rating" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/venue_name" 
     android:layout_marginLeft="10dp" 
     android:layout_marginTop="30dp" 
     android:layout_toRightOf="@id/image_view" 
     android:padding="2dp" 
     android:text="10.0" 
     android:textSize="11.25dp" 
     tools:background="#58FA58" /> 

    <TextView 
     android:id="@+id/tv_category" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/tv_rating" 
     android:layout_marginLeft="10dp" 
     android:layout_marginTop="6dp" 
     android:layout_toRightOf="@id/image_view" 
     android:text="category" 
     android:textColor="#000000" 
     android:textSize="12.38sp" /> 

    <TextView 
     android:id="@+id/tv_price" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/tv_rating" 
     android:layout_marginLeft="10dp" 
     android:layout_marginTop="6dp" 
     android:layout_toRightOf="@id/tv_category" 
     android:text="price" 
     android:textColor="#000000" 
     android:textSize="12.38sp" /> 

    <TextView 
     android:id="@+id/tv_distance" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/tv_rating" 
     android:layout_marginLeft="10dp" 
     android:layout_marginTop="6dp" 
     android:layout_toRightOf="@id/tv_price" 
     android:text="distance" 
     android:textColor="#000000" 
     android:textSize="12.38sp" /> 

    <TextView 
     android:id="@+id/tv_address" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/tv_category" 
     android:layout_marginLeft="10dp" 
     android:layout_marginTop="6dp" 
     android:layout_toRightOf="@id/image_view" 
     android:textColor="#000000" 
     android:textSize="12.38sp" 
     tools:text="Enter the adress" /> 

    <Button 
     android:layout_width="20dp" 
     android:layout_height="20dp" 
     android:layout_alignParentRight="true" 
     android:layout_marginRight="10dp" 
     android:layout_marginTop="10dp" 
     tools:background="@drawable/close_icon_grey_hdpi" /> 
</RelativeLayout> 

App se est écrasé avec l'erreur suivante

05-20 12:31:55.213 2679-2679/com.example.android.foursquare_pammu 
    E/AndroidRuntime: FATAL EXCEPTION: main 
    Process: com.example.android.foursquare_pammu, PID: 2679 
    java.lang.NullPointerException: Attempt to invoke virtual method 
    'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference 
    at com.example.android.foursquare_pammu.adapter.VenuesAdapter.onBindViewHold 
    er(VenuesAdapter.java:66) 

Répondre

1

Vous devez changer

venueName = (TextView) view.findViewById(R.id.tv_venue_name); 

à

venueName = (TextView) view.findViewById(R.id.venue_name); 

Votre TextView id est venue_name pas tv_venue_name

+0

merci bro u got it. –

0

Venu Voir article ne pas LinearLayout avoir changé RelativeLayout:

 venuesLayout = (LinearLayout) view.findViewById(R.id.venue_item); 

Pour

RelativeLayout venuesLayout; 

     venuesLayout = (RelativeLayout) view.findViewById(R.id.venue_item); 

intérieur Voir classe Titulaire

ajouter l'ID à votre disposition de racine d'élément parent en XML:

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
     android:id="@+id/venue_item" 
     android:layout_marginTop="6dp">