java - setVisibility(View.INVISIBLE) makes my app crash -


i'm trying write app buttons trigger sound , want buttons visible when pressed. when add setvisibility method crashes app. here xml code:

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:paddingleft="@dimen/activity_horizontal_margin"     android:paddingright="@dimen/activity_horizontal_margin"     android:paddingtop="@dimen/activity_vertical_margin"     android:paddingbottom="@dimen/activity_vertical_margin"     tools:context=".mainactivity"     android:id="@+id/background"     android:background="@drawable/image"     android:orientation="horizontal">      <imagebutton         android:id="@+id/btn1"         android:layout_width="109dp"         android:layout_height="89dp"         android:scaletype="fitxy"         android:layout_margintop="26dp"         android:src="@mipmap/oneeee"         android:background="@null"         android:contentdescription="@null"         android:layout_alignparenttop="true"         android:layout_alignparentleft="true"         android:layout_alignparentstart="true"         android:layout_marginleft="1dp" />  </relativelayout> 

and there java part:

import android.app.activity; import android.media.mediaplayer; import android.os.bundle; import android.view.view; import android.widget.button; import android.widget.imagebutton; import android.widget.relativelayout;  import static android.view.view.visible;  public class mainactivity extends activity {      relativelayout background;     imagebutton btn;     mediaplayer sound;      @override     protected void onpause() {         super.onpause();         sound.release();     }      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         btn.setvisibility(view.invisible);         setcontentview(r.layout.activity_main);         sound = mediaplayer.create(this, r.raw.kick);         btn = (imagebutton) findviewbyid(r.id.btn1);         btn.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view v) {                 mediaplayer mp = mediaplayer.create(getapplicationcontext(), r.raw.kick);                 btn.setvisibility(view.visible);                 mp.start();             }         });     }  } 

i'm guessing you're getting nullpointerexception.

this because you're calling btn.setvisibility() before assigning value btn. btn null.

common practice perform setcontentview(r.layout.<layout name>) right after super.oncreate. , you're setting visibility of btn before you're initializing reference actual button view.

suggested changes:

btn = (button) findviewbyid(r.id.xxx); btn.setvisibility(view.invisible); 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

Kivy: Swiping (Carousel & ScreenManager) -

jdbc - Not able to establish database connection in eclipse -