文本视图为空

时间:2016-04-20 18:10:09

标签: android null textview android-recyclerview

我正在使用卡片视图开发应用程序。但是我无法获得textview id。我使用recyclerview适配器。我将carview充气。卡片视图包含我需要的文本。所有其他{ {1}}工作正常。所以我得到textViews This is the image when debugging。我的null pointer Exception适配器类

recyclerview

我的cardview xml

public class RecyclerViewAdapterbrands extends RecyclerView
        .Adapter<RecyclerViewAdapterbrands
        .DataObjectHolder> {
    private static String LOG_TAG = "MyRecyclerViewAdapter";
    private ArrayList<DataObject> mDataset;
    private static MyClickListener myClickListenerhi;
    private String headline;
    private String reporterName;
    private String date;
    private String url;
    private Activity activityContext;
    //private static MyClickListener myClickListener;
    public static class DataObjectHolder extends RecyclerView.ViewHolder implements View.OnClickListener
    {
        TextView label,price,disctext;
        ImageView imageView;
        ImageView discount;
        ProgressBar progressBar;

        public DataObjectHolder(final View itemView, final Context activity, final ArrayList<DataObject> myDataset) {
            super(itemView);
            label = (TextView) itemView.findViewById(R.id.textView);
            price= (TextView) itemView.findViewById(R.id.price);
            imageView=(ImageView)itemView.findViewById(R.id.iproductimg);
            discount=(ImageView)itemView.findViewById(R.id.discimg);
            disctext=(TextView) itemView.findViewById(R.id.discounttxt);//here not getting the id
            progressBar=(ProgressBar)itemView.findViewById(R.id.progress);
            Log.i(LOG_TAG, "Adding Listener");
            itemView.setOnClickListener(this);

        }

        @Override
        public void onClick(View v) {
            myClickListenerhi.onItemClick(getLayoutPosition(),v);
        }
    }



    public RecyclerViewAdapterbrands(Activity context, ArrayList<DataObject> myDataset, MyClickListener myClickListener1) {
        mDataset = myDataset;
        activityContext=context;
        myClickListenerhi=myClickListener1;
    }

    @Override
    public DataObjectHolder onCreateViewHolder(ViewGroup parent,
                                               int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.cardview, parent, false);

        DataObjectHolder dataObjectHolder = new DataObjectHolder(view,activityContext,mDataset);
        return dataObjectHolder;
    }

    @Override
    public void onBindViewHolder(final DataObjectHolder holder, int position) {
        Context context=null;
        holder.label.setText(mDataset.get(position).getmText1());
        holder.price.setText(String.format("Price:%s", mDataset.get(position).getPrice()));
       // holder.dateTime.setText(mDataset.get(position).getmText2());
        Drawable placeholder = holder.imageView.getContext().getResources().getDrawable(R.drawable.placeholder);
        holder.imageView.setImageDrawable(placeholder);
        if(Double.parseDouble(mDataset.get(position).getDisc())>0){
            holder.discount.setVisibility(View.VISIBLE);
            holder.disctext.setVisibility(View.VISIBLE);
            holder.disctext.setText(mDataset.get(position).getDisc());
        }
        //new ImageDownloaderTask(holder.imageView).execute(mDataset.get(position).getUrl());
        mDataset.get(position).setPosition(position);
            Picasso.with(activityContext)
                .load(mDataset.get(position).getUrl())
                .placeholder(R.drawable.placeholder)
                .into(holder.imageView, new Callback() {
                    @Override
                    public void onSuccess() {
                        holder.progressBar.setVisibility(View.INVISIBLE);
                    }

                    @Override
                    public void onError() {
                    }
                });
    }

    public void addItem(DataObject dataObj, int index) {
        mDataset.add(index, dataObj);
        notifyItemInserted(index);
    }

    public void deleteItem(int index) {
        mDataset.remove(index);
        notifyItemRemoved(index);
    }

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

    public interface MyClickListener {
        void onItemClick(int position, View v);
    }

}

我的日志猫

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:weightSum="3"
    >

    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_width="156dp"
        android:layout_height="274dp"
        android:layout_margin="5dp"
        card_view:cardCornerRadius="2dp"
        card_view:contentPadding="5dp"
        card_view:cardBackgroundColor="#ffffff">

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

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="200dp"
                android:id="@+id/iproductimg"
                android:src="@drawable/placeholder"
                android:layout_alignParentTop="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:scaleType="fitXY"
                android:layout_alignBottom="@+id/progress" />
            <ProgressBar
                android:id="@+id/progress"
                style="?android:attr/progressBarStyleSmall"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:visibility="visible"
                android:indeterminate="false" />
            <TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textStyle="bold"
                android:layout_above="@+id/price"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true" />

            <TextView
                android:id="@+id/price"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textStyle="bold"
                android:layout_alignParentBottom="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/discimg"
                android:src="@drawable/disc"
                android:layout_alignParentTop="true"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true"
                android:visibility="invisible" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="-15%"
                android:id="@+id/discounttxt"     //this is the textview id ineeded
                android:textIsSelectable="false"
                android:textSize="10sp"
                android:layout_marginLeft="8dp"
                android:layout_marginStart="8dp"
                android:layout_alignBottom="@+id/discimg"
                android:layout_alignLeft="@+id/discimg"
                android:layout_alignStart="@+id/discimg"
                android:layout_marginBottom="13dp"
                android:visibility="invisible" />

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

0 个答案:

没有答案