'null android.view.View.getVisibility()'对空对象引用

时间:2016-11-25 10:17:45

标签: android android-recyclerview recycler-adapter

我正在使用recyclerview并突然遇到一个奇怪的错误。我已尽力而为。甚至放置断点。但它无法到达任何地方。 arraylist中有数据。我已经尝试了StackOverFlow上提供的所有解决方案。它仍然不起作用,也可用于ListView的解决方案。我正在使用recycerview,所以我无法得到解决方案。没有空值。  我的回收器适配器如下:

private class RecyclerAdapter extends RecyclerView.Adapter<RecyclerViewHolders> {

    private Context context;
    private ArrayList<Integer> arrayTarget = new ArrayList<>();
    private ArrayList<Integer> arrayAchievements = new ArrayList<>();
    private ArrayList<String> arrayAssociateName = new ArrayList<>();
    private ArrayList<String> arrayUserType = new ArrayList<>();
    private ArrayList<Integer> arrayAchievedGrp = new ArrayList<>();


    public RecyclerAdapter(Context c, ArrayList<Integer> arrayTarget,
                           ArrayList<String> arrayAssociateName, ArrayList<Integer> arrayAchievements,
                           ArrayList<String> arrayUserType, ArrayList<Integer> arrayAchievedGrp) {
        this.context = c;
        this.arrayTarget = arrayTarget;
        this.arrayAchievements = arrayAchievements;
        this.arrayAssociateName = arrayAssociateName;
        this.arrayUserType = arrayUserType;
        this.arrayAchievedGrp = arrayAchievedGrp;

    }


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

        return new RecyclerViewHolders(layout);
    }

    //this will attach the row at different positions
    @Override
    public void onBindViewHolder(RecyclerViewHolders mHolder, final int pos) {

        mHolder.tv_AssociateName.setText(arrayAssociateName.get(pos).toUpperCase());
        mHolder.tv_target.setText(String.valueOf(arrayTarget.get(pos)));
        mHolder.tv_achievements.setText(String.valueOf(arrayAchievements.get(pos)));
        mHolder.tv_AssociateType.setText(" ( " + arrayUserType.get(pos) + " )");
        mHolder.tv_achievedSraGrp.setText(String.valueOf(arrayAchievedGrp.get(pos)));
        try {
            percentage = (arrayAchievements.get(pos) * 100) / arrayTarget.get(pos);
            if (percentage > 100) {
                percentage = 100;
            }
        } catch (Exception e) {
            e.printStackTrace();
            percentage = 0;
        }

        arrayPercentage.add(percentage);
        mHolder.tv_percentage.setText(String.valueOf(percentage) + "%");

    }

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

public class RecyclerViewHolders extends RecyclerView.ViewHolder implements View.OnClickListener {

    TextView tv_AssociateName, tv_target, tv_achievements, tv_percentage, tv_AssociateType, tv_achievedSraGrp;
    ImageButton ib_alertButton;
    LinearLayout LL_achievedGrp;

    public RecyclerViewHolders(View itemView) {
        super(itemView);
        itemView.setOnClickListener(this);
        tv_AssociateName = (TextView) itemView.findViewById(R.id.tv_AssociateName);
        tv_target = (TextView) itemView.findViewById(R.id.tv_target);
        tv_achievements = (TextView) itemView.findViewById(R.id.tv_achievements);
        tv_percentage = (TextView) itemView.findViewById(R.id.tv_percentage);
        tv_AssociateType = (TextView) itemView.findViewById(R.id.tv_AssociateType);
        tv_achievedSraGrp = (TextView) itemView.findViewById(R.id.tv_achievedSraGrp);
        LL_achievedGrp = (LinearLayout) itemView.findViewById(R.id.LL_achievedGrp);

        ib_alertButton = (ImageButton) itemView.findViewById(R.id.ib_alertButton);
        ib_alertButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Toast.makeText(getApplicationContext(), "Coming Soon..", Toast.LENGTH_LONG).show();
            }
        });

        if (mainUserType.equalsIgnoreCase("ASE")) {
            LL_achievedGrp.setVisibility(View.VISIBLE);
        } else {
            LL_achievedGrp.setVisibility(View.GONE);
        }
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(getApplicationContext(), AssociatesDashboard.class);
        intent.putExtra("empCode", arrayAssociateEmpCd.get(getLayoutPosition()));    //to open dashboard for this user
        intent.putExtra("strActivityName", ActivityConstant.ASSOCIATE_LIST);
        intent.putExtra("userType", callingUser);

    }
}

