2016-06-01 4 views
-1

解決策が必要です! addView()私は答えを知っているドントにおけるエラー.....オリエンテーションでのフラグメントレイアウトの変更

私は、以前のビュー私は助けを必要と

を使用して、レイアウトを変更したいです!

チャート---- https://github.com/PhilJay/MPAndroidChart

プロセス:com.example.tiago.alsrm_android、PID:23337 java.lang.IllegalStateException:すでに指定された子が親を持っています。子の親で最初にremoveView()を呼び出す必要があります。 android.view.ViewGroup.addView(viewGroup.java:3782)の は、android.view.ViewGroup.addView(ViewGroup.java:3637)の と とandroid.view.ViewGroup.addView(ViewGroup.java:3582)の にあります。 com.example.tiago.alsrm_android.Fragment.EDA_Fragment.onConfigurationChangedでandroid.view.ViewGroup.addView(ViewGroup.java:3558) (EDA_Fragment.java:174)

EDA_Fragmentフラグメントを拡張

パブリッククラス{

private Intent intentService; 
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()); 
    ViewGroup viewGroup = (ViewGroup) getView(); 
    viewGroup.removeAllViewsInLayout(); 

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

     View view = inflater.inflate(R.layout.activity_exam, null); 

     LineChart lineChart = (LineChart)view.findViewById(R.id.chart); 
     lineChart.addView(chart.getChart()); 


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

     View view = inflater.inflate(R.layout.activity_exam, null); 

     LineChart lineChart = (LineChart)view.findViewById(R.id.chart); 
     lineChart.addView(chart.getChart()); 

    } 
} 

}

+0

チャートとChartLineは何ですか? –

+0

もlogcatを追加...... –

+0

loagcat add ....... –

答えて

0

------最初--------

------ 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()); 
    } 
} 
関連する問題