Android新手在這裏。Android對象在onCreate後返回null
我想在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>
添加你的main.xml –
你能發佈你的完整代碼嗎? NavigationView.java,nav_header_main.xml。或者您可以嘗試使用'LinearLayout navHeaderUser =(LinearLayout)navigationView.findViewById(R.id.nav_header_user);'在'setNavHeaderOnClickAction()'方法中找到'LinearLayout''(請記住將'NavigationView'作爲參數傳遞**) –
@KingfisherPhuoc新增 –