2010-08-01 2 views
1

꽤 오랫동안 ViewStub을 채우는 동안 Null 포인터 예외로 문제를 해결하려고 노력했습니다. ImageButton을 눌렀을 때 ViewStub를 채우기 위해 일부 imageButton 및 ViewStub가 포함 된 SlidingDrawer 내에서 수행하려고합니다. ViewStubs를 만들려면 ViewStub Class Overview을 따라 왔지만 여전히 Null Pointer Exception을 얻습니다. 누군가 내가이 문제를 해결하기 위해해야 ​​할 일을 더 이상 알지 못하기 때문에 누군가 도와 주면 큰 도움이 될 것입니다. 아래 코드를 게시 해 드리겠습니다.ViewStub을 생성 할 때 Null 포인터 예외를 해결하는 방법은 무엇입니까?

감사합니다.

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

+0

이 줄은'importStub = stub.inflate();' – kivy

답변

0

분명히 findViewById(R.id.stub_exposure);은 null을 반환합니다. R.java 파일을 재생성하기 위해 클래스를 제대로 다시 컴파일 했습니까?

또한 뷰 스텁 레이아웃 XML 파일에서 텍스트 뷰의 텍스트 속성 중 하나를 android:text="@+String/hello"으로 정의했습니다.

그냥 오타가 아닌지 모르지만 R.java이 제대로 생성 되었습니까? strings.xml에 기존 값이있는 경우 (대문자가 아님) 컴파일러가 불평해야합니다.

Eclipse-를 사용하는 경우 경고 또는 오류가 발생하는지 확인하려면 프로젝트 -> 정리를 시도해야합니다. findViewById(R.id.stub_exposure) 다음/볼/찾아 ID 잘 R.class에서 생성되었지만 stub_exposure를 잡을하고 있지 않기 때문에

JRL 용의자로
+0

android : text = "@ + String/hello" 정말 바보 같은 오타 였지만, 여전히 같은 오류가 발생합니다. 또한 프로젝트를 정리했지만 경고 또는 오류가 발생하지 않습니다. ( "제대로 클래스를 다시 컴파일하고 R.java 파일을 다시 생성해야합니다"라는 의미는 무엇입니까? – kivy

+0

나는 클래스를 컴파일 했습니까? 생성 된'R.java'에'stub_exposure' ID가 있는지 확인 했습니까? – JRL

+0

존재하지 않습니다 :'public static final int stub_exposure = 0x7f06000a;'R.java' 파일이 생성되었습니다. – kivy

0

, 이것은이며, 방법은 null를 반환하고 inflate()가 호출 될 때 stub가 아직 없기 때문에 아무 반응이 없습니다 초기화 된 것은 null 개체입니다. 디버거를 사용하여이를 볼 수 있습니다.

따라서 XML 파서 또는 컴파일러에 문제가있을 수 있습니다. 나는 ViewStubView이 하나의 전용 XML 파일에서 사용하여 LayoutView의 주 인터페이스를 가진 XML 파일에 배치함으로써이 문제를 해결했습니다. 나는 LinearLayout 인 첫 번째 부모 Layer 안에 넣었습니다. 그러나 Layer을 모두 동일하게 유지하려면 내 StubView과 함께 FrameLayout에 포함시켜야했습니다. 나는 당신이 Layer 안에 StubView을 가지고 있기 때문에 이것이 중요 할 것이라고 생각합니다. 그러나 그것은 당신에게 효과가 없습니다. 따라서 StubView을 다른 계층 구조 수준에 배치하십시오.

0

ViewStub을 팽창 시키거나 표시하면 레이아웃에서 제거됩니다 (전체 점이 다른 뷰로 대체 됨). onClick 메서드는 처음에만 제대로 작동합니다. (a) findViewById에서 반환 된 null을 확인해야하고, (b) 인플레이션 후에 팽창중인 뷰의 ID를 스텁에 사용해야합니다.