【问题标题】:How to solve a Null Pointer Exception when trying to populate a ViewStub?尝试填充 ViewStub 时如何解决空指针异常?
【发布时间】:2010-08-01 12:50:38
【问题描述】:

很长一段时间以来,我一直在尝试解决填充 ViewStub 时出现空指针异常的问题。我要做的是在一个包含一些 imageButtons 和 ViewStubs 的 SlidingDrawer 中,以便在按下 ImageButton 时填充 ViewStubs。我一直在关注ViewStub Class Overview 创建 ViewStubs,但我仍然得到空指针异常。如果有人可以在这里帮助我,那就太好了,因为我真的不知道我应该怎么做才能解决这个问题......我将在下面发布我的代码

非常感谢您!


这是我的代码中导致异常的部分:

btnExposure = (ImageButton) findViewById(R.id.button_exposure);

        btnExposure.setOnClickListener(new OnClickListener()
        {

         @Override
   public void onClick(View v) {
          try{   
           ViewStub stub = (ViewStub) findViewById(R.id.stub_exposure);
           importStub = stub.inflate();
          }catch (Exception e){
                 Log.e("ERROR", "Error in Code:" +e.toString());}
   }
        });

这是包含 ViewStub 的 DrawingSlider XML 文件:

<?xml version="1.0" encoding="utf-8"?>

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent" 
   android:layout_height="fill_parent"
   android:background="@drawable/shape_background_gradient"
         >
     <LinearLayout
      android:layout_width="205px"
      android:layout_height="155px"
      android:background="#FFCCCCCC"
      android:id="@+id/VideoViewHolder"
      android:padding="2.5px"
      android:layout_centerHorizontal="true"
      android:layout_marginTop="5px"
      android:layout_marginBottom="5px"
      >
  <VideoView
   android:layout_width="200px"
   android:layout_height="150px"
   android:id ="@+id/VideoViewEdit"
   android:layout_centerHorizontal="true"

   >
  </VideoView>
  </LinearLayout>
  <ScrollView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/VideoViewHolder">
   <TableLayout
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:stretchColumns="0">
    <TableRow
      android:background="@drawable/shape_track_background"
      android:padding="5px"
      >
     <ImageView android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:src="@drawable/ic_editor_videotrack"
          android:padding="5px"
          android:layout_gravity="left"/>

    </TableRow>
    <TableRow
      android:background="@drawable/shape_track_background"
      android:padding="5px"
      >
      <ImageView android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:src="@drawable/ic_editor_audiotrack"
          android:padding="5px"
          android:layout_gravity="left"/>


    </TableRow>
    <TableRow
      android:background="@drawable/shape_track_background"
      android:padding="5px"
      >
     <ImageButton android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:src="@drawable/ic_editor_add_media"
          android:padding="5px"
          android:layout_gravity="left"
          />
    </TableRow>
   </TableLayout>
  </ScrollView>
 <SlidingDrawer
     android:id="@+id/drawer"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
  android:orientation="horizontal"
     android:handle="@+id/handle"
     android:content="@+id/content">

     <ImageView
         android:id="@id/handle"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:src="@drawable/slider_handle"
         />

     <LinearLayout
         android:id="@id/content"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
         android:orientation="vertical">
        <ImageButton 
         android:id="@+id/button_exposure"
         android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:background="#77000000"
   android:src="@drawable/ic_slider_exposure"
   android:padding="5px"
   android:layout_marginBottom="1px"
   android:layout_gravity="left"/>
        <ImageButton 
         android:id="@+id/button_properties"
         android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:background="#77000000"
   android:src="@drawable/ic_slider_properties"
   android:padding="5px"
   android:layout_marginBottom="1px"
   android:layout_gravity="left"/>
        <ImageButton 
         android:id="@+id/button_special_effects"
         android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:background="#77000000"
   android:src="@drawable/ic_slider_special_effects"
   android:padding="5px"
   android:layout_marginBottom="1px"
   android:layout_gravity="left"/>
  <ImageButton 
         android:id="@+id/button_test1"
         android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:background="#77000000"
   android:src="@drawable/ic_slider_exposure"
   android:padding="5px"
   android:layout_marginBottom="1px"
   android:layout_gravity="left"/>
        <ImageButton 
         android:id="@+id/button_test2"
         android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:background="#77000000"
   android:src="@drawable/ic_slider_exposure"
   android:padding="5px"
   android:layout_marginBottom="1px"
   android:layout_gravity="left"/>
     </LinearLayout>
     <ViewStub
      android:id="@+id/stub_exposure"
      android:inflatedId="@+id/stub_exposure_view"

      android:layout="@layout/exposureview"

      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:background="#77000000">
     </ViewStub>
     <ViewStub
      android:id="@+id/stub_properties"
      android:inflatedId="@+id/stub_properties_view"

      android:layout="@layout/propertiesview"

      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:background="#77000000">
     </ViewStub>
     <ViewStub
      android:id="@+id/stub_special_effects"
      android:inflatedId="@+id/stub_special_effects_view"

      android:layout="@layout/specialeffectsview"

      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:background="#77000000">
     </ViewStub>
 </SlidingDrawer>
