views:

84

answers:

1

Hi there,

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>
A: 

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).

JRL
android:text="@+String/hello"That was a really stupid typo, but I corrected it and still get the same error. And I also cleaned the project, but I don't get any warnings or errors :( What do you mean by "properly recompiling the class and regenerating the R.java file again"??
kivy
I meant did you compile the class and has the R.java file been regenerated. Did you check whether the `stub_exposure` ID exists in the generated `R.java`?
JRL
It does exist : `public static final int stub_exposure=0x7f06000a;`
kivy

related questions