2017-03-31 1 views
-4

Ich versuche die Blog-ID zu verwenden, um ein JSON-Objekt vom Server zu bekommen . Im Moment bin ich immer diese Fehlermeldungjava.lang.NullPointerException: Versuch, die Interface-Methode 'java.util.Iterator java.util.List.iterator()' für eine Nullobjekt-Referenz aufzurufen. Retrofit

java.lang.NullPointerException: Versuch Interface-Methode ‚java.util.Iterator java.util.List.iterator()

auf null aufzurufen Objektreferenz.Wie verwende ich die Post-ID, um den vollen Inhalt zu erhalten, was mache ich falsch? Bitte helfen !!! Adapter

MyBlog:

public class MyBlogAdapter extends RecyclerView.Adapter<BlogViewHolder> { 
    List<BlogResponse> postsList; 
    Context context; 
    public static String blog_Id, blogID; 
    private LayoutInflater inflater; 

    public MyBlogAdapter(Context context, List<BlogResponse> postsList){ 
     this.context = context; 
     this.inflater = LayoutInflater.from(context); 
     this.postsList = postsList; 
    } 

    @Override 
    public BlogViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View view = inflater.inflate(R.layout.custom_view,parent, false); 
     return new BlogViewHolder(view); 
    } 

    @Override 
    public void onBindViewHolder(BlogViewHolder holder, int position) { 
     final BlogResponse posts= postsList.get(position); 
     holder.summary.setText(posts.getBlogExcerpt().trim().toString()); 
     holder.title.setText(posts.getBlogTitle().trim().toString()); 
     // Glide.with(context).load(posts.getBlogThumbnail()).into(holder.cover); 

     holder.blogHolder.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Intent intent = new Intent(context, AnotherSingleView.class); 
       blog_Id = posts.getBlogId(); 
       intent.putExtra(blogID,blog_Id); 
       Log.d("MyblogAdapter","Please check blog Id: "+blog_Id); 

       context.startActivity(intent); 
      } 
     }); 
    } 

    @Override 
    public int getItemCount() { 
     Log.d("MyBlogAdapter,","getItemCount"+postsList.size()); 

     return postsList == null ? (0) : postsList.size(); 
    } 
} 

SecondActivity:

public class AnotherSingleView extends AppCompatActivity { 
    String postID; 
    int position; 
    public TextView blogTitle,blogSub,blogContent; 
    public ImageView blogPic; 
    List<SingleBlogPost> singleBlogPosts; 
    SingleBlogPost singleBlogPost; 

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


     Intent intent = getIntent(); 
     Bundle showBlogId = intent.getExtras(); 
     postID = showBlogId.getString(blogID); 
     Log.d("AnotherSingleView","Please check blog Id: "+postID); 

     singleBlogPosts = new ArrayList<>(); 

     blogContent = (TextView)findViewById(R.id.blog_content); 
     blogSub = (TextView) findViewById(R.id.blog_subtitle); 
     blogTitle =(TextView) findViewById(R.id.blog_title); 
     blogPic =(ImageView) findViewById(R.id.blog_pix); 
     singlePostDisplay(); 

    } 

private void singlePostDisplay() { 
    BlogaPI api = ApiClient.getBlogInterface(); 
    Call<List<SingleBlogPost>> call = api.postResponse(postID); 
    call.enqueue(new Callback<List<SingleBlogPost>>() { 
     @Override 
     public void onResponse(Call<List<SingleBlogPost>> call, Response<List<SingleBlogPost>> response) { 
      singleBlogPosts = response.body(); 
      if (singleBlogPosts != null && !singleBlogPosts.isEmpty()){ 
       for (SingleBlogPost posts : singleBlogPosts){ 
        Log.d("AnotherSingleView","Please check RESPONSE: "+response.body().toString()); 
        blogTitle.setText(posts.getBlogTitle()); 
        blogSub.setText(posts.getBlogSubtitle()); 
        blogContent.setText(posts.getBlogContent()); 
        // Glide.with(AnotherSingleView.this).load(singlepost.getBlogMedimg()).into(blogPic); 
       } 
      } 
      else{ 
       Toast.makeText(AnotherSingleView.this, "Something is empty", Toast.LENGTH_SHORT).show(); 
      }   } 

      @Override 
      public void onFailure(Call<List<SingleBlogPost>> call, Throwable t) { 
       Toast.makeText(AnotherSingleView.this, "check again: "+t.toString(), Toast.LENGTH_SHORT).show(); 
      } 
     }); 
    } 
} 

Schnittstelle:

public interface BlogaPI { 
    @GET("blog") 
    Call<BlogList> response(); 

    @GET("post/{blog_id}") 
    Call<List<SingleBlogPost>> postResponse(@Path("blog_id") String blog_id); 
    //Call<List<SingleBlogPost>> postResponse(); 
} 

SingleBl ogPost:

