绑定ArrayList时数据绑定中的NullPointerException

时间:2016-09-24 10:42:30

标签: arraylist nullpointerexception android-databinding

activity_product_detail.xml

<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

public class ProductDetailHandler {

private static Context context;

private static String mediaumUrl;

private static SliderLayout imageSlider;

private static List<ProductAttachment> attachment;

private static ProductAttachment attachItem;


@BindingAdapter({"adapter"})
public static void sliderInitialize(View view, ProductDetailData productDetailData){

    imageSlider = (SliderLayout) view;

    attachment = new ArrayList<>();
    attachment = productDetailData.getProductAttachment();

    context = view.getContext();

Log.d(TAG, "sliderInitialize: ProductDetailHandler size " + attachment.size());
}
}

当我在处理程序类中打印ArrayList的大小时,它会生成NullPointerException,但是当我在我的activity类中打印时,它的工作正常。

1 个答案:

答案 0 :(得分:0)

您的改装网络调用中的onResponse回调是异步的,并且会在sliderInitialize中的ProductDetailHandler之后触发,因此您将在sliderInitialize中获得NPE。我认为最好在mSliderList中创建一个实例变量new List<>();(初始化为ProductDetailActivity)并在改造调用之前将其设置为:

detailDatabinding.setVariable(BR.sliderList, mSliderList);