The findViewById (int) method is not defined for the PostDetail type (Fragment)

advertisements

I have code in ArticleActivity (Activity)

Trying implement to PostDetail (Fragment) findViewById(R.id.like).setOnClickListener( new ResultLikeListener( this,(TextView)findViewById(R.id.like), res ));

Please can you help me.

public class PostDetail extends Fragment{

    private int mId;
    TextView mTitle;
    WebView mWebView;
//  TextView mBody;

     private Tracker tracker;

        @Override
        public void onCreate(final Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);

        }

    }

.............
}


You have to @overide the OnCrateView Method of fragment to get the id's of your widget like textview or edittext, whatever you want to. In oncreate view you have to inflate your layout as you do in activity, as you set the setcontentview, but here in fragment you have to inflate your layoutview. The code is something like :

 public class PostDetail extends Fragment {
   TextView mTitle;
    @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        View view = (ViewGroup) inflater.inflate(R.layout.YOUR_LAYOUT_VIEW,   null);
        mTitle= (TextView ) _view.findViewById(R.id.YOUR_TEXTVIEW_ID);
       return view;
     }

}