[英]The method findViewById(int) is undefined for the type PostDetail (Fragment)
I have code in ArticleActivity (Activity) 我在ArticleActivity(活动)中有代码
Trying implement to PostDetail (Fragment) findViewById(R.id.like).setOnClickListener( new ResultLikeListener( this,(TextView)findViewById(R.id.like), res )); 尝试将工具实现为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. 您必须
@overide
片段的OnCrateView方法,以获取小部件的ID(例如textview或edittext),无论您想要什么。 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. 在oncreate视图中,您必须像设置活动一样在设置setcontentview时对布局进行膨胀,但是在此处的片段中,您必须对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;
}
}
You will need to use it like this: 您将需要像这样使用它:
View rootView = inflater.inflate(R.layout.article_fragnment, this, false);
TextView txtView = rootView.findViewById(R.id.like).setOnClickListener( new ResultLikeListener( this,(TextView)findViewById(R.id.like), res ));
您需要从其onCreate函数获取片段的rootView。
rootView.findViewById(R.id.like).setOnClickListener( new ResultLikeListener( this,(TextView)findViewById(R.id.like), res ));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.