/ / Ako vyriešiť výnimku nulového ukazovateľa pri pokuse o naplnenie ViewStub? android, nullpointerexception, viewstub

Ako vyriešiť výnimku Null Pointer pri pokuse o naplnenie ViewStub? android, nullpointerexception, viewstub

na chvíľu som sa snažil vyriešiťvydanie s výnimkou nulového ukazovateľa, zatiaľ čo populácia ViewStub. Čo sa snažím urobiť, je v SlidingDrawer, ktorý obsahuje niektoré imageButtons a ViewStubs na vyplnenie ViewStubs po stlačení ImageButton. Sledoval som to Prehľad triedy ViewStub vytvoriť ViewStuby, ale stále dostávam NullVýnimka ukazovateľa. Ak by mi niekto mohol pomôcť tu, bolo by to skvelé, pretože naozaj neviem, čo by som mal urobiť, aby som vyriešil túto otázku ...

Vopred veľmi pekne ďakujem!


Tu je časť môjho kódu spôsobujúcu výnimku:

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());}
}
});

Toto je súbor XML DrawingSlider s názvom 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>

A toto je rozloženie ViewStub xml (exposureview.xml), ktoré sa nazýva na nafukovanie stubu:

<?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>

odpovede:

0 pre odpoveď č. 1

Zrejme findViewById(R.id.stub_exposure); vráti nulu pre vás. Prepočítali ste svoju triedu správne na regeneráciu súboru R.java?

Tiež som si všimol, že ste vymedzili jednu z vašich textových vlastností textového pohľadu vo vašom výhľade na zobrazenie stubu XML ako android:text="@+String/hello".

Neviete, či je to len preklep, alebo nie, ale má R.java boli riadne vytvorené? Ak máte existujúcu hodnotu v strings.xml (upozornenie nie je veľké), potom by mal kompilátor sťažovať.

Mali by ste sa pokúsiť urobiť projekt -> Čistiť, aby ste zistili, či dostanete upozornenia alebo chyby (ak používate Eclipse).


0 pre odpoveď č. 2

Ako JRL podozrieva, je to preto, že findViewById(R.id.stub_exposure) nenachádza / nevidí / chytiť stub_exposure aj keď ID bolo dobre generované v roku 2006 R.class, a potom sa metóda vráti null a kedy inflate() sa nazýva nič, pretože sa stane stub stále nie je inicializovaný, je len null objekt. Toto môžete vidieť pomocou ladiaceho programu.

Takže môžeme vidieť, že možno existuje nejaký problém s parserom alebo kompilátorom XML. Vyriešil som problém tým, že View ktoré má používať StubView z jedného vyhradeného súboru XML do súboru XML, kde som mal hlavné rozhranie s viacerými Layouts a Views. Dala som ho do prvého rodiča Layer čo bolo LinearLayout ale musel som ho vložiť do a FrameLayout spolu so mnou StubView zachovať všetky Layerktoré sa objavujú rovnako. Myslím, že to musí byť dôležité, pretože máte aj svoje StubViews vnútri Layerale to vám nefunguje, tak skúste to dať StubViews rôznymi úrovňami hierarchie.


0 pre odpoveď č. 3

Nafúknutie alebo zobrazenie ViewStub spôsobí, že to má byťodstránené z rozloženia (celý jeho bod sa má nahradiť iným pohľadom). Vaša metóda OnClick bude prvýkrát fungovať správne. Mali by ste (a) skontrolovať nulu vrátenú z findViewById a (b) použiť ID zobrazenia, ktoré sa nafúkne do pahýl po inflácii.