Vedo
Vedo

Reputation: 643

How to connect fragment and layout

I'm trying to set on click method in my project but, I can't connect my layout to the given fragment. When you press the icon of Instagram it should open persons Instagram but it only crashes.

Fragment code:

 public class SupportFragment extends Fragment {

    @Nullable
    ImageView vedoIg;
  public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_support,container,false);
        vedoIg = v.findViewById(R.id.vedo_ig);
        vedoIg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Uri uri = Uri.parse("http://instagram.com/v");
                Intent instagram = new Intent(Intent.ACTION_VIEW, uri);
                instagram.setPackage("com.instagram.android");
                try {
                    startActivity(instagram);
                }
                catch (Exception e){
                    startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("http://instagram.com/_u/v")));
                }
            }
        });
        return inflater.inflate(R.layout.fragment_support,container,false);
    }

   }

Layout folder:

<?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="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:background="@drawable/pozadina"
    tools:context=".SupportFragment">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="40dp"
    android:text="Contact us: "
    android:textColor="#ffffff"
    android:textSize="30dp"
    android:textStyle="bold" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="90dp"
    android:text="Vedo"
    android:textColor="#ffffff"
    android:textSize="30dp"
    android:textStyle="bold" />


<ImageView
    android:id="@+id/vedo_ig"
    android:layout_width="72dp"
    android:layout_height="48dp"
    android:layout_marginLeft="40dp"
    android:layout_marginTop="150dp"
    app:srcCompat="@drawable/w_instagram"/>

Any tip would mean the world. Thanks in advance :D <3

Upvotes: 0

Views: 772

Answers (1)

Ben P.
Ben P.

Reputation: 54204

This bit of code is problematic:

public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_support,container,false);
    vedoIg = v.findViewById(R.id.vedo_ig);
    // ...
    return inflater.inflate(R.layout.fragment_support,container,false);
}

In the first line, you're inflating one copy of a view... but then you return a different copy of that same view. This means that what the user sees won't match the view you've been searching in and setting click listeners on.

Change the last line to return v to fix this issue.

Upvotes: 2

Related Questions