【问题标题】:App closes in linearLayout removeAllViews line inside fragment: Android应用程序在片段内的 linearLayout removeAllViews 行中关闭:Android
【发布时间】:2017-10-20 23:40:29
【问题描述】:

我是安卓开发新手。我将代码从 MainActivity 转移到 Fragment 类。在我的 onCreateView 方法中,我正在调用 viewFunds() 方法来动态显示来自 SQLite 的数据。

但是,当我单击该片段的选项卡时,应用程序会在到达 removeAllViews 行时关闭。每次调用此方法刷新数据时,我都需要先删除所有视图。

由于我也在其他方法中调用 ViewFunds(),例如在删除数据时,我调用它来刷新屏幕上的结果。

见下面的代码:

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

        View rootview = inflater.inflate(R.layout.activity_settings_tab, container, false);
        super.onCreate(savedInstanceState);
        myDb = new DatabaseHelper(getContext());


        btnAddData = (Button) rootview.findViewById(R.id.button_add);
        btnviewAll = (Button) rootview.findViewById(R.id.button_viewAll);
        btnAddFund = (Button) rootview.findViewById(R.id.button_AddFund);
        btnviewUpdate = (Button) rootview.findViewById(R.id.button_update);
        editEmergencyFund = (EditText) rootview.findViewById(R.id.edit_EmergencyFund);
        editProsperityFund = (EditText) rootview.findViewById(R.id.edit_ProsperityFund);
        editRewardFund = (EditText) rootview.findViewById(R.id.edit_RewardFund);

        viewAll(); //this is for a button
        viewFunds();//this is executed when the fragment is called


        return inflater.inflate(R.layout.activity_settings_tab, container, false);
    }


public void viewFunds() { 
        Cursor res2 = myDb.getConfigData();
        res2.moveToFirst();


        final View linearLayout = getActivity().findViewById(R.id.ll_config);
        ((LinearLayout) linearLayout.getParent()).removeAllViews(); //clear layout first - LINE WITH ISSUE
        ((LinearLayout) linearLayout.getParent()).setGravity(Gravity.CENTER);

 //some code follows...

 }

这组代码中写在 removeAllViews 行之后的最后一行也会导致应用关闭:

 LinearLayout llh = new LinearLayout(getActivity());
            llh.setOrientation(LinearLayout.HORIZONTAL);
            LinearLayout.LayoutParams lp_llh = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            llh.setLayoutParams(lp_llh);

            ((LinearLayout) linearLayout.getParent()).addView(llh); //code  that causes app to close

提前感谢您的帮助。

【问题讨论】:

    标签: java android android-fragments dynamic-view


    【解决方案1】:

    在这里,您返回刚刚在onCreateView 末尾创建的新View:

    return inflater.inflate(R.layout.activity_settings_tab, container, false);
    

    由于您返回新的 View 非 Views 您发现有一个有效的引用,这会导致 NPE。将其更改为:

    return rootView;
    

    【讨论】:

    • 你的意思是这样还是我理解错了?返回 inflater.inflate(rootView, 容器, false);。当我尝试时,“rootView”在这一行被标记为红色。谢谢
    • 就像上面提到的那样,用 return rootView 替换整行;您首先膨胀根视图,然后在其中定义视图,最后您应该将其作为片段视图返回。这就是为什么您应该在方法结束时返回根视图。
    • 谢谢。一个新手问题:如何在 rootView 中以编程方式定义视图?另一个:在您的答案的最后一段中,您能否提供一个示例代码,以便我理解“在 Activity 中定义一个公共方法并通过从 Fragment 调用该方法来编辑 Activity View”是什么意思。谢谢。
    • 对不起最后一部分,我把 getParent() 和 getActivity() 弄错了,你不需要在 Activity 中定义公共方法。顺便说一句,removeAllViews 问题解决了吗?
    • 不,它没有:(。我将代码更改为 ((LinearLayout) linearLayout).removeAllViews(); 但是当应用程序到达这一行时仍然崩溃。我是否使用公共 void viewFunds () 在片段中不正确?
    【解决方案2】:

    我假设((LinearLayout) linearLayout.getParent()).removeAllViews() 也会删除linearLayout。尝试这样做:

    ...
    ViewParent parent = linearLayout.getParent();
    parent.removeAllViews();
    parent.setGravity(Gravity.CENTER);
    ...
    parent.addView(llh);
    ...
    

    【讨论】:

    • 只有linearlayout下的动态对象(textviews,edittexts)应该被移除。linearlayout是指静态创建的LinearLayout。当我尝试上述建议时, parent.removeAllViews 带有红色下划线。这可能是什么原因?
    • 那么你应该调用linearLayout.removeAllViews(),因为它只删除子视图,而不是原始视图。
    • 我将这一行改为: ((LinearLayout)linearLayout).removeAllViews();但是当到达这条线时,应用程序仍然崩溃。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多