2017-05-19 9 views
0

임 : 나는이 선 "Log.i에 NullPointerException이 발생 제품명을 가져 할 할 ApolloCall.CallBack의 OnResponse 방법에안드로이드 ApolloCall.CallBack onResponse 인출되지 않은 데이터

query Products($id: ID!){ 
      node(id: $id) { 
        id 
        ... on Collection { 
           title 
           products(first: 250){ 
               edges{ 
                node{ 
                title 
                id 
              variants(first: 1){ 
               edges{ 
                node{ 
                 price 
                 } 
                } 
               } 
              images(first: 1,maxWidth:400,maxHeight:740,scale:3){ 
               edges{ 
                node{ 
                 src 
           } 
          } 
         } 
        } 
       } 
      } 
     } 
    } 
} 

제품의 제목, 가격 및 이미지를 recyclerview에 표시해야합니다 ("콜백 제품", String.valueOf (response.data(). 그들을 가져 오는 가장 좋은 방법은 무엇입니까? 미리 감사드립니다.

은 아래에있는 내 코드입니다 :

 @Override                             
protected void onCreate(@Nullable Bundle savedInstanceState) {                
    super.onCreate(savedInstanceState);                     
    setContentView(R.layout.activity_product);                    
    String s = getIntent().getStringExtra("id");                   
    Bundle extras = getIntent().getExtras();                    
    if (extras != null) {                         
     s = extras.getString("id");                      
    }                              
    application = (SampleApplication) getApplication();                 


    content = (ViewGroup) findViewById(R.id.rl_content_holder);               
    progressBar = (ProgressBar) findViewById(R.id.loading_bar);               
    productRecyclerView = (RecyclerView) findViewById(R.id.rvProductList);             
    mProductAdapter = new ProductAdapter(this);                   
    mGridLayoutManager = new GridLayoutManager(this,2);                 

    fetchProducts();                          
}                               

ApolloCall.Callback<Products.Data> mDataCallback =                   
     new ApolloCall.Callback<Products.Data>() {                   

      @Override                          
      public void onResponse(@Nonnull final Response<Products.Data> response) {          

        Log.i("CallBack products", String.valueOf(response.data().node().asCollection().products().edges())); 

        runOnUiThread(new Runnable() {                   
         @Override                       
         public void run() {                    
          Toast.makeText(ProductActivity.this, "getting products", Toast.LENGTH_SHORT).show();   
          mProductAdapter.setProductData(response.data().node().asCollection().products().edges());  
          productRecyclerView.setAdapter(mProductAdapter);            
          productRecyclerView.setLayoutManager(mGridLayoutManager);          
          progressBar.setVisibility(View.GONE);               
          content.setVisibility(View.VISIBLE);               
         }                         
        });                         

      }                            

      @Override                          
      public void onFailure(@Nonnull ApolloException e) {               
       Log.e("Product Activity", e.getMessage(), e);                
       runOnUiThread(new Runnable() {                    
        @Override                        
        public void run() {                     
         progressBar.setVisibility(View.GONE);                
         Toast.makeText(ProductActivity.this, "getting products failed", Toast.LENGTH_SHORT).show();  
        }                          
       });                          
      }                            
     };                             
public void fetchProducts() {                        

    mHttpClient = new OkHttpClient.Builder()                    
      .addInterceptor(new HttpInterceptor())                   
      .build();                          
    Products productQuery = Products                      
      .builder().id("id")                       
      .build();                          
    productCall = application.apolloClient().newCall(productQuery);              
    productCall.enqueue(mDataCallback);                     

} 

여기 RecyclerView 어댑터입니다 :

public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ViewHolder> { 

    // Store the context for easy access 
    private Context mContext; 

    private List<Products.Data.Edge> productData = Collections.emptyList(); 

    public void setProductData(List<Products.Data.Edge>productData) { 
     this.productData = productData; 
     this.notifyDataSetChanged(); 
    } 

    public ProductAdapter(Context context) { 

     mContext = context; 
    } 

    private Context getContext() { 
     return mContext; 
    } 



    public static class ViewHolder extends RecyclerView.ViewHolder { 

     public TextView productListItem; 
     public ImageView productListImage; 
     private View rlContainer; 
     Context context; 

     public ViewHolder(Context context,View itemView) { 
      super(itemView); 
      productListItem = (TextView) itemView.findViewById(R.id.itemProductTxt); 
      productListImage = (ImageView)itemView.findViewById(R.id.itemProductImg); 
      rlContainer = itemView.findViewById(R.id.linear_layout); 
      this.context = context; 
     } 

     public void setProductItem(final Products.Data.Edge productItem) { 

      productListItem.setText(productItem.node().title()); 


      rlContainer.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 

        Toast.makeText(context, "list item pressed", Toast.LENGTH_SHORT).show(); 

       } 
      }); 
     } 
    } 

    @Override 
    public ProductAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     Context context = parent.getContext(); 
     LayoutInflater inflater = LayoutInflater.from(context); 

     // Inflate the custom layout 
     View productView = inflater.inflate(R.layout.listitem_product, parent, false); 

     // Return a new holder instance 
     ProductAdapter.ViewHolder viewHolder = new ProductAdapter.ViewHolder(context,productView); 
     return viewHolder; 
    } 


    @Override 
    public void onBindViewHolder(ProductAdapter.ViewHolder holder, int position) { 

     final Products.Data.Edge productsEntry = this.productData.get(position); 
     holder.setProductItem(productsEntry); 



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


} 

답변

0

당신이 스택 추적을 제공하십시오 수 없습니다. 그리고 빈 응답을 되찾았습니까?

요청한 노드 별 ID가 존재하지 않기 때문에 NPE가 발생했을 수 있습니다.