Java setContentView(int)不工作
我正在努力完成一个教程,但遇到了一些困难。教程 实现一个类SingleFragmentActivity来承载一个片段(附带xml文件) 我的问题是在这个SingleFragmentActivity.java类中,有一个对setContentView(int)的调用抛出了一个错误,它说它找不到方法。setContentView(int)不是片段类的一部分吗Java setContentView(int)不工作,java,android,Java,Android,我正在努力完成一个教程,但遇到了一些困难。教程 实现一个类SingleFragmentActivity来承载一个片段(附带xml文件) 我的问题是在这个SingleFragmentActivity.java类中,有一个对setContentView(int)的调用抛出了一个错误,它说它找不到方法。setContentView(int)不是片段类的一部分吗 import android.app.Fragment; 导入android.app.FragmentManager; 导入android.
import android.app.Fragment;
导入android.app.FragmentManager;
导入android.os.Bundle
public abstract class SingleFragmentActivity extends Fragment {
protected abstract Fragment createFragment();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment);
FragmentManager manager = getFragmentManager();
Fragment fragment = manager.findFragmentById(R.id.fragmentContainer);
if (fragment == null) {
fragment = createFragment();
manager.beginTransaction()
.add(R.id.fragmentContainer, fragment)
.commit();
}
}
}
.app.Fragment
的视图已在onCreateView
中初始化。基本上,您需要使用充气器充气视图,并在onCreateView
中将其作为根视图返回
以及链接中的示例
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
// We have different layouts, and in one of them this
// fragment's containing frame doesn't exist. The fragment
// may still be created from its saved state, but there is
// no reason to try to create its view hierarchy because it
// won't be displayed. Note this is not needed -- we could
// just run the code below, where we would create and return
// the view hierarchy; it would just never be used.
return null;
}
ScrollView scroller = new ScrollView(getActivity());
TextView text = new TextView(getActivity());
int padding = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
4, getActivity().getResources().getDisplayMetrics());
text.setPadding(padding, padding, padding, padding);
scroller.addView(text);
text.setText(Shakespeare.DIALOGUE[getShownIndex()]);
return scroller;
}
此外,如果要使用
setContentView
,请使用支持库中的FragmentActivity
。.app.Fragment
的视图在onCreateView
中初始化。基本上,您需要使用充气器充气,以根视图的形式返回onCreateView
以及链接中的示例
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
// We have different layouts, and in one of them this
// fragment's containing frame doesn't exist. The fragment
// may still be created from its saved state, but there is
// no reason to try to create its view hierarchy because it
// won't be displayed. Note this is not needed -- we could
// just run the code below, where we would create and return
// the view hierarchy; it would just never be used.
return null;
}
ScrollView scroller = new ScrollView(getActivity());
TextView text = new TextView(getActivity());
int padding = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
4, getActivity().getResources().getDisplayMetrics());
text.setPadding(padding, padding, padding, padding);
scroller.addView(text);
text.setText(Shakespeare.DIALOGUE[getShownIndex()]);
return scroller;
}
此外,如果要使用
setContentView
,请使用支持库中的FragmentActivity
。.app.Fragment
的视图在onCreateView
中初始化。基本上,您需要使用充气器充气,以根视图的形式返回onCreateView
以及链接中的示例
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
// We have different layouts, and in one of them this
// fragment's containing frame doesn't exist. The fragment
// may still be created from its saved state, but there is
// no reason to try to create its view hierarchy because it
// won't be displayed. Note this is not needed -- we could
// just run the code below, where we would create and return
// the view hierarchy; it would just never be used.
return null;
}
ScrollView scroller = new ScrollView(getActivity());
TextView text = new TextView(getActivity());
int padding = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
4, getActivity().getResources().getDisplayMetrics());
text.setPadding(padding, padding, padding, padding);
scroller.addView(text);
text.setText(Shakespeare.DIALOGUE[getShownIndex()]);
return scroller;
}
此外,如果要使用
setContentView
,请使用支持库中的FragmentActivity
。.app.Fragment
的视图在onCreateView
中初始化。基本上,您需要使用充气器充气,以根视图的形式返回onCreateView
以及链接中的示例
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
// We have different layouts, and in one of them this
// fragment's containing frame doesn't exist. The fragment
// may still be created from its saved state, but there is
// no reason to try to create its view hierarchy because it
// won't be displayed. Note this is not needed -- we could
// just run the code below, where we would create and return
// the view hierarchy; it would just never be used.
return null;
}
ScrollView scroller = new ScrollView(getActivity());
TextView text = new TextView(getActivity());
int padding = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
4, getActivity().getResources().getDisplayMetrics());
text.setPadding(padding, padding, padding, padding);
scroller.addView(text);
text.setText(Shakespeare.DIALOGUE[getShownIndex()]);
return scroller;
}
此外,如果要使用
setContentView
,请使用支持库中的FragmentActivity
。这样做是错误的。setContentView(int)适用于Activity
,但您的类正在扩展Fragment,因此需要膨胀xml并在onCreateView
中返回视图
例如:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout. activity_fragment, container, false);
// Do some Initialization
return view;
}
还有一点需要注意的是,不要用Activity来命名你的片段类。这会让你感到困惑:)你做得不对。setContentView(int)将用于
活动
,但您的类正在扩展片段,因此您需要膨胀xml并在onCreateView中返回视图
例如:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout. activity_fragment, container, false);
// Do some Initialization
return view;
}
请注意,不要用Activity命名片段类。这会让你困惑:)你做得不对。setContentView(int)将用于
活动
,但您的类正在扩展片段,因此您需要膨胀xml并在onCreateView中返回视图
例如:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout. activity_fragment, container, false);
// Do some Initialization
return view;
}
请注意,不要用Activity命名片段类。这会让你困惑:)你做得不对。setContentView(int)将用于
活动
,但您的类正在扩展片段,因此您需要膨胀xml并在onCreateView中返回视图
例如:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout. activity_fragment, container, false);
// Do some Initialization
return view;
}
请注意,不要用Activity命名片段类。这会让您感到困惑:)
setContentView
不是片段的成员。要为片段膨胀视图,膨胀后需要将视图返回到onCreateView
:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_layout, container, false);
}
setContentView
不是Fragment
的成员。要为片段膨胀视图,膨胀后需要将视图返回到onCreateView
:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_layout, container, false);
}
setContentView
不是Fragment
的成员。要为片段膨胀视图,膨胀后需要将视图返回到onCreateView
:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_layout, container, false);
}
setContentView
不是Fragment
的成员。要为片段膨胀视图,膨胀后需要将视图返回到onCreateView
:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_layout, container, false);
}
Fragment使用onCreateView()并充气而不是onCreate(),onContentView()专门用于活动Fragment使用onCreateView()并充气而不是onCreate(),onContentView()专门用于活动Fragment使用onCreateView()并充气而不是onCreate()而专门针对活动的onContentView()片段使用onCreateView()并充气,而不是专门针对活动的onCreate()和onContentView()