【问题标题】:Access buttons in Activity from card view从卡片视图访问活动中的按钮
【发布时间】:2018-11-09 23:08:42
【问题描述】:

我是 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);
  • 然后将其作为答案发布。你可以接受

标签: android button android-activity material-design android-cardview


【解决方案1】:

你试过了吗:

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

【讨论】:

    猜你喜欢
    • 2014-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多