在onCreate之后,Android对象返回null

时间:2017-09-27 02:34:51

标签: java android android-layout

Android新手在这里。

我正在尝试在LinearLayout上设置onClick事件。但我一直在收到错误 Attempt to invoke virtual method 'void android.widget.LinearLayout.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

如果我理解正确意味着引用为空。这对我来说没有意义,因为我在onCreate

之后调用它

这是我的代码,任何人都有任何线索我做错了什么?

MainActivity - HERE!的地方是发生崩溃的地方

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(布局)包含在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>

1 个答案:

答案 0 :(得分:1)

NavigationView标头通常以这种方式包含在内:

<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"/>

由于支持库版本23.1.0 NavigationView使用的是RecyclerView,并且标题已添加为RecyclerView个项目之一。

活动findViewById无法找到标题及其内部。

要访问它,您需要从NavigationView获取标题,并相对于标题视图调用findViewById

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

希望我做对了,它会为你工作!