gpt4 book ai didi

Android:带有 RecyclerView 方向更改错误的 fragment

转载 作者:行者123 更新时间:2023-11-29 01:29:15 32 4
gpt4 key购买 nike

我在我的应用程序中使用滑动选项卡,我有 2 个带有 View 寻呼机的 fragment 。 fragment 内还有一个回收器 View 。

类别 fragment

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);
}}

发现 fragment

我只有 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;
}

问题是,当我改变应用程序的方向时,它给出了一个异常并显示消息“不幸的是 ezycity 已停止”。当我单击确定按钮时,应用程序再次重新启动并正常工作。

日志

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。这是正确的。

然后你在cat中搜索mRecyclerView。这也意味着 mRecyclerView 的父级是 cat。

之后你将返回 mRecyclerView。因此寻呼机试图将 mRecyclerView 添加到页面,但是 mRecyclerView 已经有一个父级 (cat)。


所以这个 fragment 的解决方案可能是:

代替:

 return mRecyclerView;

使用:

return cat;

关于Android:带有 RecyclerView 方向更改错误的 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32218227/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com