无法从线性布局中删除视图

时间:2014-11-18 15:59:34

标签: android android-fragments android-linearlayout

我正在加载一个表示片段中级别的视图的线性布局。它第一次工作正常,但每次我尝试调整视图时,我都会收到一条错误,指出非法参数错误。我调用从线性布局中删除所有视图,所以我不太清楚发生了什么。

我首先在一项活动中做了一个概念验证,它运作良好。我应该在这里提一下,片段有时会松散焦点,然后重新加载。这似乎只有在重新加载片段时才会发生。

以下是加载视图的代码:

private void setMinutes(int width, int height) {

    mLevelLayout.removeAllViews();

    for(int i = 0; i < mLevelViewByMinutes.length; i++) {
         View mLevelView = mLevelViewByMinutes[i];

        if(mLevelView == null)
            mLevelView = getLevelView(width, height);   
        else
            mLevelView = adjustLevelView(height, mLevelView, i < mCurrentMinute - 1);

        mLevelLayout.addView(mLevelViewByMinutes[i] = mLevelView, i);
    }
}

private View getLevelView(int width, int height) {
    LayoutInflater inflater = (LayoutInflater)   this.getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View levelView = (View) inflater.inflate(R.layout.view_workout_level, null);

    LayoutParams params = new LayoutParams(width, height);
    levelView.setLayoutParams(params);

    return levelView;
}

 private View adjustLevelView(int height, View levelView, boolean isNowOrPast) {
     if(isNowOrPast)
        levelView.setBackground(getResources().getDrawable(R.drawable.workout_level_white));
    else 
        levelView.getLayoutParams().height = height;

    return levelView;
}

这是我的错误消息:

11-18 09:56:03.330: E/AndroidRuntime(12352): FATAL EXCEPTION: main
11-18 09:56:03.330: E/AndroidRuntime(12352): Process: ............PID: 12352
11-18 09:56:03.330: E/AndroidRuntime(12352): java.lang.IllegalStateException: The specified child     already has a parent. You must call removeView() on the child's parent first.
11-18 09:56:03.330: E/AndroidRuntime(12352):    at    android.view.ViewGroup.addViewInner(ViewGroup.java:3759)
11-18 09:56:03.330: E/AndroidRuntime(12352):    at android.view.ViewGroup.addView(ViewGroup.java:3612)
11-18 09:56:03.330: E/AndroidRuntime(12352):    at android.view.ViewGroup.addView(ViewGroup.java:3557)
11-18 09:56:03.330: E/AndroidRuntime(12352):    at  com.octane.smartlink.fragments.SupplementLevelFragment.setMinutes(SupplementLevelFragment.java:97)
11-18 09:56:03.330: E/AndroidRuntime(12352):    at com.octane.smartlink.fragments.SupplementLevelFragment.onResume(SupplementLevelFragment.java:50)
11-18 09:56:03.330: E/AndroidRuntime(12352):    at android.app.Fragment.performResume(Fragment.java:1743)
11-18 09:56:03.330: E/AndroidRuntime(12352):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:924)
11-18 09:56:03.330: E/AndroidRuntime(12352):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
11-18 09:56:03.330: E/AndroidRuntime(12352):    at android.app.BackStackRecord.popFromBackStack(BackStackRecord.java:767)
11-18 09:56:03.330: E/AndroidRuntime(12352):    at android.app.FragmentManagerImpl.popBackStackState(FragmentManager.java:1496)
11-18 09:56:03.330: E/AndroidRuntime(12352):    at android.app.FragmentManagerImpl$2.run(FragmentManager.java:486)
11-18 09:56:03.330: E/AndroidRuntime(12352):    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
11-18 09:56:03.330: E/AndroidRuntime(12352):    at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
11-18 09:56:03.330: E/AndroidRuntime(12352):    at android.os.Handler.handleCallback(Handler.java:733)
11-18 09:56:03.330: E/AndroidRuntime(12352):    at android.os.Handler.dispatchMessage(Handler.java:95)
11-18 09:56:03.330: E/AndroidRuntime(12352):    at android.os.Looper.loop(Looper.java:157)
11-18 09:56:03.330: E/AndroidRuntime(12352):    at android.app.ActivityThread.main(ActivityThread.java:5350)
11-18 09:56:03.330: E/AndroidRuntime(12352):    at java.lang.reflect.Method.invokeNative(Native Method)
11-18 09:56:03.330: E/AndroidRuntime(12352):    at java.lang.reflect.Method.invoke(Method.java:515)
11-18 09:56:03.330: E/AndroidRuntime(12352):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
11-18 09:56:03.330: E/AndroidRuntime(12352):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
11-18 09:56:03.330: E/AndroidRuntime(12352):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

您也可以使用此解决方法:

 if (view != null) {
    //Get the real LinearLayout which is holding your View
    ViewGroup parent = (ViewGroup) view.getParent();
    //check if it is null
    if (parent != null) {
        //remove your view
        parent.removeView(view);
    }
 }