代码之家  ›  专栏  ›  技术社区  ›  Talib

从卡视图访问活动中的按钮

  •  0
  • Talib  · 技术社区  · 8 年前

    我对android还不熟悉,我相信解决方案会非常简单,但我在这上面浪费时间。

    约束布局与内部的卡视图一起使用。

    我有两张单独的卡片,里面有按钮。当我试图进入按钮,它给我npe。

    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"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".LandingPage"
        tools:layout_editor_absoluteY="81dp">
    <android.support.v7.widget.CardView
        android:id="@+id/signupCard"
        style="@style/CardView.Light"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="18dp"
        android:layout_marginEnd="29dp"
        android:layout_marginLeft="29dp"
        android:layout_marginRight="29dp"
        android:layout_marginStart="29dp"
        android:layout_marginTop="198dp"
        app:layout_constraintBottom_toTopOf="@+id/loginCard"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">
    
        <Button
            android:id="@+id/signup"
            style="@style/Widget.AppCompat.Button.Borderless"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/background_light"
            android:minWidth="0dp"
            android:paddingEnd="8dp"
            android:paddingStart="8dp"
            android:scaleType="center"
            android:text="@string/signUp"
            android:textColor="#DE000000" />
    </android.support.v7.widget.CardView>
    
    <android.support.v7.widget.CardView
        android:id="@+id/loginCard"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="69dp"
        android:layout_marginEnd="25dp"
        android:layout_marginLeft="25dp"
        android:layout_marginRight="25dp"
        android:layout_marginStart="25dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/signupCard">
    
        <Button
            android:id="@+id/login"
            style="@style/Widget.AppCompat.Button.Borderless"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/background_light"
            android:minWidth="0dp"
            android:paddingEnd="8dp"
            android:paddingStart="8dp"
            android:scaleType="center"
            android:text="@string/login"
            android:textColor="#DE000000" />
    </android.support.v7.widget.CardView>
    

    代码:

      private   CardView signUpCardview;
            private   Button signUp;
             signUpCardview= new CardView(this);
                signUpCardview = (CardView)signUpCardview.findViewById(R.id.signupCard);
                signUp= (Button)signUpCardview.findViewById(R.id.signup);
    

    我得到以下错误:

    java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.support.v7.widget.CardView.getContext()' on a null object reference
    
    1 回复  |  直到 8 年前
        1
  •  1
  •   Yunus Kulyyev    8 年前

    你试过了吗:

    signUp= (Button) findViewById(R.id.signup);