1

I'm trying to get a text view to link to a site, but it won't find the id from xml in android. Any help would be great. Here's the code

It wont find the R.id.textviewlink

public class Fragment_2 extends Fragment{
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){

        return inflater.inflate(R.layout.fragment_2, null);

        TextView t2 = (TextView) getView().findViewById(R.id.textviewlink);
        t2.setMovementMethod(LinkMovementMethod.getInstance());
    }
}

XML

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingTop="10dip"
android:paddingLeft="10dip"
android:paddingRight="10dip" 
android:background="@drawable/backgroundw">

<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

            <TextView
    android:id="@+id/textviewlink"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="Free PDF Download"
    android:linksClickable="true"/>

    </LinearLayout>
    </ScrollView>
    </RelativeLayout>
mhatch
  • 4,441
  • 6
  • 36
  • 62
user2407147
  • 1,508
  • 2
  • 22
  • 40

1 Answers1

3

That is because you have already returned your inflated layout before finding the view item. There are two options.

First, the inflate() method always requires three arguments:

inflater.inflate(R.layout.file_name, container, false);

Put the following in onStart:

TextView t2 = (TextView) getView().findViewById(R.id.textviewlink);
t2.setMovementMethod(LinkMovementMethod.getInstance());

Or just use this:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
   View v = inflater.inflate(R.layout.fragment_2, container, false); //don't forget the third argument here
   TextView t2 = (TextView) v.findViewById(R.id.textviewlink);
   t2.setMovementMethod(LinkMovementMethod.getInstance());

   return v;
 }
TronicZomB
  • 8,667
  • 7
  • 35
  • 50
  • Still does not show up with both methods but what do you mean by "don't forget the third argument here" – user2407147 May 30 '13 at 20:58
  • public class Fragment_2 extends Fragment{ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ return inflater.inflate(R.layout.fragment_2, null); } public void onStart() { TextView t2 = (TextView) getView().findViewById(R.id.textviewlink); t2.setMovementMethod(LinkMovementMethod.getInstance()); } } – user2407147 May 30 '13 at 20:58
  • "eturn inflater.inflate(R.layout.fragment_2, null);" The inflate() method requires three arguments, your layout xml, the ViewGroup, and a Boolean attachToRoot. I will update my answer. – TronicZomB May 30 '13 at 21:00
  • In the code you posted in the comments (which should be edited into your question) you also never called `super.onStart()`. You need to call that first if using that method. – TronicZomB May 30 '13 at 21:04