这是代码:
public class HelpDetailsFragment extends Fragment
{
private static final String TAG = "MeMoGame";
public static HelpDetailsFragment newInstance(int index)
{
HelpDetailsFragment detailFragment = new HelpDetailsFragment();
Bundle bundleArgs = new Bundle();
bundleArgs.putInt("index", index);
detailFragment.setArguments(bundleArgs);
return detailFragment;
} // newInstance()
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState)
{
if(container == null)
{
Log.i(TAG, "Different layouts and in one this fragment's containing frame does not exist.");
return null;
} else {
// I checked that container is NOT null
Log.i(TAG, "This is the parent view that the fragment's UI should be attached to.");
}
View mView = new View(getActivity());
container.addView(mView);
return container;
}
出现这个错误信息:AndroidRuntime(785): Caused by: java.lang.IllegalStateException: The specified child has a parent. 您必须首先在孩子的父母上调用 removeView()。
可以/有人请解释我做错了什么吗?
当我做:
View mView = new View(getActivity());
TextView text = (TextView) new TextView()
mView.addView(text);
return mView;
我收到相同的错误消息。
我的救赎就在这里:
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState)
{
Context context = getActivity();
FrameLayout frameLayout = new FrameLayout(context);
int height = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 50, getActivity()
.getResources().getDisplayMetrics());
int padding = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 10, context
.getResources().getDisplayMetrics());
// set the header
TextView headText = new TextView(context);
headText.setHeight(height);
headText.setPadding(padding, 0, 0, 0);
headText.setTextAppearance(context, R.style.details_Header);
headText.setText(HelpScreenData.HELP_HEADERS[getCurrentIndex()]);
frameLayout.addView(headText);
ScrollView scroller = new ScrollView(context);
// set detail text
TextView detailText = new TextView(context);
detailText.setPadding(padding, padding + height, padding, padding);
detailText.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
detailText.setText(HelpScreenData.HELP_DETAILS[getCurrentIndex()]);
scroller.addView(detailText);
frameLayout.addView(scroller);
return frameLayout;
} // onCreateView()
一个解释仍然非常受欢迎!