Android 如何称呼孩子';已具有父级的指定子级的父级
代码如下:Android 如何称呼孩子';已具有父级的指定子级的父级,android,Android,代码如下: public class HelpDetailsFragment extends Fragment { private static final String TAG = "MeMoGame"; public static HelpDetailsFragment newInstance(int index) { HelpDetailsFragment detailFragment = new HelpDetailsFragment(); Bundle bundleA
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):原因:java.lang.IllegalStateException:指定的子级已具有父级。必须首先对子级的父级调用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()
我们仍然非常欢迎您的解释 这里的问题是您正在返回容器--
返回容器代码>您应该返回mView在您的情况下,容器是活动布局中片段的实际容器哪一行导致错误?您想在这里做什么?消息中没有行号。您如何查看错误消息?您正在使用Eclipse LogCat视图吗?如果是,行号将在stacktrace的后面。查找类似“at Xxx.Yyy()”的行,其中包含类的名称和导致问题的方法。源文件名和行号列在括号中。在Eclipse中,您可以双击LogCat直接跳转到引用的行号;是罪魁祸首。有什么问题吗?请编辑您的问题并添加包含该行代码的方法。