【问题标题】:"Avoid passing null as the view root" with popupwindow使用弹出窗口“避免将 null 作为视图根传递”
【发布时间】:2014-11-11 08:30:51
【问题描述】:

所以,我的程序运行良好。我只剩下三个警告(每个弹出窗口一个),这让我很烦恼,我一直在寻找适合我需要的解决方案,但似乎找不到。

这是我的代码(其他两个弹出窗口类似)

else if(id == R.id.action_resetstats){
        LayoutInflater layoutInflater  = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
        View popupView = layoutInflater.inflate(R.layout.resetpop, null);
        final PopupWindow popupWindow = new PopupWindow(popupView,LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

        Button yesDismiss = (Button)popupView.findViewById(R.id.yesDismiss);
        yesDismiss.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View v){
                SharedPreferences prefs = getSharedPreferences(savedData, Context.MODE_PRIVATE);
                Editor editor = prefs.edit();
                editor.clear();
                editor.commit();

                counterW = 0;
                counterL = 0;
                counterT = 0;
                counterTot = 0;
                timerTime = 3;

                popupWindow.dismiss();
            }
        });    

        popupWindow.showAsDropDown(yeDismiss, 50, 50);


        Button noDismiss = (Button)popupView.findViewById(R.id.noDismiss);
        noDismiss.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View v){
                popupWindow.dismiss();
            }
        });    

        popupWindow.showAsDropDown(naDismiss, 50, 50);

xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_margin="5dp"
android:background="@android:color/black"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">

<RelativeLayout
    android:layout_width="230dp"
    android:layout_height="300dp"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:layout_margin="2dp"
    android:background="@android:color/darker_gray"
    android:orientation="vertical" >

    <Button
        android:id="@+id/noDismiss"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:text="NO!" />

    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:orientation="vertical" >

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="       Reset stats?"
            android:textSize="22sp" />
    </RelativeLayout>

    <Button
        android:id="@+id/yesDismiss"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:text="Yes" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/relativeLayout1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="34dp"
        android:text="Are you sure you want " />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:text="to reset your statistics?" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView2"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="39dp"
        android:text="!CANNOT BE UNDONE!" />

</RelativeLayout>

</RelativeLayout>

问题是在这里解决问题:

View popupView = layoutInflater.inflate(R.layout.resetpop, null);

警告说:

Avoid passing null as the view root (needed to resolve layout parameters on the inflated layout's root element)

任何想法如何以最好的方式解决这个问题?提前致谢!

【问题讨论】:

  • 忘记添加警告。现在应该编辑。
  • 我明白了。您通常应该提供父布局,以便复制其布局约束。在你的情况下,我认为你可以简单地忽略警告
  • 啊,很高兴知道我至少可以忽略它。我对任何类型的编程都很陌生,所以任何知识都非常有帮助。

标签: android android-layout null popupwindow layout-inflater


【解决方案1】:

你可以使用:

context = popupView.getContext();
inflater.inflate(R.layout.resetpop, new LinearLayout(context), false);

这应该可以解决警告。

【讨论】:

  • 构造一个假的父母是没有意义的。我宁愿简单地压制警告。
【解决方案2】:

当你写下面的代码博客时,你的意思是用父级膨胀这个布局,将它附加到父级,但是由于传递 null,inflater 将无法附加新的父级

View popupView = layoutInflater.inflate(R.layout.resetpop, null);

LayoutInflater class -->

  350
  351    public View More ...inflate(int resource, ViewGroup root) {
  352        return inflate(resource, root, root != null);
  353    }

但是,它会随着给定的父级膨胀,但不会将其附加到新的父级。

View popupView = layoutInflater.inflate(R.layout.resetpop, new LinearLayout(context), false);

【讨论】:

  • 他不需要。就像他说的,他的代码运行良好,他只是想摆脱警告。 inflate 方法中的 false 实际上与传递 NULL 相同(就像您使用代码 sn-p 声明的那样),但不会引发任何警告。
  • 这只是一个更多的信息:) 我知道你的回答是正确的
【解决方案3】:

PopupWindow 性能问题和无法关闭

如果有人偶然发现这里,因为PopupWindow 上的一些性能问题。

当您启动 PopupWindow 时,它只是冻结了整个屏幕并且无法继续,除非重新启动应用程序。发生在旧设备上,我的恰好是三星 GT-N7100 (API 19)。

好吧,我尝试在使用LayoutInflater 时解决警告。但是,并没有解决滞后问题。

第 1 步 - 使用软件加速将视图设置为渲染

根据这个论坛帖子,我只需将视图设置为对视图使用软件加速。那么,一切就如雨后春笋般。

https://androidforums.com/threads/android-popupwindow-performance-issues.882794/

val popupView = inflater.inflate(R.layout.popup_tooltip, null)
popupView.setLayerType(View.LAYER_TYPE_SOFTWARE, null)

第 2 步 - 将 PopupWindow 可聚焦属性设置为 Dismiss

在那之后,我仍然无法解散PopupWindow。如果仅将 focusable 设置为 true,则性能问题仍然存在。

我必须手动设置属性以允许在检测到点击时将其关闭。

https://stackoverflow.com/a/45408724/2867351

val popupWindow = PopupWindow(popupView, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)

popupWindow.isFocusable = true
popupWindow.isOutsideTouchable = true
popupWindow.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))

就是这样。干杯队友?

【讨论】:

    猜你喜欢
    • 2014-12-11
    • 1970-01-01
    • 1970-01-01
    • 2011-12-25
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多