微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么将 ImageView 添加到 CardView 或片段时应用会崩溃?

如何解决为什么将 ImageView 添加到 CardView 或片段时应用会崩溃?

我正在尝试制作一个类似于 Twitter 的 CardView,以便将它与 RecyclerView 一起使用。 没有 ImageView 的 CardView 运行良好,但是当我添加 ImageView 时,应用程序给了我一个 NullPointerException

这是没有 ImageView 的 CardView 的代码效果很好):

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

<androidx.cardview.widget.CardView 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:id="@+id/twoot_cardView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardUseCompatPadding="true"
    tools:ignore="MissingConstraints">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:animateLayoutChanges="true"
        android:orientation="vertical">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="5dp">

            <de.hdodenhof.circleimageview.CircleImageView
                android:id="@+id/twootUserImg"
                android:layout_width="60dp"
                android:layout_height="60dp"
                android:padding="5dp"
                android:src="@mipmap/user_round" />

            <LinearLayout
                android:id="@+id/twootContent"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="5dp"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="5dp"
                android:layout_marginEnd="5dp"
                android:layout_marginRight="5dp"
                android:layout_marginBottom="5dp"
                android:layout_toRightOf="@id/twootUserImg"
                android:orientation="vertical"
                android:layout_toEndOf="@id/twootUserImg">

                <TextView
                    android:id="@+id/twootUserName"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="UserName"
                    android:textColor="#404040"
                    android:textSize="16sp"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/twootUserTwoot"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="5dp"
                    android:text="This is a sample Twoot" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="horizontal">

                    <TextView
                        android:id="@+id/twootUserRetwoot"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="8" />

                    <TextView
                        android:id="@+id/twootUserLike"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="15" />
                </LinearLayout>
            </LinearLayout>
        </RelativeLayout>
    </LinearLayout>
</androidx.cardview.widget.CardView>

这是代码 CardView 和 ImageView 的赞和转发(不起作用):

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

<androidx.cardview.widget.CardView 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:id="@+id/twoot_cardView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardUseCompatPadding="true"
    tools:ignore="MissingConstraints">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:animateLayoutChanges="true"
        android:orientation="vertical">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="5dp">

            <de.hdodenhof.circleimageview.CircleImageView
                android:id="@+id/twootUserImg"
                android:layout_width="60dp"
                android:layout_height="60dp"
                android:padding="5dp"
                android:src="@mipmap/user_round" />

            <LinearLayout
                android:id="@+id/twootContent"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="5dp"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="5dp"
                android:layout_marginEnd="5dp"
                android:layout_marginRight="5dp"
                android:layout_marginBottom="5dp"
                android:layout_toRightOf="@id/twootUserImg"
                android:orientation="vertical"
                android:layout_toEndOf="@id/twootUserImg">

                <TextView
                    android:id="@+id/twootUserName"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/username"
                    android:textColor="#404040"
                    android:textSize="16sp"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/twootUserTwoot"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="5dp"
                    android:text="This is a sample Twoot" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="horizontal">

                    <ImageView
                        android:id="@+id/imageView11"
                        android:layout_width="20dp"
                        android:layout_height="20dp"
                        android:layout_weight="1"
                        app:srcCompat="@drawable/retwoot" />

                    <TextView
                        android:id="@+id/twootUserRetwoot"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="8" />

                    <ImageView
                        android:id="@+id/imageView12"
                        android:layout_width="20dp"
                        android:layout_height="20dp"
                        android:layout_weight="1"
                        app:srcCompat="@drawable/heart" />

                    <TextView
                        android:id="@+id/twootUserLike"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="15" />
                </LinearLayout>
            </LinearLayout>
        </RelativeLayout>
    </LinearLayout>
</androidx.cardview.widget.CardView>

我得到的错误是:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.twittor,PID: 13755
    java.lang.IllegalStateException: Could not execute method for android:onClick
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:414)
        at android.view.View.performClick(View.java:7448)
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
        at android.view.View.performClickInternal(View.java:7425)
        at android.view.View.access$3600(View.java:810)
        at android.view.View$PerformClick.run(View.java:28305)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409)
        at android.view.View.performClick(View.java:7448) 
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119) 
        at android.view.View.performClickInternal(View.java:7425) 
        at android.view.View.access$3600(View.java:810) 
        at android.view.View$PerformClick.run(View.java:28305) 
        at android.os.Handler.handleCallback(Handler.java:938) 
        at android.os.Handler.dispatchMessage(Handler.java:99) 
        at android.os.Looper.loop(Looper.java:223) 
        at android.app.ActivityThread.main(ActivityThread.java:7656) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
        at com.example.twittor.LoginActivity.Next(LoginActivity.java:32)
        at java.lang.reflect.Method.invoke(Native Method) 
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409) 
        at android.view.View.performClick(View.java:7448) 
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119) 
        at android.view.View.performClickInternal(View.java:7425) 
        at android.view.View.access$3600(View.java:810) 
        at android.view.View$PerformClick.run(View.java:28305) 
        at android.os.Handler.handleCallback(Handler.java:938) 
        at android.os.Handler.dispatchMessage(Handler.java:99) 
        at android.os.Looper.loop(Looper.java:223) 
        at android.app.ActivityThread.main(ActivityThread.java:7656) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 

尝试:

我尝试使用较小的照片或在其他标签添加 ImageView,但两者都不起作用

LoginActivity.java 是包含 RecyclerView 的活动之前的活动

我还尝试在包含 RecyclerView 的片段上添加图像(应用程序也因此崩溃)

解决方法

最后,我将 id 从 ImageViewX 更改为其他 id,如“like”或“retwoot”,现在我在导航抽屉活动中收到“ID 未引用此活动内的视图”错误

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。