很长一段时间我一直试图解决与填充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>
NPE发生何种线路? – JRL 2010-08-01 13:03:09
at this line'importStub = stub.inflate();' – kivy 2010-08-01 13:17:09