Android 当尝试填充ViewStub时,如何解决空指针异常?

Android 当尝试填充ViewStub时,如何解决空指针异常?,android,nullpointerexception,viewstub,Android,Nullpointerexception,Viewstub,很长一段时间以来,我一直试图解决填充ViewStub时出现空指针异常的问题。我试图做的是在一个滑动抽屉中,其中包含一些imageButtons和ViewStubs,以便在按下ImageButton时填充ViewStubs。我一直遵循创建ViewStub的方法,但仍然得到Null指针异常。如果有人能在这里帮助我,那就太好了,因为我真的不知道该怎么做才能解决这个问题…我将在下面发布我的代码 提前非常感谢 以下是导致异常的代码部分: btnExposure = (ImageButton) findV

很长一段时间以来,我一直试图解决填充ViewStub时出现空指针异常的问题。我试图做的是在一个滑动抽屉中,其中包含一些imageButtons和ViewStubs,以便在按下ImageButton时填充ViewStubs。我一直遵循创建ViewStub的方法,但仍然得到Null指针异常。如果有人能在这里帮助我,那就太好了,因为我真的不知道该怎么做才能解决这个问题…我将在下面发布我的代码

提前非常感谢


以下是导致异常的代码部分:

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),调用它来膨胀存根:

<?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>

显然
findViewById(R.id.stub\u暴露)为您返回空值。您是否正确地重新编译了类以重新生成R.java文件

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

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


你应该试着做一个项目->清理,看看是否有警告或错误(如果你正在使用Eclipse)。

显然
findViewById(R.id.stub\u暴露)为您返回空值。您是否正确地重新编译了类以重新生成R.java文件

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

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


你应该试着做一个项目->清理,看看你是否收到警告或错误(如果你正在使用Eclipse)。

正如JRL怀疑的那样,这是因为
findViewById(R.id.stub\u exposure)
没有找到/看到/捕获
stub\u exposure
,尽管id已经在
R.class
中生成,然后,该方法返回
null
,当调用
inflate()
时,不会发生任何事情,因为
stub
仍然没有初始化,它只是一个
null
对象。通过使用调试器可以看到这一点


因此,我们可以看到XML解析器或编译器可能存在一些问题。我通过将
StubView
要使用的
视图
从一个专用XML文件放到一个XML文件中解决了这个问题,在这个XML文件中,我有了更多
布局
s和
视图
s的主界面。我把它放在第一个父
中,这是一个
线性布局
,但我必须将它与我的
StubView
一起嵌入到
框架布局中,以保持所有
的外观相同。我认为这一定很重要,因为在
层中也有
StubView
s,但它对您不起作用。因此,尝试将
StubView
s放入层次结构的不同级别。

正如JRL所怀疑的那样,这是因为
findViewById(R.id.stub\u exposure)
没有找到/查看/捕获
stub\u exposure
,尽管在
R.class
中已经很好地生成了id,然后,该方法返回
null
,当调用
inflate()
时,不会发生任何事情,因为
stub
仍然没有初始化,它只是一个
null
对象。通过使用调试器可以看到这一点


因此,我们可以看到XML解析器或编译器可能存在一些问题。我通过将
StubView
要使用的
视图
从一个专用XML文件放到一个XML文件中解决了这个问题,在这个XML文件中,我有了更多
布局
s和
视图
s的主界面。我把它放在第一个父
中,这是一个
线性布局
,但我必须将它与我的
StubView
一起嵌入到
框架布局中,以保持所有
的外观相同。我认为这一定很重要,因为在
层中也有
StubView
s,但它对您不起作用。因此,尝试将
存根视图
放入层次结构的不同级别。

膨胀或显示视图存根会导致将其从布局中删除(它的整个点将被另一个视图替换)。您的onClick方法只能在第一次正常工作。您应该(a)检查findViewById返回的空值,以及(b)在充气后使用正在充气到存根中的视图id。

充气或显示视图存根会导致将其从布局中删除(它的整个点将被另一个视图替换)。您的onClick方法只能在第一次正常工作。您应该(a)检查findViewById返回的空值,以及(b)在充气后使用正在充气到存根中的视图id。

NPE发生在哪一行?在这一行
importStub=stub.inflate()NPE发生在哪一行?在这一行
importStub=stub.inflate()
android:text=“@+String/hello”这是一个非常愚蠢的打字错误,但我纠正了它,仍然得到相同的错误。我还清理了项目,但没有收到任何警告或错误:(你说的“正确地重新编译类并重新生成R.java文件”是什么意思??我的意思是你编译了类并且重新生成了R.java文件了吗