当我将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,如";像";或";retwoot";,现在我收到一个";ID不引用此活动内的视图";导航抽屉活动错误

最新更新