1

かなり長い間、ViewStub の作成中に Null Pointer Exception の問題を解決しようとしてきました。私がやろうとしているのは、ImageButton が押されたときに ViewStubs を埋めるためのいくつかの imageButtons と ViewStubs を含む SlidingDrawer 内です。ViewStub クラスの概要に従ってViewStubを作成しましたが、それでも Null Pointer 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());}
   }
        });

これは、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>
4

3 に答える 3

1

ViewStub をインフレートまたは表示すると、レイアウトから削除されます (そのポイント全体が別のビューに置き換えられます)。onClick メソッドは、最初の 1 回だけ正しく機能します。(a) findViewById から返された null をチェックし、(b) インフレ後にスタブにインフレートしているビューの ID を使用する必要があります。

于 2012-04-21T03:13:07.653 に答える
0

JRL が推測しているように、これはID が で適切に生成されているにもかかわらず、 がfindViewById(R.id.stub_exposure)検索/参照/キャッチしないためであり、その後メソッドが返され、が呼び出されてもはまだ初期化されておらず、単なるオブジェクトであるため何も起こりません。これは、デバッガーを使用して確認できます。stub_exposureR.classnullinflate()stubnull

したがって、XML パーサーまたはコンパイラーに何らかの問題がある可能性があることがわかります。Viewが使用するStubViewを、単一の専用 XML ファイルから、より多くLayoutの およびを備えたメイン インターフェイスを持つ XML ファイルに変更することで、問題を解決しましたView。最初の親Layerである a の中に入れましたが、すべてのs が同じように見えるようにするために、 my と一緒LinearLayoutに a に埋め込む必要がありました。s の中にsもありますが、うまくいかないので、これは重要だと思います。そのため、階層のさまざまなレベルに sを配置してみてください。FrameLayoutStubViewLayerStubViewLayerStubView

于 2012-04-02T08:27:04.033 に答える
0

どうやらfindViewById(R.id.stub_exposure);null を返します。クラスを適切に再コンパイルして、R.java ファイルを再生成しましたか?

また、ビュー スタブ レイアウト XML ファイルで、テキスト ビューのテキスト プロパティの 1 つを として定義していることに気付きましたandroid:text="@+String/hello"

単なるタイプミスかどうかわかりませんが、R.java正しく生成されていますか? strings.xml(大文字ではないことに注意してください)に既存の値がある場合、コンパイラは文句を言うはずです。

Project -> Clean を実行して、警告またはエラーが発生するかどうかを確認する必要があります (Eclipse を使用している場合)。

于 2010-08-01T13:22:07.463 に答える