简体   繁体   中英

onBackPressed() method not working properly in android

I m doing something like that.

@Override
    public void onBackPressed() {
        homeTabLayout = findViewById(R.id.rel_HomeLayout);
        reviewLayout = findViewById(R.id.scroll_ReviewLayout);

        if (reviewLayout.getVisibility() == View.VISIBLE) {
            reviewLayout.setVisibility(View.GONE);
        } else {

        }
    }

but its raising the below Exceptions (while im pressing the backKey)

java.lang.NullPointerException

and

my.package.name.MainActivity.onBackPressed(MainActivity.java:303)

here is my XML file which does not contains this reviewLayout and reviewLayout.

   <?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <RelativeLayout
            android:id="@+id/Rel_ScreenFirst"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/top_header_gradient"
            android:paddingBottom="8dp"
            android:paddingTop="8dp" >

            <TextView
                android:id="@+id/txt_Login"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="6dp"
                android:layout_toLeftOf="@+id/txt_Register"
                android:layout_alignParentRight="true"
                android:clickable="true"
                android:text="Login/Register"
                android:textColor="@color/White"
                android:textSize="12sp" />


            <TextView
                android:id="@+id/txt_Shia"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBaseline="@+id/txt_islamicLibrary"
                android:layout_alignParentLeft="true"
                android:paddingLeft="5dp"
                android:text="Shia"
                android:textColor="@color/White"
                android:textSize="15sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/txt_islamicLibrary"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@+id/txt_Shia"
                android:text="IslamicLibrary"
                android:textColor="@color/White"
                android:textSize="23sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/txt_dotCom"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBaseline="@+id/txt_islamicLibrary"
                android:layout_toRightOf="@+id/txt_islamicLibrary"
                android:text=".com"
                android:textColor="@color/White"
                android:textSize="12sp"
                android:textStyle="bold" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/Rel_Spinner"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/Rel_ScreenFirst"
            android:background="@drawable/bottom_header"
            android:padding="5dp" >

            <Spinner
                android:id="@+id/spnrLanguage"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="2dp"
                android:paddingLeft="5dp" >
            </Spinner>

            <FrameLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_toLeftOf="@+id/spnrBrowseBy"
                android:layout_toRightOf="@+id/spnrLanguage" >

                <EditText
                    android:id="@+id/EditTxt_Search"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:hint="Search Here..."
                    android:inputType="text"
                    android:selectAllOnFocus="true"
                    android:singleLine="true"
                    android:textSize="12sp"
                    android:textStyle="bold" >
                </EditText>

                <Button
                    android:id="@+id/btn_GO"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right|center_vertical"
                    android:text="GO" />
            </FrameLayout>

            <Spinner
                android:id="@+id/spnrBrowseBy"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_marginRight="2dp"
                android:paddingLeft="5dp"
                android:text="Browse By" >
            </Spinner>
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/rel_01"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_below="@+id/Rel_Spinner"
            android:layout_gravity="bottom" >

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="60dp"
                android:layout_alignParentBottom="true"
                android:tabStripEnabled="false" />

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_above="@android:id/tabs"
                android:layout_alignParentLeft="true"
                android:layout_margin="0dp"
                android:background="@drawable/background" >
            </FrameLayout>
        </RelativeLayout>
    </RelativeLayout>

</TabHost>

yes that's right, its not present in the content layout. how can I get reference of this view. while the actual view exits in my Home.java file.. and im doing all this code in the Main.java file.

I guess the View that you have set does not have R.id.scroll_ReviewLayout . So check whether it is present in the layout xml or add a null check

if (reviewLayout != null && reviewLayout.getVisibility() == View.VISIBLE) {
            reviewLayout.setVisibility(View.GONE);
        }

I am not sure. But try below changes

homeTabLayout = (RelativeLayout) findViewById(R.id.rel_HomeLayout); //(Relativelayout) is object of rel_HomeLayout. So ,use name of rel_HomeLayout control


 reviewLayout = (RelativeLayout) findViewById(R.id.scroll_ReviewLayout);

Just put a log below the line

reviewLayout = findViewById(R.id.scroll_ReviewLayout);

and see if reviewLayout is null

 Log.i("Log","reviewLayout " + reviewLayout);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM