我有一个fragment
,其中有一个我们从服务器获取的列表。如果没有使用“再试一次”按钮的互联网,我想显示另一个屏幕。直到现在我已经创建了布局并在互联网出现时获取列表。但是当没有互联网的时候,我试着在fragment
中添加我的无互联网屏幕,但不知怎的,这给我一个错误。
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at android.view.ViewGroup.addViewInner(ViewGroup.java:4309)
at android.view.ViewGroup.addView(ViewGroup.java:4145)
at android.view.ViewGroup.addView(ViewGroup.java:4086)
at android.view.ViewGroup.addView(ViewGroup.java:4059)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1425)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1740)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1809)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:799)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2580)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2367)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2322)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2229)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:700)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5461)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
这是我的片段布局:
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/challenge_landing"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/challengeLandingRV"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</GridLayout>
片段:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.challenge_landing_fragment, container, false);
setUI(view);
initializeChallengeList();
mChallengeManager= ChallengeManager.singleton();
fetchChallengesFromNetwork();
// setupPresenter(view);
setHasOptionsMenu(true);
return view;
}
private void setUI(View view) {
progressDialog = CustomProgressDialog.createDialog(getContext(),getResources().getString(R.string.loading));
view.findViewById(R.id.challengeLandingRV);
mChallengeLandingContainer = (GridLayout) view.findViewById(R.id.challenge_landing);
}
private void initializeChallengeList() {
mMy_challenges_Model = new ArrayList<Challenge>();
mOpen_challenges_Model = new ArrayList<Challenge>();
mClose_challenges_Model = new ArrayList<Challenge>();
mAll_Challenges_model = new ArrayList<Challenge>();
mOrderedChallegeList=new ArrayList<Challenge>();
}
private void fetchChallengesFromNetwork() {
JSONObject param = new JSONObject();
try {
param.put("timeline", "all");
param.put("include_rank",true);
param.put("registered_flag", "all");
Log.d("FetchChallengereq ", "Req "+param.toString());
} catch (JSONException e) {
e.printStackTrace();
}
if (NetworkUtils.isConnectedToNetwork(getContext())) {
progressDialog.show();
mChallengeManager.fetchChallengeList(param, new ChallengeManager.ChallengeListResponseListener() {
@Override
public void onRequestCompleted(int statusCode, List<Challenge> challengeList, String statusMessage,int total_challenges) {
Log.d("FetchChallenge ", "Response "+statusCode);
// if (mProgressBar != null) mProgressBar.dismiss();
progressDialog.dismiss();
if (statusCode == 200) {
if (statusMessage.equals("Challenges successfully fetched")) {
mAll_Challenges_model = challengeList;
separateChallenges(mAll_Challenges_model);
Log.d("All Challenges list", "Response "+mAll_Challenges_model.toString());
populateData(mChallenge_RecycleView, mOrderedChallegeList);//for my challenges
}
}
else {
Toast.makeText(getContext(),statusMessage,Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCurrentChallengesRequestCompleted(int statusCode, List<Challenge> publicchallengeList, List<Challenge> corporatechallengeList, String statusMessage, int total_challenges) {
}
});
} else {
showNoInternetScreen();
progressDialog.dismiss();
//Toast.makeText(getContext(), getString(R.string.network_connection), Toast.LENGTH_LONG).show();
}
}
private void showNoInternetScreen() {
final GridLayout mChallengeLandingContainer = (GridLayout)view.findViewById(R.id.challenge_landing);
if (null != mChallengeLandingContainer) {
//mChallengeLandingContainer.removeAllViews();
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null) {
parent.removeView(view);
}
view = View.inflate(view.getContext(), R.layout.content_list_fragment_no_internet, null);
mChallengeLandingContainer.addView(view);
TextView mRetryButton = (TextView) view.findViewById(R.id.retryButton);
mRetryButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (com.android.mobiefit.sdk.network.NetworkUtils.isConnectedToNetwork(getActivity())) {
// mChallengeLandingContainer.removeAllViews();
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null) {
parent.removeView(view);
}
view = View.inflate(view.getContext(), R.layout.challenge_landing_fragment, null);
mChallengeLandingContainer.addView(view);
setUI(view);
initializeChallengeList();
mChallengeManager= ChallengeManager.singleton();
fetchChallengesFromNetwork();
// setupPresenter(view);
setHasOptionsMenu(true);
} else {
}
}
});
}
}
所以我有点困惑。如何使用removeView()以及为什么我会收到此错误?
答案 0 :(得分:0)
如何使用removeView()
在你的onCreateView
中 if (view != null) {
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null) {
parent.removeView(view);
}
}
try {
view = inflater.inflate(R.layout.challenge_landing_fragment, container, false);
} catch (InflateException e) {
}
为什么我收到此错误
您正在实例化动态布局并向其添加子项