2016-09-24 11 views
0

activity_product_detail.xmlNullPointerExceptionがバインドのArrayList

<data> 

    <import type="android.text.Html"/> 
    <import type="java.util.List" alias="list"/> 

    <variable 
     name="productDetail" 
     type="com.example.k2.kishanoil.pojoclasses.productdetail.ProductDescription" /> 

    <variable 
     name="dProduct" 
     type="com.example.k2.kishanoil.pojoclasses.product.ProductData" /> 

    <variable 
     name="sliderList" 
     type="list"/> 

</data> 


<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.k2.kishanoil.activities.ProductDetailActivity"> 


    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <RelativeLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"> 

      <com.daimajia.slider.library.SliderLayout 
       android:id="@+id/productDetailSlider" 
       android:layout_width="match_parent" 
       android:layout_height="0dp" 
       tools:auto_cycle="true" 
       tools:indicator_visibility="visible" 
       adapter="@{sliderList}"/> 

      <ImageView 
       android:id="@+id/descSliderPlay" 
       android:layout_width="100dp" 
       android:layout_height="100dp" 
       android:layout_centerHorizontal="true" 
       android:layout_centerVertical="true" 
       android:src="@drawable/playimage" 
       android:visibility="gone" /> 


      <com.daimajia.slider.library.Indicators.PagerIndicator 
       android:id="@+id/detailCustomIndecator" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       tools:shape="oval" /> 

     </RelativeLayout> 

     <android.support.v7.widget.CardView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_margin="5dp" 
      app:cardCornerRadius="5dp"> 

      <RelativeLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_margin="10dp"> 

       <TextView 
        android:id="@+id/productDetailTitle" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_marginBottom="10dp" 
        android:layout_marginLeft="10dp" 
        android:text="@{productDetail.name}" 
        android:textColor="@color/colorDarkBlack" 
        android:textSize="20dp" /> 

       <android.support.v7.widget.AppCompatCheckBox 
        android:id="@+id/productDetailcheckbox" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignLeft="@+id/productDetailTitle" 
        android:layout_alignStart="@+id/productDetailTitle" 
        android:layout_below="@+id/productDetailTitle" 
        android:text="Add To Inquiry" 
        android:textColor="@color/colorLightGray" 
        app:buttonTint="@color/colorPrimaryDark" 
        android:checked="@{(dProduct.selected == true) ? true : false }"/> 

       <ImageView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignParentEnd="true" 
        android:layout_alignParentRight="true" 
        android:layout_alignTop="@+id/productDetailcheckbox" 
        android:layout_marginRight="10dp" 
        android:padding="5dp" 
        android:src="@drawable/icon_share" /> 

      </RelativeLayout> 


     </android.support.v7.widget.CardView> 

     <android.support.v4.widget.NestedScrollView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_marginLeft="5dp" 
      android:layout_marginRight="5dp" 
      android:layout_marginTop="5dp"> 

      <android.support.v7.widget.CardView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       app:cardCornerRadius="5dp"> 

       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_margin="5dp" 
        android:orientation="vertical"> 

        <TextView 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_marginBottom="10dp" 
         android:layout_marginLeft="10dp" 
         android:text="Description" 
         android:textColor="@color/colorDarkBlack" 
         android:textSize="20dp" /> 

        <TextView 
         android:id="@+id/detailDescription" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_marginLeft="5dp" 
         android:text="@{productDetail.description}" /> 


       </LinearLayout> 

      </android.support.v7.widget.CardView> 

     </android.support.v4.widget.NestedScrollView> 

    </LinearLayout> 

</android.support.design.widget.CoordinatorLayout> 

ProductDetailActivity.java

public class ProductDetailActivity extends AppCompatActivity { 

private SliderLayout imageSlider; 

private Context context; 

private DisplayMetrics matrics; 
private Display display; 

private ActivityProductDetailBinding detailDatabinding; 

private ProductData productData; 

private ProductDetailResponse res; 

String mediaumUrl; 

ImageView sliderPlay; 

ActionBar actionBar; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    //setContentView(R.layout.activity_product_detail); 
    detailDatabinding = DataBindingUtil.setContentView(ProductDetailActivity.this, R.layout.activity_product_detail); 

    if(getIntent() != null) 
     productData = new Gson().fromJson(getIntent().getStringExtra("productData"), ProductData.class); 

    context = getApplicationContext(); 

    actionBar = getSupportActionBar(); 
    actionBar.setTitle(productData.getName()); 

    actionBar.setHomeButtonEnabled(true); 
    actionBar.setDisplayHomeAsUpEnabled(true); 

    sliderPlay = (ImageView) findViewById(R.id.descSliderPlay); 

if(MyInternetConnection.isConnected(context)){ 

     RetrofitInterface retrofitInterface = MyRetrofit.getRetrofit().create(RetrofitInterface.class); 

     Call<ProductDetailResponse> productDetailResponseCall = retrofitInterface.getProductDetail(MyRetrofit.TOKEN, productData.getId()); 

     productDetailResponseCall.enqueue(new Callback<ProductDetailResponse>() { 
      @Override 
      public void onResponse(Call<ProductDetailResponse> call, Response<ProductDetailResponse> response) { 

       if(response.isSuccessful()){ 
        res = response.body(); 
        Log.d(TAG, "onResponse: ProductDetail Result " + res.getResult() + " Message : " + res.getMessage()); 
        if(res.getResult().equals(KeyVariable.RESPONSE_SUCCESS)){ 

         Log.d(TAG, "onResponse: Product detail " + new Gson().toJson(res.getData().getProduct().get(0)) + " \n size" + res.getData().getProductAttachment().size()); 

         detailDatabinding.setVariable(BR.sliderList, res.getData().getProductAttachment()); 

         Log.d(TAG, "onResponse: List size " + res.getData().getProductAttachment().size()); 

       if (res.getData().getProduct().get(0).getDescription() == null){ 
          res.getData().getProduct().get(0).setDescription(""); 
         } 
         detailDatabinding.setVariable(BR.productDetail, res.getData().getProduct().get(0)); 
         detailDatabinding.setVariable(BR.dProduct, productData); 

         detailDatabinding.executePendingBindings(); 
        } 
       } 
      } 

      @Override 
      public void onFailure(Call<ProductDetailResponse> call, Throwable t) { 
       MySnackBar.showGeneralSnackbar(imageSlider.getRootView(), R.string.msg_problem); 
       Log.d(TAG, "onFailure: ProductDetailActivity " + t.getLocalizedMessage()); 
      } 
     }); 

    } else { 
     MySnackBar.showInternetSnackbar(imageSlider.getRootView()); 
    } 
} 
} 

ProductDetailHandler.java

私のハンドラクラスでArrayListのサイズを出力すると、NullPointerExceptionが生成されますが、自分のアクティビティクラスで印刷したときに正常に動作します。あなたの改造のネットワーク呼び出しで

+2

:私はProductDetailActivityに(new List<>();として初期化される)mSliderList変数インスタンスを作成し、によって改造呼び出しの前にそれを設定した方が良いと思います。 –

答えて

0

onResponseコールバックは非同期であり、ProductDetailHandlerの後半sliderInitializeより発射されるので、ヨーヨーはsliderInitializeにNPEを取得します。テキストとしてあなたのコードを投稿してください

detailDatabinding.setVariable(BR.sliderList, mSliderList); 
+0

うまくいってもデータはスライダーに読み込めませんでした –

関連する問題