在活动开始时无法将relativelayout设置为不可见给出NullPointerException

时间:2015-06-04 06:12:45

标签: android android-relativelayout android-switch

我有一个布局,我有一个开关和RelativeLayout,其中包含2个图像按钮。默认情况下,开关保持关闭状态。我想要的是当活动加载时我想将相对布局设置为b不可见,如果开关设置为开启状态,那么RelativeLayout应该是可见的。

当我尝试在我的OnCreate中将其设置为隐形时,它会给我NullPointerException ..

如何在活动开始时将布局设置为b不可见,如果检查开关,则只有布局应该可见..? 请帮助..

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>

活动: -

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

3 个答案:

答案 0 :(得分:0)

在Increate中创建相对布局

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

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

}

它可以解决您的问题

答案 1 :(得分:0)

onCreate(..)方法中设置内容视图后,您需要先初始化所有视图对象。在这种情况下,您的RelativeLayout对象未初始化,并且抛出NPE(空指针例外)

你只需要像这样修改你的onCreate(..)方法,事情应该顺利进行。

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

我希望这会有所帮助。

答案 2 :(得分:0)

您没有创建相对布局的引用。

在此之前..

relativeLayout2.setVisibility(View.INVISIBLE);

添加这个

RelativeLayout relativeLayout2 = (RelativeLayout) findViewById(R.id.relative2);