我试图将一个片段附加到一个活动,我已经跟着多个教程,我的代码看起来完全相同(适应我的应用程序)而不是教程中的那些,我无法找到我的错误。
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.view.View;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_comments);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.id_layout_comments, new CommentsFragment())
.commit();
}
我在“.add(R.id.id_layout_comments,new CommentsFragment())”中得到错误,它说:“无法解析方法add(int,package.CommentsFragment)”。
这是我要调用片段的类的标题:
public class CommentsActivity extends FragmentActivity
这是片段本身之一:
public class CommentsFragment extends Fragment
如果您需要更多信息,请与我们联系。非常感谢先进!
答案 0 :(得分:0)
真的你不需要扩展FragmentActivity,只需扩展Activity即可。
看看这个例子:
CommentsActivity:
public class CommentsActivity extends FragmentActivity {
...
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_comments);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.id_layout_comments, new CommentsFragment())
.commit();
}
...
}
CommentsFragment:
public class CommentsFragment extends Fragment{
...
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.your_resource, container, false);
return view;
}
...
}
就是这样:D