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