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

发布于 2024-09-12 00:24:18 字数 7168 浏览 11 评论 0原文

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

非常感谢您!


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

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>

for quite a while I have been trying to solve an issue with a Null Pointer Exception while populating a ViewStub. What I am trying to do is within a SlidingDrawer that contains some imageButtons and ViewStubs to fill in the ViewStubs when an ImageButton is pressed. I have been following the ViewStub Class Overview to create the ViewStubs, but I still get the Null Pointer Exception. If someone could help me out here, that would be great because I really don't know anymore what I should do to solve this issue...I will post my code below

Thank you very much in advance!


Here is the part of my code causing the exception:

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());}
   }
        });

This is the DrawingSlider XML file containing the ViewStub:

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

And this is the ViewStub layout xml (exposureview.xml), which is called to inflate the 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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

删除→记忆 2024-09-19 00:24:18

膨胀或显示 ViewStub 会导致它从布局中删除(它的全部要点是被另一个视图替换)。您的 onClick 方法只会在第一次时正常工作。您应该 (a) 检查从 findViewById 返回的 null,以及 (b) 使用在膨胀后要膨胀到存根中的视图的 id。

Inflating or showing a ViewStub causes it to be removed from the layout (the whole point of it is to be replaced by another view). Your onClick method is only going to work properly the first time. You should (a) check for a null returned from findViewById, and (b) use the id of the view you're inflating into the stub after the inflation.

゛时过境迁 2024-09-19 00:24:18

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

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

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

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

Apparently findViewById(R.id.stub_exposure); returns null for you. Did you recompile your class properly to regenerate the R.java file?

Also, I noticed you defined one of your text view's text property in your view stub layout XML file as android:text="@+String/hello".

Don't know if it's just a typo or not, but has the R.java been properly generated? If you have an existing value in strings.xml (notice not uppercase) then the compiler should complain.

You should try to do a Project -> Clean to see if you get warnings or errors (if you're using Eclipse).

柏拉图鍀咏恒 2024-09-19 00:24:18

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

因此我们可以看出 XML 解析器或编译器可能存在一些问题。我通过将 StubView 使用的 View 从单个专用 XML 文件放到主界面与更多 Layout 的 XML 文件中解决了这个问题View。我将它放在第一个父层 Layer 中,它是一个 LinearLayout 但我必须将它与我的 StubView< 一起嵌入到 FrameLayout 中/code> 保持所有Layer 的外观相同。我认为这一定很重要,因为您的 StubView 也位于 Layer 中,但它对您不起作用。因此,请尝试将 StubView 放入层次结构的不同级别。

As JRL suspects, this is because findViewById(R.id.stub_exposure) doesn't find/see/catch stub_exposure although the ID has been well generated in R.class, and then, the method returns null and when inflate() is called nothing happens because stub is still not initialized, is just a null object. You can see this by using the debugger.

So hence we can see that maybe there is some problem with the XML parser or compiler. I solved the problem by putting the View to be used by the StubView from a single dedicated XML file to the XML file where I had the main interface with more Layouts and Views. I put it inside the first parent Layer which was a LinearLayout but I had to embed it into a FrameLayout together with my StubView to keep all the Layers appearing the same. I think this must be important because you also have your StubViews inside Layers but it doesn't work to you. So try to put the StubViews into different levels of the hierarchy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文