我在我的应用程序中使用了``滑动''选项卡,我有2个片段和一个视图分页器。
类别片段
public class Categories extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
if (cat == null) {
cat = inflater.inflate(R.layout.categories, container, false);
} else {
((ViewGroup) cat.getParent()).removeAllViews();
}
catData = new ArrayList<>();
if(mRecyclerView == null) {
mRecyclerView = (RecyclerView) cat.findViewById(R.id.recyclerView);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
}else{
((ViewGroup) mRecyclerView.getParent()).removeAllViews();
}
return mRecyclerView;
}
Handler handler = new Handler();
Runnable mStatusChecker = new Runnable() {
@Override
public void run() {
GetCategoriesTask access = new GetCategoriesTask();
access.execute();
//this function can change value of mInterval.
handler.postDelayed(mStatusChecker, 10000);
}
};
private class GetCategoriesTask extends AsyncTask<Void, Void, ArrayList<CategoryData>> {
@Override
protected void onPreExecute() {
}
//task is running in background
@Override
protected ArrayList<CategoryData> doInBackground(Void... arg0) {
ServiceHandler sh = new ServiceHandler();
String baseAddress = "url";
String jsonStr = sh.makeServiceCall(ServiceHandler.GET,baseAddress);
if(jsonStr != null){
try{
JSONObject jsonObj = new JSONObject(jsonStr);
JSONArray data = jsonObj.getJSONArray("data");
catData.clear();
for(int i = 0;i < data.length();i++){
JSONObject c = data.getJSONObject(i);
String categoryName = c.getString("category");
catData.add(new CategoryData(categoryName));
}
return catData;
}catch (JSONException ex){
ex.printStackTrace();
}
}
Log.d("Response: ", "> " + jsonStr);
return null;
}
//after executing background task, this method will execute.
@Override
protected void onPostExecute(ArrayList<CategoryData> data) {
if(!notifier) {
adapter = new CategoryRecyclerViewAdapter(getContext(), data);
mRecyclerView.setAdapter(adapter);
notifier = true;
}
else{
adapter.notifyDataSetChanged();
Toast.makeText(getContext(), "changed", Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onResume() {
super.onResume();
mStatusChecker.run();
}
@Override
public void onSaveInstanceState(final Bundle outState) {
// super.onSaveInstanceState(outState);
}
@Override
public void onPause() {
super.onPause();
handler.removeCallbacks(mStatusChecker);
}
@Override
public void onDestroy() {
super.onDestroy();
handler.removeCallbacks(mStatusChecker);
}
@Override
public void onStop() {
super.onStop();
handler.removeCallbacks(mStatusChecker);
}}
发现片段
我只有oncreate方法。
public class Discover extends Fragment{
public View v;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
if (v == null) {
v = inflater.inflate(R.layout.categories, container, false);
} else {
((ViewGroup) v.getParent()).removeAllViews();
}
return v;
}
问题是,当我更改应用程序的方向时,它会出现异常并显示消息“不幸的是,酶已停止。”当我单击确定按钮时,应用程序再次重新启动并正常工作。
日志猫
08-26 10:17:25.954 4452-4452/com.ezycity.seneru.ezycity E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.ezycity.seneru.ezycity, PID: 4452
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ezycity.seneru.ezycity/com.ezycity.seneru.ezycity.MainActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3950)
at android.app.ActivityThread.access$900(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1309)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: 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:3936)
at android.view.ViewGroup.addView(ViewGroup.java:3786)
at android.support.v4.view.ViewPager.addView(ViewPager.java:1342)
at android.view.ViewGroup.addView(ViewGroup.java:3727)
at android.view.ViewGroup.addView(ViewGroup.java:3700)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1032)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1197)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1179)
at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1991)
at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:165)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:507)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1236)
at android.app.Activity.performStart(Activity.java:6006)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2288)
那么我该如何解决这个问题呢?
最佳答案
因此,让我们看一下这段代码:
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
if (cat == null) {
cat = inflater.inflate(R.layout.categories, container, false);
} else {
( (ViewGroup) cat.getParent()).removeAllViews();
}
catData = new ArrayList<>();
if(mRecyclerView == null) {
mRecyclerView = (RecyclerView) cat.findViewById(R.id.recyclerView);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
}else{
((ViewGroup) mRecyclerView.getParent()).removeAllViews();
}
return mRecyclerView;
}
最初,由于最后一个参数,您正在膨胀未附加到根目录的
cat
。那是对的。然后,您正在
mRecyclerView
中搜索cat
。这也意味着mRecyclerView
的父级是cat。之后,您将返回
mRecyclerView
。因此,寻呼机试图将
mRecyclerView
添加到页面中,但是mRecyclerView
已经有一个父级(猫)。因此,此片段的解决方案可能是:
代替:
return mRecyclerView;
采用:
return cat;