How to solve a Null Pointer Exception when trying to populate a ViewStub?
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>
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.
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).
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 Layout
s and View
s. 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 Layer
s appearing the same. I think this must be important because you also have your StubView
s inside Layer
s but it doesn't work to you. So try to put the StubView
s into different levels of the hierarchy.
精彩评论