Android: the findViewById (int) method is undefined for the type first (Fragment)

advertisements

I'm new to fragments and i'm developing an application using Swipe view with tabs. My aim here is to get a textview display texts stored in the string-array and changes whenever the application is restarted. But there seems to be a problem when i'm using findViewById.

Code:

First.java

import java.util.Random;

import android.support.v4.app.Fragment;
import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class first extends Fragment{

String[] strArr;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    View rootView = inflater.inflate(R.layout.first_xml, container, false);
    strArr = getResources().getStringArray(R.array.quote);
             //quote is the name given to the string array

    return rootView;
}

@Override
public void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    refreshTV();
}

void refreshTV(){
        TextView tv = (TextView)findViewById(R.id.text1);
        Random ran = new Random();
        int c = ran.nextInt(strArr.length);
        tv.setText(strArr[c]);

    }

}

2.first_xml.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#fa6a6a" >

<TextView android:layout_width="fill_parent"
    android:id="@+id/text1"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="@array/quote"
    android:textSize="40dp"
    android:layout_centerInParent="true"/>

</RelativeLayout>

Any help will be appreciated. Please Let me know if anything i mentioned is not clear enough. Thank you !


The Fragment class does not have the findViewById(...) method, so you have to get your views from your rootView or your Activity. I would suggest making your TextView a member of your Fragment, retrieving it from your rootView, and referencing it as needed.

public class first extends Fragment {

String[] strArr;
TextView tv;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    View rootView = inflater.inflate(R.layout.first_xml, container, false);
    tv = rootView.findViewById(R.id.text1);
    strArr = getResources().getStringArray(R.array.quote);
             //quote is the name given to the string array

    return rootView;
}

@Override
public void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    refreshTV();
}

void refreshTV(){
        Random ran = new Random();
        int c = ran.nextInt(strArr.length);
        tv.setText(strArr[c]);
    }
}

(Edited to remove redundant calls to findViewById.)