调用构造函数。但后来没有任何位置得到错误。 LogCat如下:

FATAL EXCEPTION: main
                                                                           Process: com.mapmymarketing.salesscope, PID: 24220
                                                                           java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getVisibility()' on a null object reference
                                                                               at android.widget.LinearLayout.forceUniformHeight(LinearLayout.java:1380)
                                                                               at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1368)
                                                                               at android.widget.TableRow.onMeasure(TableRow.java:114)
                                                                               at android.view.View.measure(View.java:17547)
                                                                               at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
                                                                               at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
                                                                               at android.widget.TableLayout.measureChildBeforeLayout(TableLayout.java:464)
                                                                               at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
                                                                               at android.widget.TableLayout.measureVertical(TableLayout.java:476)
                                                                               at android.widget.TableLayout.onMeasure(TableLayout.java:439)
                                                                               at android.view.View.measure(View.java:17547)
                                                                               at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
                                                                               at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
                                                                               at android.support.v7.widget.CardView.onMeasure(CardView.java:211)
                                                                               at android.view.View.measure(View.java:17547)
                                                                               at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:727)
                                                                               at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:463)
                                                                               at android.view.View.measure(View.java:17547)
                                                                               at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
                                                                               at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:706)
                                                                               at android.support.design.widget.HeaderScrollingViewBehavior.onMeasureChild(HeaderScrollingViewBehavior.java:90)
                                                                               at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:1364)
                                                                               at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:771)
                                                                               at android.view.View.measure(View.java:17547)
                                                                               at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
                                                                               at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
                                                                               at android.view.View.measure(View.java:17547)
                                                                               at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:1085)
                                                                               at android.view.View.measure(View.java:17547)
                                                                               at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
                                                                               at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
                                                                               at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
                                                                               at android.view.View.measure(View.java:17547)
                                                                               at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
                                                                               at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
                                                                               at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
                                                                               at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
                                                                               at android.view.View.measure(View.java:17547)
                                                                               at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
                                                                               at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
                                                                               at android.view.View.measure(View.java:17547)
                                                                               at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
                                                                               at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
                                                                               at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
                                                                               at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
                                                                               at android.view.View.measure(View.java:17547)
                                                                               at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
                                                                               at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
                                                                               at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2615)
                                                                               at android.view.View.measure(View.java:17547)
                                                                               at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2015)
                                                                               at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1173)
                                                                               at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1379)
                                                                               at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)
                                                                               at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5885)
                                                                               at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
                                                                            at android.vi

这是回收器适配器行的XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/ll_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:background="@color/white"
android:orientation="vertical">

<android.support.v7.widget.CardView
    android:id="@+id/CardView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:paddingBottom="20dp"
    android:paddingTop="20dp"
    app:cardElevation="4dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginStart="5dp"
            android:layout_marginTop="5dp"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/tv_AssociateName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="5dp"
                android:text="Swaroop Pandey"
                android:textColor="@color/black"
                android:textSize="15sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/tv_AssociateType"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginStart="10dp"
                android:text="SO"
                android:textColor="@color/PrimaryDark2"
                android:textSize="15sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/tv_percentage"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:gravity="end"
                android:text="50 %"
                android:textColor="@color/pink"
                android:textSize="20sp"
                android:textStyle="bold" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginStart="10dp"
            android:layout_marginTop="5dp"
            android:orientation="horizontal">


            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="5dp"
                android:text="Target - "
                android:textColor="@color/black"
                android:textSize="15sp" />

            <TextView
                android:id="@+id/tv_target"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="10dp"
                android:layout_marginStart="10dp"
                android:text="10000"
                android:textColor="@color/PrimaryDark2"
                android:textSize="15sp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="10dp"
            android:layout_marginStart="10dp"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Achievements - "
                android:textColor="@color/black"
                android:textSize="15sp" />

            <TextView
                android:id="@+id/tv_achievements"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginStart="10dp"
                android:text="5000"
                android:textColor="@color/PrimaryDark2"
                android:textSize="15sp" />
        </LinearLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="10dp"
            android:layout_marginStart="10dp">

            <LinearLayout
                android:id="@+id/LL_achievedGrp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/tv_text"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="5dp"
                    android:text="Achieved SRA Group - "
                    android:textColor="@color/black"
                    android:textSize="15sp" />

                <TextView
                    android:id="@+id/tv_achievedSraGrp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="3"
                    android:textColor="@color/PrimaryDark2"
                    android:textSize="15sp" />
            </LinearLayout>

            <ImageButton
                android:id="@+id/ib_alertButton"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_marginEnd="10dp"
                android:layout_marginRight="10dp"
                android:background="@drawable/alert" />
        </RelativeLayout>
    </LinearLayout>
