有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

更改方向上的片段布局时addView()中出现java错误

我在addView()中遇到一个错误。屏幕从纵向开始。我把它改为横向,现在当我试图切换到纵向,它抛出错误

(纵向开始)>;景观->;(肖像例外)

图表--https://github.com/PhilJay/MPAndroidChart

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at 安卓.view.ViewGroup.addViewInner(ViewGroup.java:3784)
at 安卓.view.ViewGroup.addView(ViewGroup.java:3637)
at 安卓.view.ViewGroup.addView(ViewGroup.java:3582)
at 安卓.view.ViewGroup.addView(ViewGroup.java:3558)

碎片

import 安卓.app.Fragment;

public class EDA_Fragment extends Fragment {

    private Chart chart = null;
    private View fragmentRootContainer;

    @Override
    public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        if(fragmentRootContainer == null) {
            fragmentRootContainer = inflater.inflate(R.layout.activity_exam, container, false);

            if (chart == null)
                chart = new Chart((LineChart) fragmentRootContainer.findViewById(R.id.chart), EDA, 1023f, 1f);

        }
        return fragmentRootContainer;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setRetainInstance(true);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        LayoutInflater inflater = LayoutInflater.from(getActivity());
        LinearLayout linearLayout = (LinearLayout) getView();
        linearLayout.removeAllViewsInLayout();

        // Checks the orientation of the screen
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {

            fragmentRootContainer = inflater.inflate(R.layout.activity_exam, linearLayout, false);

            LineChart lineChart = (LineChart) fragmentRootContainer.findViewById(R.id.chart);
            //((ViewGroup)lineChart.getParent()).removeView(lineChart);
            lineChart.addView(chart.getChart());


        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){

            fragmentRootContainer = inflater.inflate(R.layout.activity_exam, linearLayout, false);

            LineChart lineChart = (LineChart) fragmentRootContainer.findViewById(R.id.chart);
            //((ViewGroup)lineChart.getParent()).removeView(lineChart);
            lineChart.addView(chart.getChart());

        }
    }
}

共 (1) 个答案

  1. # 1 楼答案

    首先

    将此添加到AndroidManifest中

    android:name=".Activity.EDA_Activity"
    android:configChanges="orientation"
    

    第二

      in layout PORTRAIT add this -> android:orientation="vertical"
    
      in layout LANDSCAPE add this ->  android:orientation="horizontal"
    

    第三

    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    
        LayoutInflater inflater = LayoutInflater.from(getActivity());
        LinearLayout linearLayout = (LinearLayout) getView();
        if(linearLayout != null)
            linearLayout.removeAllViewsInLayout();
    
        // Checks the orientation of the screen
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
    
            ViewGroup parentViewGroup = (ViewGroup)chart.getChart().getParent();
            if (parentViewGroup != null)
                parentViewGroup.removeView(chart.getChart());
    
            fragmentRootContainer = inflater.inflate(R.layout.activity_exam, linearLayout, true);
            LineChart lineChart = (LineChart) fragmentRootContainer.findViewById(R.id.chart);
            lineChart.addView(chart.getChart());
        }
        else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
    
            ViewGroup parentViewGroup = (ViewGroup)chart.getChart().getParent();
            if (parentViewGroup != null)
                parentViewGroup.removeView(chart.getChart());
    
            fragmentRootContainer = inflater.inflate(R.layout.activity_exam, linearLayout, true);
            inflater.inflate(R.layout.activity_exam, linearLayout, false);
    
            LineChart lineChart = (LineChart) fragmentRootContainer.findViewById(R.id.chart);
            lineChart.addView(chart.getChart());
        }
    }