public class SingleBlogPost { 

@SerializedName("blog_id") 
@Expose 
private String blogId; 
@SerializedName("blog_title") 
@Expose 
private String blogTitle; 
@SerializedName("blog_subtitle") 
@Expose 
private String blogSubtitle; 
@SerializedName("blog_excerpt") 
@Expose 
private String blogExcerpt; 
@SerializedName("blog_content") 
@Expose 
private String blogContent; 
@SerializedName("blog_thumbnail") 
@Expose 
private String blogThumbnail; 
@SerializedName("blog_medimg") 
@Expose 
private String blogMedimg; 
@SerializedName("category_title") 
@Expose 
private String categoryTitle; 
public SingleBlogPost(String blogId,String blogTitle, String blogSubtitle, String blogExcerpt, 
         String blogContent, String blogThumbnail, String blogMedimg, String categoryTitle){ 
    this.blogId = blogId; 
    this.blogTitle = blogTitle; 
    this.blogSubtitle = blogSubtitle; 
    this.blogExcerpt = blogExcerpt; 
    this.blogContent = blogContent; 
    this.blogThumbnail = blogThumbnail; 
    this.blogMedimg = blogMedimg; 
    this.categoryTitle = categoryTitle; 
} 

public String getBlogId() { 
    return blogId; 
} 

public void setBlogId(String blogId) { 
    this.blogId = blogId; 
} 

public String getBlogTitle() { 
    return blogTitle; 
} 

public void setBlogTitle(String blogTitle) { 
    this.blogTitle = blogTitle; 
} 

public String getBlogSubtitle() { 
    return blogSubtitle; 
} 

public void setBlogSubtitle(String blogSubtitle) { 
    this.blogSubtitle = blogSubtitle; 
} 

public String getBlogExcerpt() { 
    return blogExcerpt; 
} 

public void setBlogExcerpt(String blogExcerpt) { 
    this.blogExcerpt = blogExcerpt; 
} 

public String getBlogContent() { 
    return blogContent; 
} 

public void setBlogContent(String blogContent) { 
    this.blogContent = blogContent; 
} 

public String getBlogThumbnail() { 
    return blogThumbnail; 
} 

public void setBlogThumbnail(String blogThumbnail) { 
    this.blogThumbnail = blogThumbnail; 
} 

public String getBlogMedimg() { 
    return blogMedimg; 
} 

public void setBlogMedimg(String blogMedimg) { 
    this.blogMedimg = blogMedimg; 
} 

public String getCategoryTitle() { 
    return categoryTitle; 
} 

public void setCategoryTitle(String categoryTitle) { 
    this.categoryTitle = categoryTitle; 
} 

} 
+2

Kann nicht ohne ein vollständiges Logcat sicher sagen, aber da der einzige Iterator, den ich in Ihrem Code finden kann '' ist (SingleBlogPost posts: singleBlogPosts) ', würde ich sagen,' singleBlogPosts' ist 'null'. Überprüfen Sie, dass 'response.body()' nicht leer ist. –

+3

Mögliche Duplikate von [Was ist eine NullPointerException und wie behebe ich sie?] (Http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) –

Antwort

1

Überprüfen Sie, ob der Serverantwort unterscheidet sich dann null, bevor Sie eine enhanced for wie folgt tun:

if(singleBlogPosts!= null) { 
    for (SingleBlogPosts posts : singleBlogPosts){ 
     Log.d("AnotherSingleView","Please check RESPONSE: "+response.body().toString()); 
     blogTitle.setText(posts.getBlogTitle()); 
     blogSub.setText(posts.getBlogSubtitle()); 
     blogContent.setText(posts.getBlogContent()); 
     // Glide.with(AnotherSingleView.this).load(singlepost.getBlogMedimg()).into(blogPic); 
    } 
} 
+0

Ändern Sie Ihre 'if' Anweisungen, um' singleBlogPost' (Kleinbuchstaben) zu verwenden. Sie möchten auf die Instanz verweisen, nicht auf die Klasse. –

+0

Danke @MichaelDodd! –

+0

Immer noch nicht ganz richtig. Kann momentan keine Bearbeitung vorschlagen, da die Warteschlange voll ist, aber es sollte 'if (singleBlogPosts! = Null &&! SingleBlogPosts.isEmpty()) {' gefolgt von 'for (SingleBlogPost posts: singleBlogPosts)' –

-2

Sie Frage war Liste Iterator, aber wir `t sehen Sie überall dort verwenden, auf Liste , ich schätze, Sie können RecyclerView auf Sie verwenden Daten nicht erhalten, weil Daten erhalten asynchronous.try Daten zu verwenden, wenn Sie sicher sein können, dass es vorhanden ist.

+0

'iterator' verweist in diesem Fall auf das Durchlaufen eines' List'-Objekts, nicht eines 'ListView'. Siehe Luiz's Antwort. –

+0

Ich fügte hinzu, 'if (singleBlogPosts = null && singleBlogPosts.isEmpty()!) { für (SingleBlogPost Beiträge: singleBlogPosts) { } } else { Toast.makeText (AnotherSingleView.this: "Etwas ist leer" , Toast.LENGTH_SHORT) .show(); } und bekomme die else-Anweisung. Was könnte falsch sein? –

+0

@ekennechilex Besser, dies als Kommentar zu Ihrer ursprünglichen Frage zu posten. Das bedeutet, dass Sie eine leere oder leere Antwort erhalten, d. H. Ihre Anfrage schlägt fehl. –

Verwandte Themen