我在我的应用程序中使用了Sliding选项卡,我有2个带有view pager的片段。在片段中还有一个回收器视图。
类别片段
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;
}
问题是,当我更改应用程序的方向时,它会给出异常并显示消息&#34;不幸的是,ezycity已停止&#34;。当我单击确定按钮时,应用程序再次重新启动并正常工作。
logcat的
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)
那么我如何解决这个问题?
答案 0 :(得分:1)
让我们来看看这段代码:
class PrescriptionTrendOverview(viewsets.ModelViewSet):
queryset = Prescription.objects.all()
serializer_class = PrescriptionSerializer
def get_queryset(self):
queryset = super(PrescriptionTrendOverview, self).get_queryset()
doctor_name = self.request.query_params.get('doctor', None)
pharmacy_name = self.request.query_params.get('pharmacy', None)
start_date = self.request.query_params.get('start_date', None)
end_date = self.request.query_params.get('end_date', None)
if doctor_name and pharmacy_name and start_date and end_date:
# You can write the query set this way:
queryset = queryset.filter(Q(doctor__name=doctor_name)
& Q(pharmacy__name=pharmacy_name)
& Q(date_prescribed__gte=start_date)
& Q(date_prescribed__lte=end_date))
# or this way
queryset = queryset.filter(doctor__name = doctor_name,
pharmacy__name = pharmacy_name,
ate_prescribed__gte=start_date,
date_prescribed__lte=end_date)
return queryset
首先,由于最后一个参数,您正在膨胀@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
的父级是cat。
之后您将返回mRecyclerView
。
因此,寻呼机正在尝试将mRecyclerView
添加到页面,但mRecyclerView
已经有父(cat)。
所以此片段的解决方案可能是:
而不是:
mRecyclerView
使用:
return mRecyclerView;