</RelativeLayout>

这是 ViewStub 布局 xml (exposureview.xml),调用它来为 Stub 充气:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 >
 <TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="@+String/hello"
 ></TextView>
 <SeekBar
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:thumb="@drawable/seek_thumb"
  android:progress="50"
  android:max="100">
 </SeekBar>

</RelativeLayout>

【问题讨论】:

  • NPE 发生在哪一行?
  • 在这一行importStub = stub.inflate();

标签: android nullpointerexception viewstub


【解决方案1】:

膨胀或显示 ViewStub 会导致它从布局中移除(它的全部内容将被另一个视图替换)。您的 onClick 方法只会在第一次正常工作。您应该 (a) 检查从 findViewById 返回的空值,并且 (b) 在膨胀后使用要膨胀到存根中的视图的 id。

【讨论】:

    【解决方案2】:

    显然findViewById(R.id.stub_exposure); 为您返回 null。您是否正确地重新编译了您的类以重新生成 R.java 文件?

    另外,我注意到您在视图存根布局 XML 文件中将文本视图的文本属性之一定义为 android:text="@+String/hello"

    不知道这是否只是一个错字,但R.java 是否已正确生成?如果您在 strings.xml 中有一个现有值(注意不是大写),那么编译器应该会抱怨。

    您应该尝试做一个项目 -> 清理以查看是否收到警告或错误(如果您使用的是 Eclipse)。

    【讨论】:

    • android:text="@+String/hello" 这是一个非常愚蠢的错字,但我更正了它,仍然得到同样的错误。而且我还清理了项目,但没有收到任何警告或错误:(“正确重新编译类并再次重新生成 R.java 文件”是什么意思??
    • 我的意思是您是否编译了该类并重新生成了 R.java 文件。检查生成的R.java中是否存在stub_exposure ID?
    • 确实存在:public static final int stub_exposure=0x7f06000a;
    【解决方案3】:

    正如 JRL 所怀疑的那样,这是因为 findViewById(R.id.stub_exposure) 没有找到/看到/捕获 stub_exposure,尽管在 R.class 中已经很好地生成了 ID,然后,该方法返回 null 并且当 inflate()被称为什么都没有发生,因为stub 仍未初始化,只是一个null 对象。您可以使用调试器看到这一点。

    因此,我们可以看到 XML 解析器或编译器可能存在问题。我解决了这个问题,将StubView 使用的View 从单个专用XML 文件放到我拥有更多Layouts 和Views 的主界面的XML 文件中。我将它放在第一个父 Layer 中,这是一个 LinearLayout,但我必须将它与我的 StubView 一起嵌入到 FrameLayout 中,以保持所有 Layers 看起来相同。我认为这一定很重要,因为您在Layers 中也有您的StubViews,但它对您不起作用。所以试着把StubViews 放到层次结构的不同层次。

    【讨论】:

      猜你喜欢
      • 2014-01-06
      • 1970-01-01
      • 1970-01-01
      • 2022-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多