从卡片视图访问 Activity 中的按钮

Access buttons in Activity from card view

我是 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

你试过了吗:

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