将片段链接到 Activity 错误
Linking Fragment to Activity error
我正在尝试将片段附加到 activity,我已经学习了多个教程,但我的代码似乎与教程中的代码完全相同(适用于我的应用程序),但我找不到我的代码在哪里错误是。
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())”处收到错误,它说:"cant resolve method add(int, package.CommentsFragment)".
这是我要调用片段的 class 的 header:
public class CommentsActivity extends FragmentActivity
这是片段本身之一:
public class CommentsFragment extends Fragment
如果您需要更多信息,请告诉我。非常感谢!
真的不需要扩展 FragmentActivity,扩展 Activity 就够了。
看这个例子:
评论Activity:
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();
}
...
}
评论片段:
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
我正在尝试将片段附加到 activity,我已经学习了多个教程,但我的代码似乎与教程中的代码完全相同(适用于我的应用程序),但我找不到我的代码在哪里错误是。
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())”处收到错误,它说:"cant resolve method add(int, package.CommentsFragment)".
这是我要调用片段的 class 的 header:
public class CommentsActivity extends FragmentActivity
这是片段本身之一:
public class CommentsFragment extends Fragment
如果您需要更多信息,请告诉我。非常感谢!
真的不需要扩展 FragmentActivity,扩展 Activity 就够了。
看这个例子:
评论Activity:
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();
}
...
}
评论片段:
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