-1

I'm trying make a clickable zone on a imageView in a fragment. I have the flowing error :

java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference

Does anybody can help me ?

code of my fragment :

RelativeLayout rl = (RelativeLayout) this.getView().findViewById(R.id.clikeable1);
        rl.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity().getApplicationContext(), "toto", Toast.LENGTH_SHORT).show();
            }
        });

code of the relativelayout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"


    <TextView

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="290dp"
        android:layout_marginTop="110dp"
        android:text="Entrez le code étape" />
   <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/password_keyboard" />

    <ImageView
        android:id="@+id/passwordKo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="295dp"
        android:layout_marginTop="170dp"
        app:srcCompat="@android:drawable/presence_invisible" />

    <ImageView
        android:id="@+id/passwordOk"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="405dp"
        android:layout_marginTop="170dp"
        app:srcCompat="@android:drawable/presence_invisible" />

    <EditText
        android:id="@+id/passwordEtape"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="false"
        android:layout_marginTop="35dp"
        android:ems="3"
        android:inputType="numberSigned"
        android:maxLength="4" />


    <RelativeLayout
        android:layout_width="350dp"
        android:layout_height="350dp"
        android:id="@+id/clikeable1"
        android:onClick="action1"
        android:layout_alignLeft="@+id/imageView2"
        android:layout_alignBottom="@+id/imageView2"/>

</RelativeLayout>

Thank you. Max

Progman
  • 16,827
  • 6
  • 33
  • 48
max
  • 49
  • 2
  • 4
  • `this.getView()` is `null`. If you're in an `Activity` class, you should be able to call `findViewById(R.id.clikeable1);` directly. – TimonNetherlands Dec 07 '20 at 15:43
  • Hello TimonNetherlands, I 'm not in an Activity but in a dialogfragment. findViewById(R.id.clikeable1); Thanks a lot – max Dec 11 '20 at 07:27
  • If the layout you pasted above is the layout you're using in your `Activity`, then you can get the `RelativeLayout` with id 'clikeable1' like this: `getActivity().findViewById( R.id.clikeable1 )`; – TimonNetherlands Dec 11 '20 at 10:10
  • I'm sorry, I'm new in Android an the Activity and Fragment concept....In my MainActivity I open a DialogFragment where I'm using the a RelativeLayout. When I tried to use getActivity().findViewById( R.id.clikeable1 ), I have the following error : java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RelativeLayout.setOnClickListener(android.view.View$OnClickListener)' on a null object reference – max Dec 14 '20 at 08:24
  • Sorry for confusing you. I missed the part where you said you're in a `DialogFragment`. My previous comment is wrong. I've added an answer that should work. – TimonNetherlands Dec 15 '20 at 08:41

1 Answers1

0

There must be a onCreateView method in your DialogFragment class.

In that method you're inflating the RelativeLayout, like so:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View parent = inflater.inflate(R.layout.name_of_your_layout, container, false);
    
    return parent;
}

Now, in order to find the 'clikeable1' RelativeLayout, you will have to look for it in its parent. The method above then will look something like:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View parent = inflater.inflate(R.layout.name_of_your_layout, container, false);
    RelativeLayout rl = (RelativeLayout) parent.findViewById(R.id.clikeable1);
    rl.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            Toast.makeText(getActivity().getApplicationContext(), "toto", Toast.LENGTH_SHORT).show();
        }
    });

    return parent;
}
TimonNetherlands
  • 1,033
  • 1
  • 6
  • 6
  • 1
    Thank you TimonNetherlands. It's working fine. In fact, I was using an AlertDialog with onCreateDialog instead of onCreateView. Seems that the RelativeLayout onClickListener is not compatible with an AlertDialog. – max Dec 22 '20 at 07:13