0

I have a layout where i have a switch and RelativeLayout which contains 2 Image buttons. By default switch remains in the off state. What i want is when the activity loads i want to set the relative layout to b invisible and if the switch is set in the on state then the RelativeLayout should be visible.

When i try to set it invisible in my OnCreate it is giving me NullPointerException..

How can i set the layout to b invisible when the activity starts and if the switch is checked then only the layout should be visible.. ? Please Help..

Layout.xml :-

 <Switch
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    style="@style/btnStyleBeige"
    android:id="@+id/switch1"
    android:layout_marginRight="10dp"
    android:layout_alignTop="@+id/textView7"
    android:layout_alignStart="@+id/button5" />

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/relative2"
    android:layout_marginTop="40dp"
    android:layout_below="@+id/switch1"
    android:layout_alignParentStart="true"
    android:layout_alignEnd="@+id/taskname">

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/pickDate"
        android:src="@drawable/c2"
        style="@style/btnStyleBeige"
        android:layout_marginStart="65dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/pickTime"
        style="@style/btnStyleBeige"
        android:src="@drawable/c1"
        android:layout_marginEnd="65dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true" />

</RelativeLayout>

Activity :-

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.addtask);

    getActionBar().setDisplayHomeAsUpEnabled(true);


    relativeLayout2.setVisibility(View.INVISIBLE);

    Switch aSwitch = (Switch) findViewById(R.id.switch1);

    aSwitch.setOnCheckedChangeListener(this);

    init();

    /** Capture our View elements */
    //pDisplayDate = (TextView) findViewById(R.id.displayDate);
    pPickDate = (ImageButton) findViewById(R.id.pickDate);

    /** Listener for click event of the button */
    pPickDate.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            showDialog(DATE_DIALOG_ID);
        }
    });

   /** Capture our View elements */
    //pDisplayTime = (TextView) findViewById(R.id.displayTime);
    pPickTime = (ImageButton) findViewById(R.id.pickTime);


    /** Listener for click event of the button */
    pPickTime.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            showDialog(TIME_DIALOG_ID);
        }
    });

    /** Get the current date and time */
    final Calendar cal = Calendar.getInstance();
    pYear = cal.get(Calendar.YEAR);
    pMonth = cal.get(Calendar.MONTH);
     pDay = cal.get(Calendar.DAY_OF_MONTH);

    mHour = cal.get(Calendar.HOUR_OF_DAY);
    mMinute = cal.get(Calendar.MINUTE);
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

    relativeLayout2 = (RelativeLayout)findViewById(R.id.relative2);
    if(isChecked) {
        relativeLayout2.setVisibility(View.GONE);
    } else {
        relativeLayout2.setVisibility(View.VISIBLE);
    }
}
user2205230
  • 173
  • 1
  • 2
  • 16
  • 1
    your `relativeLayout2` is not initialized – N J Jun 04 '15 at 06:14
  • 1
    As Nilesh said initialize relativeLayout2 as :- `relativeLayout2 = (RelativeLayout)findViewById(R.id.relative2);` in on create() method before setting visibility – Pankaj Jun 04 '15 at 06:16
  • maybe you ahve not initialized your layout. can you post your logcat error ? it would be more helpful. – RajSharma Jun 04 '15 at 06:20

3 Answers3

0

Make Your Relative Layout Inside Oncreate

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.addtask);

relativeLayout2 = (RelativeLayout)findViewById(R.id.relative2);
 relativeLayout2.setVisibility(View.INVISIBLE);

}

It may Solve your Problem

Android Dev
  • 421
  • 8
  • 26
0

After setting content View in onCreate(..) method , you need to first initialise all of your View Objects. In this case your RelativeLayout Object is not initialised and there fore throwing a NPE (Null Pointer Exception)

You just need to modify your onCreate(..) method like this and things should work smoothly.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.addtask);

    getActionBar().setDisplayHomeAsUpEnabled(true);

    //This line of code is missing.
    relativeLayout2 = (RelativeLayout) findViewById(R.id. relative2);

    relativeLayout2.setVisibility(View.INVISIBLE);

    Switch aSwitch = (Switch) findViewById(R.id.switch1);

    aSwitch.setOnCheckedChangeListener(this);

    init();

    /** Capture our View elements */
    //pDisplayDate = (TextView) findViewById(R.id.displayDate);
    pPickDate = (ImageButton) findViewById(R.id.pickDate);

    /** Listener for click event of the button */
    pPickDate.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
            showDialog(DATE_DIALOG_ID);
        }
    });

    /** Capture our View elements */
    //pDisplayTime = (TextView) findViewById(R.id.displayTime);
    pPickTime = (ImageButton) findViewById(R.id.pickTime);

    /** Listener for click event of the button */
    pPickTime.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            showDialog(TIME_DIALOG_ID);
        }
    });

    /** Get the current date and time */
    final Calendar cal = Calendar.getInstance();
    pYear = cal.get(Calendar.YEAR);
    pMonth = cal.get(Calendar.MONTH);
    pDay = cal.get(Calendar.DAY_OF_MONTH);

    mHour = cal.get(Calendar.HOUR_OF_DAY);
    mMinute = cal.get(Calendar.MINUTE);
}

I hope this helps.

Salman Khakwani
  • 6,684
  • 7
  • 33
  • 58
0

You did not created the reference of relative layout.

Before this line..

relativeLayout2.setVisibility(View.INVISIBLE);

add this one

RelativeLayout relativeLayout2 = (RelativeLayout) findViewById(R.id.relative2);
Vid
  • 1,012
  • 1
  • 14
  • 29