Java 应用程序停止处理CircleImageView的setImageResource

Java 应用程序停止处理CircleImageView的setImageResource,java,android,android-xml,Java,Android,Android Xml,我必须在应用程序中使用java代码动态显示一些图像。我可以使用simpleImageView来实现这一点。但使用“hdodenhof”。我只能使用XML文件中硬编码的静态图像。我希望从java代码中动态选择源代码。我试过一些选择。但该应用程序似乎停止工作。有什么我能做的吗?先谢谢你 这是我的DisplayMessageActivity.java文件 import de.hdodenhof.circleimageview.CircleImageView; LinearLayout pages=f

我必须在应用程序中使用java代码动态显示一些图像。我可以使用simple
ImageView
来实现这一点。但使用“hdodenhof”。我只能使用XML文件中硬编码的静态图像。我希望从java代码中动态选择源代码。我试过一些选择。但该应用程序似乎停止工作。有什么我能做的吗?先谢谢你

这是我的
DisplayMessageActivity.java
文件

import de.hdodenhof.circleimageview.CircleImageView;

LinearLayout pages=findViewById(R.id.pages);
LayoutInflater inflater=LayoutInflater.from(this);

for(int i=0;i<6;i++){
    View view = inflater.inflate(R.layout.item,pages,false);
    CircleImageView circleImageView=findViewById(R.id.circleimage);
    circleImageView.setImageResource(R.drawable.test);
    pages.addView(view);
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <de.hdodenhof.circleimageview.CircleImageView
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/circleimage"
        android:layout_width="96dp"
        android:layout_height="96dp"
        android:src="@mipmap/ic_launcher"
        app:civ_border_width="4dp"
        app:civ_border_color="#00000000"/>
</LinearLayout>
<androidx.constraintlayout.widget.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=".DisplayMessageActivity">

    <HorizontalScrollView
        android:layout_width="0dp"
        android:layout_height="190dp"
        android:scrollbars="none"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout
            android:id="@+id/pages"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="horizontal" />
    </HorizontalScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
最后是
activity\u display\u message.xml
文件

import de.hdodenhof.circleimageview.CircleImageView;

LinearLayout pages=findViewById(R.id.pages);
LayoutInflater inflater=LayoutInflater.from(this);

for(int i=0;i<6;i++){
    View view = inflater.inflate(R.layout.item,pages,false);
    CircleImageView circleImageView=findViewById(R.id.circleimage);
    circleImageView.setImageResource(R.drawable.test);
    pages.addView(view);
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <de.hdodenhof.circleimageview.CircleImageView
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/circleimage"
        android:layout_width="96dp"
        android:layout_height="96dp"
        android:src="@mipmap/ic_launcher"
        app:civ_border_width="4dp"
        app:civ_border_color="#00000000"/>
</LinearLayout>
<androidx.constraintlayout.widget.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=".DisplayMessageActivity">

    <HorizontalScrollView
        android:layout_width="0dp"
        android:layout_height="190dp"
        android:scrollbars="none"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout
            android:id="@+id/pages"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="horizontal" />
    </HorizontalScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>

应用程序在对话框中显示以下消息,应用程序崩溃

Testingapp停止工作


我猜您会遇到空指针异常,这就是应用程序崩溃的原因。看起来您的活动将内容视图设置为
onCreate
函数中的
setContentView
中的
activity\u display\u message.xml
布局。因此,
findViewById
将仅查找该布局中的引用

CircleImageView
在不同的布局中定义,该布局在您的活动中不会膨胀,因此在您执行
findViewById(R.id.CircleImages)时-由于指令未找到您要查找的视图,您将收到一个空指针异常

您需要从for循环内部膨胀的视图中获取
CircleImageView
的布局参考。我认为以下几点应该能解决你的问题

CircleImageView circleImageView = view.findViewById(R.id.circleimage);

你好使用CircleImageview的setImageDrawable()而不是setImageResource()尝试使用或任何其他图像加载程序库加载图像。我也尝试过setImageDrawable,但它仍然停止工作@nice你能解释一下glide吗?我是android新手。更改CircleImageView CircleImageView=findViewById(R.id.circleimage);到CircleImageView CircleImageView=view.findViewById(R.id.CircleImages);谢谢雷兹·瓦伊。现在我已经理解了我代码的错误。非常感谢你的帮助。很高兴知道我能帮上忙。不客气!:)瓦伊,我有个小问题。当我仅使用普通imageview时,指令如何找到我的imageview id,尽管它位于不同的布局项.xml上?它不应该找到。不过,如果没有看到一些代码,我无法确认。不用担心!快乐编码。