Dimezis / BlurView

Dynamic iOS-like blur of underlying Views for Android
Apache License 2.0
3.5k stars 333 forks source link

android.view.ViewGroup.getViewTreeObserver()' on a null object reference #193

Closed karim-eg closed 1 year ago

karim-eg commented 1 year ago

Hello, i am trying to display blur at recyclerview customview but it show me this Error:

Attempt to invoke virtual method 'android.view.ViewTreeObserver android.view.ViewGroup.getViewTreeObserver()' on a null object reference

Screenshot_2022_12_16_18_05_18_21_80de061aead4140daa69cfda3df4a614

here the code that i used:

float flt = 12f;
View decorView = getWindow().getDecorView();

ViewGroup rootView = (ViewGroup) decorView.findViewById(R.id.content);

Drawable windowBackground = decorView.getBackground();

blurView.setupWith(rootView, new RenderScriptBlur(MainActivity.this))
.setFrameClearDrawable(windowBackground)
.setBlurRadius(flt)
.setBlurAutoUpdate(true);

The XML Code:

<LinearLayout 
  android:id="@+id/content" 
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" 
  android:layout_marginLeft="4dp" 
  android:layout_marginTop="2dp" 
  android:layout_marginRight="4dp" 
  android:layout_marginBottom="10dp" 
  android:orientation="vertical"> 
          <eightbitlab.com.blurview.BlurView 
           android:id="@+id/blurView" 
           android:layout_width="match_parent" 
           android:layout_height="match_parent" 
           android:orientation="vertical"> 
                     <TextView 
                      android:id="@+id/text1" 
                      android:focusable="false" 
                      android:layout_width="wrap_content" 
                      android:layout_height="wrap_content" 
                      android:padding="8dp" 
                      android:text="TextView" 
                      android:textSize="12sp" 
                      android:textColor="#000000" /> 
          </eightbitlab.com.blurview.BlurView> 
 </LinearLayout>
Dimezis commented 1 year ago

Your decorView.findViewById(R.id.content) is null most likely

karim-eg commented 1 year ago

Your decorView.findViewById(R.id.content) is null most likely

But i've added the linear as you see in code findViewById(R.id.content)

i've tried the same source on another new app and it worked, i don't know why it doesn't work here

Dimezis commented 1 year ago

I don't see the full code, so I can't tell you why. But if it's null, it's not a library bug

karim-eg commented 1 year ago

I don't see the full code, so I can't tell you why. But if it's null, it's not a library bug

the code below is inside recyclerView Adapter:

float flt = 12f;
View decorView = getWindow().getDecorView();

ViewGroup rootView = (ViewGroup) decorView.findViewById(R.id.content);

Drawable windowBackground = decorView.getBackground();

blurView.setupWith(rootView, new RenderScriptBlur(MainActivity.this))
.setFrameClearDrawable(windowBackground)
.setBlurRadius(flt)
.setBlurAutoUpdate(true);
Dimezis commented 1 year ago

So is it null?

karim-eg commented 1 year ago

Attempt to invoke virtual method 'android.view.ViewTreeObserver android.view.ViewGroup.getViewTreeObserver()' on a null object reference

Yes, But Finally i've solved it after i used this condition:

if (rootView != null) {
rootView.getViewTreeObserver();
}
Dimezis commented 1 year ago

If that solves your issue, there's just something wrong with your setup. Why are you setting the direct parent of the BlurView as the root view? Why are you looking it up in the decorView?

karim-eg commented 1 year ago

If that solves your issue, there's just something wrong with your setup. Why are you setting the direct parent of the BpurView as the root view? Why are you looking it up in the decorView?

Yes, as I mentioned this code is inside Recycler View Adapter linked with custom view

This is same like Facebook posts but the post background is blurred instead of white color, this is the nearest example.