</android.support.v7.widget.CardView>

这是包含TableRow布局的布局(主布局)

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <android.support.v7.widget.CardView
        android:id="@+id/cardView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:elevation="5dp"
        app:cardBackgroundColor="@color/background">

        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="10dp">

            <TableRow
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_column="1"
                    android:layout_marginEnd="10dp"
                    android:layout_marginRight="10dp"
                    android:src="@drawable/blue_ring" />

                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_column="2"
                    android:layout_weight="3.5"
                    android:text="Total Associates"
                    android:textColor="@color/white"
                    android:textSize="15sp" />

                <TextView
                    android:id="@+id/tv_totalTarget"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_column="3"
                    android:layout_weight="1"
                    android:gravity="end|center"
                    android:text="3000"
                    android:textColor="@color/white"
                    android:textSize="15sp" />

                <TextView
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_column="4"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"
                    android:layout_weight="1"
                    android:gravity="end|center"
                    android:text="100%"
                    android:textColor="#8b8ce6"
                    android:textSize="15sp" />
            </TableRow>

            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_marginBottom="5dp"
                android:layout_marginTop="10dp"
                android:background="#30ffffff" />

            <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_column="1"
                    android:layout_marginEnd="10dp"
                    android:layout_marginRight="10dp"
                    android:src="@drawable/green_ring" />

                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_column="2"
                    android:layout_weight="3.5"
                    android:text="Associate achieved upto 5 group"
                    android:textColor="@color/white"
                    android:textSize="15sp" />

                <TextView
                    android:id="@+id/tv_totalAchievements"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_column="3"
                    android:layout_weight="1"
                    android:gravity="end|center"
                    android:text="1500"
                    android:textColor="@color/Primary"
                    android:textSize="15sp" />

                <TextView
                    android:id="@+id/tv_achievementsPercentage"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_column="4"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"
                    android:layout_weight="1"
                    android:gravity="end|center"
                    android:text="50%"
                    android:textColor="#51e4ca"
                    android:textSize="15sp" />
            </TableRow>

            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_marginBottom="5dp"
                android:layout_marginTop="10dp"
                android:background="#30ffffff" />

            <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_column="1"
                    android:layout_marginEnd="10dp"
                    android:layout_marginRight="10dp"
                    android:src="@drawable/orange_ring" />

                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_column="2"
                    android:layout_weight="3.5"
                    android:text="Associates achieved less than 5 group"
                    android:textColor="@color/white"
                    android:textSize="15sp" />

                <TextView
                    android:id="@+id/tv_totalGap"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_column="3"
                    android:layout_weight="1"
                    android:gravity="end|center"
                    android:text="1500"
                    android:textColor="@color/Primary"
                    android:textSize="15sp" />

                <TextView
                    android:id="@+id/tv_gapPercentage"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_column="4"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"
                    android:layout_weight="1"
                    android:gravity="end|center"
                    android:text="50%"
                    android:textColor="#ed9178"
                    android:textSize="15sp" />
            </TableRow>
        </TableLayout>
    </android.support.v7.widget.CardView>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/AssociateList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/cardView2"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp" />

    <TextView
        android:id="@+id/no_data_available"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/cardView2"
        android:layout_gravity="center"
        android:gravity="center"
        android:padding="10dp"
        android:text="No Data Available"
        android:textColor="@color/white"
        android:visibility="gone" />
</RelativeLayout>

2 个答案:

答案 0 :(得分:3)

我遇到了同样的问题而且我只删除了android:layout_column="",并且它有效。我不知道背后的理由

答案 1 :(得分:1)

我在主布局中删除了TableRow布局,并将其替换为Linear Layout。我真的不知道错误的原因。我会仔细看看的。一旦我得到正确的解决方案,我将编辑并提交正确的答案。