2010-08-01 120 views
1

很长一段时间我一直试图解决与填充ViewStub时空指针异常的问题。我想要做的是在一个SlidingDrawer内包含一些imageButtons和ViewStubs来填充ViewStub当一个ImageButton被按下时。我一直在关注ViewStub Class Overview来创建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> 
+0

NPE发生何种线路? – JRL 2010-08-01 13:03:09

+0

at this line'importStub = stub.inflate();' – kivy 2010-08-01 13:17:09

回答

0

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

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

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

您应该尝试执行Project - > Clean来查看是否会收到警告或错误(如果您使用的是Eclipse)。

+0

android:text =“@ + String/hello” 这是一个非常愚蠢的错字,但我纠正它,仍然得到相同的错误。我也清理了这个项目,但是我没有收到任何警告或者错误:( 你是什么意思“正确地重新编译类并重新生成R.java文件”? – kivy 2010-08-01 13:38:48

+0

我的意思是你编译过类并且R.java文件被重新生成了吗?你检查'stub_exposure' ID是否存在于生成的'R.java'中? – JRL 2010-08-01 13:42:07

+0

它确实存在:'public static final int stub_exposure = 0x7f06000a;'' – kivy 2010-08-01 14:03:12

0

由于JRL犯罪嫌疑人,这是因为findViewById(R.id.stub_exposure)没有找到/看/搭上stub_exposure虽然ID已R.class得到了很好的生成,然后,该方法返回nullinflate()叫什么都不会发生,因为stub仍不初始化,只是一个null对象。你可以通过使用调试器来看到这一点。

因此,我们可以看到,XML解析器或编译器可能存在一些问题。我通过将ViewStubView从单个专用XML文件使用到XML文件来解决此问题,其中我有更多的Layout s和 s的主界面。我把它放在第一个父Layer这是一个LinearLayout,但我不得不将它嵌入FrameLayout连同我的StubView,以保持所有Layer s出现相同。我认为这一定很重要,因为你的StubView也在Layer之内,但它对你不起作用。因此,请尝试将StubView置于层次结构的不同级别。

0

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