activity 启动时无法将 relativelayout 设置为不可见,给出 NullPointerException

Unable to set relativelayout to invisible when activity starts gives NullPointerException

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

当我尝试在我的 OnCreate 中将它设置为不可见时,它给我 NullPointerException..

如何在 activity 启动时将布局设置为不可见,如果选中开关,则只有布局应该可见..? 请帮助..

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

在 Oncreate 中创建相对布局

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

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

}

它可能会解决您的问题

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

希望对您有所帮助。

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

在这一行之前..

relativeLayout2.setVisibility(View.INVISIBLE);

添加这个

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