尝试隐藏视图/布局时出现NullPointerException

时间:2014-08-10 22:49:38

标签: java android xml android-layout android-relativelayout

当我点击其中一个菜单项时,我试图隐藏一个视图/布局,但我一直得到一个nullpointerexception。

这就是我的做法。

RelativeLayout relativeLayout;
relativeLayout = (RelativeLayout) findViewById(R.id.layouttohide);

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.action_cog:
        if (relativeLayout.isShown() == true) {
            relativeLayout.setVisibility(relativeLayout.GONE);
        }else {
            relativeLayout.setVisibility(relativeLayout.VISIBLE);
        }

        return true;
    case R.id.action_stuff:
        Toast.makeText(getApplicationContext(),
                  item.getTitle(), Toast.LENGTH_SHORT).show();
        return true;

    default:
        return super.onOptionsItemSelected(item);
    }
}

在XML文件中,这就是我的布局设置。

<RelativeLayout
    android:id="@+id/layouttohide"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_marginBottom="0dp"
    android:background="#025fb6"
    android:visibility="visible" >

这是错误

08-10 18:56:23.936: E/AndroidRuntime(6473): FATAL EXCEPTION: main
08-10 18:56:23.936: E/AndroidRuntime(6473): java.lang.NullPointerException
08-10 18:56:23.936: E/AndroidRuntime(6473):     at com.app.MainActivity.onOptionsItemSelected(MainActivity.java:156)
08-10 18:56:23.936: E/AndroidRuntime(6473):     at android.app.Activity.onMenuItemSelected(Activity.java:2534)
08-10 18:56:23.936: E/AndroidRuntime(6473):     at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:372)
08-10 18:56:23.936: E/AndroidRuntime(6473):     at android.support.v7.app.ActionBarActivity.superOnMenuItemSelected(ActionBarActivity.java:244)
08-10 18:56:23.936: E/AndroidRuntime(6473):     at android.support.v7.app.ActionBarActivityDelegateICS.onMenuItemSelected(ActionBarActivityDelegateICS.java:164)
08-10 18:56:23.936: E/AndroidRuntime(6473):     at android.support.v7.app.ActionBarActivity.onMenuItemSelected(ActionBarActivity.java:130)
08-10 18:56:23.936: E/AndroidRuntime(6473):     at android.support.v7.app.ActionBarActivityDelegateICS$WindowCallbackWrapper.onMenuItemSelected(ActionBarActivityDelegateICS.java:308)
08-10 18:56:23.936: E/AndroidRuntime(6473):     at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:965)
08-10 18:56:23.936: E/AndroidRuntime(6473):     at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
08-10 18:56:23.936: E/AndroidRuntime(6473):     at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
08-10 18:56:23.936: E/AndroidRuntime(6473):     at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
08-10 18:56:23.936: E/AndroidRuntime(6473):     at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:514)
08-10 18:56:23.936: E/AndroidRuntime(6473):     at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:99)
08-10 18:56:23.936: E/AndroidRuntime(6473):     at android.view.View.performClick(View.java:4084)
08-10 18:56:23.936: E/AndroidRuntime(6473):     at android.view.View$PerformClick.run(View.java:16966)
08-10 18:56:23.936: E/AndroidRuntime(6473):     at android.os.Handler.handleCallback(Handler.java:615)
08-10 18:56:23.936: E/AndroidRuntime(6473):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-10 18:56:23.936: E/AndroidRuntime(6473):     at android.os.Looper.loop(Looper.java:146)
08-10 18:56:23.936: E/AndroidRuntime(6473):     at android.app.ActivityThread.main(ActivityThread.java:4777)
08-10 18:56:23.936: E/AndroidRuntime(6473):     at java.lang.reflect.Method.invokeNative(Native Method)
08-10 18:56:23.936: E/AndroidRuntime(6473):     at java.lang.reflect.Method.invoke(Method.java:511)
08-10 18:56:23.936: E/AndroidRuntime(6473):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
08-10 18:56:23.936: E/AndroidRuntime(6473):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
08-10 18:56:23.936: E/AndroidRuntime(6473):     at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:3)

我看到的一个问题是您为setVisibility(...)

输入的参数

变化:

if (relativeLayout.isShown() == true) {
        relativeLayout.setVisibility(relativeLayout.GONE);
    }else {
        relativeLayout.setVisibility(relativeLayout.VISIBLE);
    }

 if (relativeLayout.isShown() == true) {
        relativeLayout.setVisibility(View.GONE);
    }else {
        relativeLayout.setVisibility(View.VISIBLE);
    }

答案 1 :(得分:1)

过去这对我有用:

http://developer.android.com/reference/android/view/View.html

if (relativeLayout.isShown()) {
    relativeLayout.setVisibility(View.GONE);
} else {
    relativeLayout.setVisibility(View.VISIBLE);
}

答案 2 :(得分:0)

relativeLayout变量只是null。 您setContentView()的布局ID错误,或者您只是没有设置布局。

顺便说一句,你应该在你的onOptionsItemSelected:

上这样做
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.action_cog:
            relativeLayout.setVisibility(relativeLayout.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
        return true;
    /* ... */
    }
}