简体   繁体   中英

Clickable ImageView inside Button

Whenever I set the ImageView to clickable it's crashing. Is it not possible to have a clickable ImageView inside a button or am just doing it wrong? I already set the image to be clickable.

** EDITED **

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

btPlumbers.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(HomeActivity.this);
            View layoutPlumber = getLayoutInflater().inflate(R.layout.layout_plumber, null);
            bottomSheetDialog.setContentView(layoutPlumber);

            BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from((View) layoutPlumber.getParent());
            bottomSheetBehavior.setPeekHeight(
                    (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 400, getResources().getDisplayMetrics())
            );

            ImageView imgPTap = (ImageView) findViewById(R.id.imageview_pTap);

            imgPTap.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //
                }
            });

            bottomSheetDialog.show();
        }
    });

Here's the error:

 E/UncaughtException: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                     at com.fixitph.client.HomeActivity$1.onClick(HomeActivity.java:189)
                     at android.view.View.performClick(View.java:4781)
                     at android.view.View$PerformClick.run(View.java:19900)
                     at android.os.Handler.handleCallback(Handler.java:739)
                     at android.os.Handler.dispatchMessage(Handler.java:95)
                     at android.os.Looper.loop(Looper.java:159)
                     at android.app.ActivityThread.main(ActivityThread.java:5541)
                     at java.lang.reflect.Method.invoke(Native Method)
                     at java.lang.reflect.Method.invoke(Method.java:372)
                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:975)
                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:770)

XML file

<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">
    <ImageView
        android:id="@+id/imageview_pTap"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="0"
        android:layout_gravity="center"
        android:layout_row="0"
        android:clickable="true"
        android:padding="15dp"
        app:srcCompat="@drawable/ic_plumber_64dp" />
</android.support.constraint.ConstraintLayout>

EDITED WITH XML

Either btPlumbers or imgPtap is null. Try assigning them a value before calling setOnClickListener()

You can do it this way:

btPlumbers = findViewById(R.id.<your_view_id>);
imgPtap = findViewById(R.id.<your_view_id>);

Instead of

  ImageView imgPTap = (ImageView) findViewById(R.id.imageview_pTap);

try this in your activity .

  ImageView imgPTap = (ImageView) layoutPlumber.findViewById(R.id.imageview_pTap);

( still i flag this question as duplicate )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM