繁体   English   中英

Android 对象在 onCreate 后返回 null

[英]Android Object coming back null after onCreate

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!的地方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);
            }
        });
    }
}

活动_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>

包含在 activity_main.xml 中的 nav_header_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标头通常以这种方式包含:

<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的项目之一。

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

希望我做对了,它会对你有用!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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