简体   繁体   English

对于类型PostDetail(片段),未定义方法findViewById(int)

[英]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.

相关问题 对于new Runnable(){}类型,未定义方法findViewById(int) - The method findViewById(int) is undefined for the type new Runnable(){} 方法findViewById(int)未定义类型R.layout - The method findViewById(int) is undefined for the type R.layout 适配器的类型和控制器也未定义方法findViewById(int) - The method findViewById(int) is undefined for the type and the controller of the adapter too 对于Jsoup解析器,使用AsyncTask为类型定义了方法findViewById(int) - The method findViewById(int) is undefined for the type using AsyncTask for Jsoup Parser 无法解析方法 findViewByID(int) 片段 java - Cannot resolve method findViewByID(int) fragment java 无法解析@Nullable片段中的方法'findViewById(int)' - Cannot resolve method 'findViewById(int)' in @Nullable fragment Android SDK:类型未定义的findViewById(int) - Android SDK: findViewById(int) undefined for type Android-未为Fragment类型定义方法“…” - Android - The method “…” is undefined for the type Fragment 未为Fragment类型定义方法getSupportFragmentManager() - The method getSupportFragmentManager() is undefined for the type Fragment 无法解析方法findViewById(“ int”) - Cannot resolve method findViewById(“int”)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM