【问题标题】:How to show Views of inflated layout in Tabbed Activity?如何在选项卡式活动中显示膨胀布局的视图?
【发布时间】:2016-05-06 15:50:37
【问题描述】:

我在android studio 中创建了一个默认选项卡式活动。 我尝试使用 tab1 和 tab2 显示 2 个视图。 他们继承了LinearLayout。 以下是他们的代码(Tab1)。

public class Tab1 extends LinearLayout {
    View rootView;

    public Tab1(Context context) {
        super(context);
        init(context);
    }

    public Tab1(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    //inflater
    private void init(Context context) {
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        rootView = inflater.inflate(R.layout.fragment_main_tab1, this, true);
    }

    public View getView(){ //getter
        return rootView;
    }
}

这是我在默认tabbed activity 中创建的onCreateView 方法。 我想在每个选项卡上显示 tab1、tab2。

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

    View rootView; //return view
    Tab1 tab1 = new Tab1(StaticManager.applicationContext); //inflated activity
    Tab2 tab2 = new Tab2(StaticManager.applicationContext); //inflated activity

    if (getArguments().getInt(ARG_SECTION_NUMBER) == 1) {
        container.addView(tab1);
        rootView = tab1.getRootView();
    } else {
        container.addView(tab2);
        rootView = tab2.getRootView();
    }
    return rootView;
}

错误信息是这样的,

05-06 14:36:57.972 19264-19264/kr.eyeballs.mobilecloudchattingapp E/AndroidRuntime: FATAL EXCEPTION: main
05-06 14:36:57.972 19264-19264/kr.eyeballs.mobilecloudchattingapp E/AndroidRuntime: Process: kr.eyeballs.mobilecloudchattingapp, PID: 19264
05-06 14:36:57.972 19264-19264/kr.eyeballs.mobilecloudchattingapp E/AndroidRuntime: java.lang.StackOverflowError

它不起作用。 我该如何解决这个问题?

如果你不明白我的意思,让我知道会补充更多信息。

【问题讨论】:

    标签: android android-tabbed-activity


    【解决方案1】:

    您调用了错误的方法,您应该在片段中调用getView() 而不是getRootView()ViewGroup 中的代码和平导致堆栈溢出:

       /**
         * @hide
         */
        @Override
        public void resetResolvedLayoutDirection() {
            super.resetResolvedLayoutDirection();
    
            int count = getChildCount();
            for (int i = 0; i < count; i++) {
                final View child = getChildAt(i);
                if (child.isLayoutDirectionInherited()) {
                    child.resetResolvedLayoutDirection();
                }
            }
        }
    

    【讨论】:

    • 有效!谢谢 :) 我对 getRootView 和 getView 很困惑哈哈
    猜你喜欢
    • 1970-01-01
    • 2016-12-02
    • 1970-01-01
    • 1970-01-01
    • 2020-10-15
    • 2011-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多