片段中的initUI()在图像视图中给出错误

时间:2019-02-17 15:08:22

标签: android android-fragments exception-handling nullpointerexception

在Fragment中,我做了initUI()并初始化了图像视图,但是却报错了。我正在提供Fragment的initUI()和XML文件。

错误:

 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
        at novumlogic.payment.home.CateFragment.initUI(CateFragment.java:95)
        at novumlogic.payment.home.CateFragment.onCreate(CateFragment.java:61)

片段

private  void initUI() {

    //@SuppressLint("ResourceType") AppCompatTextView tvDay = ((AppCompatTextView) rootView.findViewById(R.id.imgSendMoney,16.0F));
    //AppCompatTextView tvDay1 = (AppCompatTextView) rootView.findViewById(R.id.imgReceiveMoney);
   ViewCompat.setElevation((ImageView) rootView.findViewById(R.id.imgSendMoney), 16.0F);
   ViewCompat.setElevation((ImageView)rootView.findViewById(R.id.imgReceiveMoney), 16.0F);
    /*((AppCompatImageView)root.findViewById(R.id.imgNotification)).setOnClickListener((View.OnClickListener) new View.OnClickListener() {
        public final void onClick(View it) {
            Toast.makeText((Context)HomeFragment.this, (CharSequence)"Notification List", 0).show();
        }
    });*/
    ((CardView)rootView.findViewById(R.id.cardSendMoney)).setOnClickListener((View.OnClickListener)(new View.OnClickListener() {
        public final void onClick(View it) {
            Intent k = new Intent(getContext(), SendMoneyActivity.class);
            startActivity(k);

            //HomeFragment.this.startActivity(new Intent((Context)HomeFragment.this.getActivity(), SendMoneyActivity.class));
        }
    }));

  CardView card_view = (CardView) rootView.findViewById(R.id.cardReceiveMoney);
    card_view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // do whatever you want to do on click (to launch any fragment or activity you need to put intent here.)
            Intent k = new Intent(getContext(), ReceiveMoneyActivity.class);
            startActivity(k);
        }
    });
    ((RecyclerView)rootView.findViewById(R.id.recyclerBankAccounts)).setItemViewCacheSize(0);
    ((RecyclerView)rootView.findViewById(R.id.recyclerBankAccounts)).setHasFixedSize(true);
    RecyclerView var10000 = (RecyclerView)rootView.findViewById(R.id.recyclerBankAccounts);
    Intrinsics.checkExpressionValueIsNotNull(var10000, "root.recyclerBankAccounts");
    var10000.setLayoutManager((RecyclerView.LayoutManager)(new LinearLayoutManager((Context)this.getActivity(), 0, false)));
    BankAccountsListAdapter bankAccountsAdapter = new BankAccountsListAdapter();
    var10000 = (RecyclerView)rootView.findViewById(R.id.recyclerBankAccounts);
    Intrinsics.checkExpressionValueIsNotNull(var10000, "root.recyclerBankAccounts");
    var10000.setAdapter((RecyclerView.Adapter)bankAccountsAdapter);
}

XML:

