简体   繁体   中英

Android Object coming back null after onCreate

Android newbie here.

I'm trying to set an onClick event on a LinearLayout . But I keep getting the error Attempt to invoke virtual method 'void android.widget.LinearLayout.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

Which, if I understand correctly means that the reference is null. Which doesn't make sense to me since I'm calling it after onCreate

Here's my code, anybody have any clues what I'm doing wrong?

MainActivity - The place with HERE! is where the crash is occuring

package ...
import ...

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        final NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        setNavHeaderOnClickAction();

    }


    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // ...
    }


    public void setNavHeaderOnClickAction(){
        // HERE! Here's the problemsome area
        LinearLayout navHeaderUser = (LinearLayout) findViewById(R.id.nav_header_user);
        navHeaderUser.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            // Intent myIntent = new Intent(MainActivity.this, UserProfile.class);
            // MainActivity.this.startActivity(myIntent);
            // DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            // drawer.closeDrawer(GravityCompat.START);
            }
        });
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">


    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>

app_bar_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.trevorwood.biggles.MainActivity">



    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include android:id="@+id/main_content" layout="@layout/content_blank"/>

    <android.support.design.widget.AppBarLayout
        android:id="@+id/search_button"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:layout_gravity="top|end"
        android:layout_margin="10dp"
        android:background="@android:drawable/ic_menu_search">


    </android.support.design.widget.AppBarLayout>

</android.support.design.widget.CoordinatorLayout>

nav_header_main.xml (layout) included in activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="120dp"
    android:background="@drawable/side_nav_bar"
    android:gravity="center"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:theme="@style/ThemeOverlay.AppCompat.Dark">

    <LinearLayout
        android:id="@+id/nav_header_user"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:orientation="horizontal"
        android:clickable="true">
        <ImageView
            android:id="@+id/imageView"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:paddingTop="@dimen/nav_header_vertical_spacing"
            app:srcCompat="@android:drawable/sym_def_app_icon"
            android:clickable="false"/>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:clickable="false">
            <TextView
                android:id="@+id/textView"
                android:layout_width="303dp"
                android:layout_height="wrap_content"
                android:paddingTop="@dimen/nav_header_vertical_spacing"
                android:text="JohnSmith007"
                android:textAppearance="@style/TextAppearance.AppCompat.Body1"
                android:clickable="false"/>
            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="1,500 points"
                android:clickable="false"/>

        </LinearLayout>


    </LinearLayout>
</LinearLayout>

activity_main_drawer.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <group
        android:id="@+id/grp1"
        android:checkableBehavior="single">
        <item
            android:id="@+id/nav_home"
            android:icon="@drawable/ic_menu_home"
            android:title="Home" />
        <item
            android:id="@+id/nav_library"
            android:icon="@drawable/ic_menu_book"
            android:title="Library" />
        <item
            android:id="@+id/nav_create"
            android:icon="@drawable/ic_menu_add"
            android:title="Create" />
        <item
            android:id="@+id/nav_user"
            android:icon="@drawable/ic_menu_user"
            android:title="My Account" />
        <item
            android:id="@+id/nav_settings"
            android:icon="@drawable/ic_menu_cog_wheel"
            android:title="Settings" />

    </group>

    <group
        android:id="@+id/grp2"
        android:checkableBehavior="single">
        <item
            android:id="@+id/nav_about"
            android:icon="@drawable/ic_menu_about"
            android:title="About" />
        <item
            android:id="@+id/nav_sign_out"
            android:icon="@drawable/ic_menu_sign_out"
            android:title="Sign Out" />
    </group>

</menu>

content_blank.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="70dp"
        android:text="content_blank"/>

</LinearLayout>

NavigationView header is usually included this way:

<android.support.design.widget.NavigationView
        android:id="@+id/nvView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@android:color/white"
        app:headerLayout="@layout/main_drawer_header"
        app:itemTextColor="@color/black"
        app:menu="@menu/menu_main_drawer"
        app:itemIconTint="@null"/>

Since support libraries version 23.1.0 NavigationView is using a RecyclerView and the header is added as one of RecyclerView 's items.

Activity's findViewById won't find the header and its internals.

To get access to it you need to get the header from NavigationView and call findViewById relative to header view:

NavigationView navigationView = (NavigationView) findViewById(R.id.nvView);
View navHeaderview = navigationView.getHeaderView(0);

LinearLayout navHeaderUser = (LinearLayout) navHeaderview.findViewById(R.id.nav_header_user);
    navHeaderUser.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        // Intent myIntent = new Intent(MainActivity.this, UserProfile.class);
        // MainActivity.this.startActivity(myIntent);
        // DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        // drawer.closeDrawer(GravityCompat.START);
        }
    });

Hope I got it right and it'll work for you!

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