0
這是代碼:如何調用孩子的父母爲已經有母這個指定的子
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()
的解釋仍然會非常歡迎!
哪條線路會導致錯誤?你想在這裏做什麼? –
消息中沒有行號。 – PageMaker
你是如何查看錯誤信息的?你在使用Eclipse LogCat視圖嗎?如果是這樣,行號將在堆棧跟蹤中稍後。找到類似於「在Xxx.Yyy()」的行,並使用您的類的名稱和導致問題的方法。源文件名和行號在括號中列出。在Eclipse中,您可以雙擊LogCat直接跳到引用的行號。 –