<?xml version="1.0" encoding="utf-8"?>

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="?android:attr/actionBarSize">

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <android.support.v7.widget.AppCompatImageView
                android:id="@+id/imgLogo"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_marginStart="@dimen/margin_8"
                android:layout_marginTop="@dimen/margin_16"
                android:layout_marginBottom="@dimen/margin_16"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:srcCompat="@drawable/fee_logo" />

            <android.support.v7.widget.AppCompatTextView
                style="@style/BoldTitleStyle"
                android:layout_margin="@dimen/margin_8"
                android:text="@string/app_name"
                app:layout_constraintBottom_toBottomOf="@+id/imgLogo"
                app:layout_constraintStart_toEndOf="@id/imgLogo"
                app:layout_constraintTop_toTopOf="@+id/imgLogo" />

            <android.support.v7.widget.AppCompatImageView
                android:id="@+id/imgNotification"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/margin_16"
                android:layout_marginEnd="@dimen/margin_8"
                android:layout_marginBottom="@dimen/margin_16"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:srcCompat="@drawable/ic_bell_icon" />

            <android.support.v7.widget.AppCompatImageView
                android:id="@+id/imgBadge"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:layout_constraintStart_toStartOf="@id/imgNotification"
                app:layout_constraintTop_toTopOf="@id/imgNotification"
                app:srcCompat="@drawable/badge" />

            <android.support.v7.widget.AppCompatTextView
                android:id="@+id/txtBadge"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="@dimen/margin_8"
                android:text="02"
                android:textColor="@color/colorPrimary"
                android:textSize="@dimen/text_tiny_8"
                app:layout_constraintBottom_toBottomOf="@id/imgBadge"
                app:layout_constraintEnd_toEndOf="@id/imgBadge"
                app:layout_constraintStart_toStartOf="@id/imgBadge"
                app:layout_constraintTop_toTopOf="@id/imgBadge" />

        </android.support.constraint.ConstraintLayout>

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

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/nestedScrollView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:padding="@dimen/margin_8"
        android:layout_marginBottom="@dimen/margin_8"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/appbar">

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <android.support.v7.widget.AppCompatTextView
                android:id="@+id/txtMyBankAccounts"
                style="@style/BoldSubTitleStyle"
                android:text="@string/title_my_bank_accounts"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recyclerBankAccounts"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginEnd="8dp"
                app:layout_constraintEnd_toEndOf="@+id/cardReceiveMoney"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/txtMyBankAccounts" />

            <android.support.v7.widget.CardView
                android:id="@+id/cardSendMoney"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="@dimen/margin_8"
                android:layout_marginTop="@dimen/margin_16"
                android:layout_marginRight="@dimen/margin_4"
                android:layout_marginBottom="@dimen/margin_16"
                app:cardCornerRadius="@dimen/margin_16"
                app:layout_constraintEnd_toStartOf="@+id/cardReceiveMoney"
                app:layout_constraintHorizontal_bias="0.5"
                app:layout_constraintHorizontal_chainStyle="packed"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/recyclerBankAccounts">

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:orientation="horizontal"
                    android:padding="@dimen/margin_8">

                    <ImageView
                        android:id="@+id/imgSendMoney"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="@dimen/margin_8"
                        android:layout_marginEnd="@dimen/margin_8"
                        android:background="@drawable/send_money_icon"
                        android:padding="@dimen/margin_8"
                        app:srcCompat="@drawable/ic_arrow_forward" />

                    <android.support.v7.widget.AppCompatTextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:fontFamily="@font/poppins_bold"
                        android:padding="@dimen/margin_8"
                        android:text="@string/msg_send_money"
                        android:textColor="@color/colorSecondary" />

                </LinearLayout>

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

            <android.support.v7.widget.CardView
                android:id="@+id/cardReceiveMoney"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="@dimen/margin_4"
                android:layout_marginTop="@dimen/margin_16"
                android:layout_marginRight="@dimen/margin_8"
                android:layout_marginBottom="@dimen/margin_16"
                app:cardCornerRadius="@dimen/margin_16"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.5"
                app:layout_constraintHorizontal_chainStyle="packed"
                app:layout_constraintStart_toEndOf="@+id/cardSendMoney"
                app:layout_constraintTop_toBottomOf="@id/recyclerBankAccounts">

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:orientation="horizontal"
                    android:padding="@dimen/margin_8">

                    <ImageView
                        android:id="@+id/imgReceiveMoney"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="@dimen/margin_8"
                        android:layout_marginEnd="@dimen/margin_8"
                        android:background="@drawable/receive_money_icon"
                        android:padding="@dimen/margin_8"
                        app:srcCompat="@drawable/ic_arrow_back" />

                    <android.support.v7.widget.AppCompatTextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:fontFamily="@font/poppins_bold"
                        android:padding="@dimen/margin_8"
                        android:text="@string/msg_receive_money"
                        android:textColor="@color/colorSecondary" />

                </LinearLayout>

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

            <android.support.v7.widget.AppCompatTextView
                android:id="@+id/txtSendMoneyTo"
                style="@style/BoldSubTitleStyle"
                android:layout_marginTop="@dimen/margin_22"
                android:text="@string/label_send_money_to"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/cardSendMoney" />

            <android.support.v7.widget.AppCompatTextView
                android:id="@+id/txtSendMoneyToViewAll"
                style="@style/BoldSubTitleStyle"
                android:layout_margin="@dimen/margin_8"
                android:fontFamily="@font/poppins_medium"
                android:text="@string/label_view_all"
                android:textSize="@dimen/text_small_14"
                app:layout_constraintBaseline_toBaselineOf="@+id/txtSendMoneyTo"
                app:layout_constraintEnd_toEndOf="@id/cardReceiveMoney" />

            <android.support.v7.widget.CardView
                android:id="@+id/linearSendMoneyTo"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/margin_8"
                app:cardCornerRadius="@dimen/margin_16"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/txtSendMoneyTo">

                <android.support.constraint.ConstraintLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <novumlogic.payment.customview.DashboardContactItem
                        android:id="@+id/imgAddContact"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_margin="@dimen/margin_8"
                        app:contactImage="@drawable/ic_add_circle"
                        app:contactName="Add New"
                        app:hasBackgroundRing="true"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toStartOf="@+id/contactItem1"
                        app:layout_constraintHorizontal_bias="0.5"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent" />

                    <novumlogic.payment.customview.DashboardContactItem
                        android:id="@+id/contactItem1"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_margin="@dimen/margin_8"
                        app:contactImage="@drawable/ic_avatar"
                        app:contactName="Alice"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toStartOf="@+id/contactItem2"
                        app:layout_constraintHorizontal_bias="0.5"
                        app:layout_constraintHorizontal_chainStyle="packed"
                        app:layout_constraintStart_toEndOf="@+id/imgAddContact"
                        app:layout_constraintTop_toTopOf="parent" />

                    <novumlogic.payment.customview.DashboardContactItem
                        android:id="@+id/contactItem2"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_margin="@dimen/margin_8"
                        app:contactImage="@drawable/ic_avatar"
                        app:contactName="Bob"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toStartOf="@+id/contactItem3"
                        app:layout_constraintHorizontal_bias="0.5"
                        app:layout_constraintHorizontal_chainStyle="packed"
                        app:layout_constraintStart_toEndOf="@+id/contactItem1"
                        app:layout_constraintTop_toTopOf="parent" />

                    <novumlogic.payment.customview.DashboardContactItem
                        android:id="@+id/contactItem3"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_margin="@dimen/margin_8"
                        app:contactImage="@drawable/ic_avatar"
                        app:contactName="Emily"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toStartOf="@+id/imgViewMore"
                        app:layout_constraintHorizontal_bias="0.5"
                        app:layout_constraintHorizontal_chainStyle="packed"
                        app:layout_constraintStart_toEndOf="@+id/contactItem2"
                        app:layout_constraintTop_toTopOf="parent" />

                    <android.support.v7.widget.AppCompatImageView
                        android:id="@+id/imgViewMore"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintHorizontal_bias="0.5"
                        app:layout_constraintHorizontal_chainStyle="packed"
                        app:layout_constraintStart_toEndOf="@+id/contactItem3"
                        app:layout_constraintTop_toTopOf="parent"
                        app:srcCompat="@drawable/ic_keyboard_arrow_right" />

                </android.support.constraint.ConstraintLayout>
            </android.support.v7.widget.CardView>
        </android.support.constraint.ConstraintLayout>
    </android.support.v4.widget.NestedScrollView>

 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">
<android.support.design.widget.CollapsingToolbarLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/collaps"
    android:fitsSystemWindows="true"

    app:expandedTitleMarginStart="48dp"

    app:expandedTitleMarginEnd="64dp"
    >

    <FrameLayout
        android:id="@+id/institutions_tab_fragments"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <com.cooltechworks.views.shimmer.ShimmerRecyclerView
            android:id="@+id/recycler_view_institutions"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollbars="vertical" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:id="@+id/noDataFound"
            android:visibility="invisible"
            >
            <include
                layout="@layout/no_data_found"
                >
            </include>
        </LinearLayout>
    </FrameLayout>

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

    android:id="@+id/scroll"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:clipToPadding="false"

    app:layout_behavior="@string/appbar_scrolling_view_behavior">

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

</LinearLayout>
</android.support.constraint.ConstraintLayout>

1 个答案:

答案 0 :(得分:0)

您的rootView为空。

看着堆栈跟踪,您正在initUI()中调用onCreate()。还为时过早。如果您的rootView是在onCreateView()中膨胀的通常视图,则需要在通货膨胀后调用initUI(),在onCreateView()中调用onViewCreated(),然后实际初始化{{ 1}}。