App Stopped Working On setImageResource of CircleImageView

Dipto Roy :

I have to show some images dynamically using java code in app horizontally. I can do this using simple ImageView. But using CircleImageView by "hdodenhof". I am able to use only static image hardcoded in the XML file. I want the source to be dynamically selected from my java code. I have tried some option. But the app seems to stop working. Is there anything I can do? Thank you in advance.

Here is my DisplayMessageActivity.java file

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);
}

And the item.xml file

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

And finally the activity_display_message.xml file

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

The application shows the following message in the dialog and the app crashes.

Testingapp stopped working

Reaz Murshed :

I guess you are getting a null pointer exception and that's why the application is crashing. Looks like your activity set the content view to the activity_display_message.xml layout in your setContentView in your onCreate function. Hence the findViewById will only find the references from that layout only.

The CircleImageView is defined in a different layout which is not inflated in your activity and hence while you are doing findViewById(R.id.circleimage); - you are getting a null pointer exception as the instruction did not find the view that you are looking for.

You need to take the layout reference of your CircleImageView from the view that you have inflated inside your for loop. I think the following should solve your problem.

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=145289&siteId=1