Fix several NPEs in ShadowXMLBlock

Update ShadowXmlBlock.get{Name,NameSpace,Text} to not NPE if called after
close. This behavior is consistent with real Android.

Fixes #6292

PiperOrigin-RevId: 362575777
diff --git a/integration_tests/ctesque/src/test/java/android/content/res/ResourcesTest.java b/integration_tests/ctesque/src/test/java/android/content/res/ResourcesTest.java
index b3ce0e5..34f451f 100644
--- a/integration_tests/ctesque/src/test/java/android/content/res/ResourcesTest.java
+++ b/integration_tests/ctesque/src/test/java/android/content/res/ResourcesTest.java
@@ -575,6 +575,16 @@
   }
 
   @Test
+  public void testGetXml_notNPEAfterClose() {
+    XmlResourceParser parser = resources.getXml(R.xml.preferences);
+    parser.close();
+    // the following methods should not NPE if the XmlResourceParser has been closed.
+    assertThat(parser.getName()).isNull();
+    assertThat(parser.getNamespace()).isEmpty();
+    assertThat(parser.getText()).isNull();
+  }
+
+  @Test
   public void openRawResource_shouldLoadRawResources() throws Exception {
     InputStream resourceStream = resources.openRawResource(R.raw.raw_resource);
     assertThat(resourceStream).isNotNull();
diff --git a/shadows/framework/src/main/java/org/robolectric/shadows/ShadowXmlBlock.java b/shadows/framework/src/main/java/org/robolectric/shadows/ShadowXmlBlock.java
index 953813b..c2e1a4c 100644
--- a/shadows/framework/src/main/java/org/robolectric/shadows/ShadowXmlBlock.java
+++ b/shadows/framework/src/main/java/org/robolectric/shadows/ShadowXmlBlock.java
@@ -144,28 +144,37 @@
   @Implementation(minSdk = VERSION_CODES.LOLLIPOP)
   protected static int nativeGetNamespace(long state) {
     ResXMLParser resXMLParser = getResXMLParser(state);
+    if (resXMLParser == null) {
+      return -1;
+    }
     return resXMLParser.getElementNamespaceID();
   }
 
   @Implementation(maxSdk = VERSION_CODES.KITKAT_WATCH)
   protected static int nativeGetName(int state) {
-    return (int)nativeGetName((long)state);
+    return nativeGetName((long) state);
   }
 
   @Implementation(minSdk = VERSION_CODES.LOLLIPOP)
   protected static int nativeGetName(long state) {
     ResXMLParser resXMLParser = getResXMLParser(state);
+    if (resXMLParser == null) {
+      return -1;
+    }
     return resXMLParser.getElementNameID();
   }
 
   @Implementation(maxSdk = VERSION_CODES.KITKAT_WATCH)
   protected static int nativeGetText(int state) {
-    return (int)nativeGetText((long)state);
+    return nativeGetText((long) state);
   }
 
   @Implementation(minSdk = VERSION_CODES.LOLLIPOP)
   protected static int nativeGetText(long state) {
     ResXMLParser resXMLParser = getResXMLParser(state);
+    if (resXMLParser == null) {
+      return -1;
+    }
     return resXMLParser.getTextID();
   }
 
@@ -346,6 +355,6 @@
   }
 
   private static ResXMLParser getResXMLParser(long state) {
-    return Registries.NATIVE_RES_XML_PARSERS.getNativeObject(state);
+    return Registries.NATIVE_RES_XML_PARSERS.peekNativeObject(state);
   }
 }