如何从 MainActivity 访问 Header.xml 布局中的按钮?

How do I get access to a button inside my Header.xml layout from my MainActivity?

我遇到的问题是我无法直接从 MainActivity 访问 header xml 布局文件中的按钮。每次我 运行 应用程序都会崩溃。

我不断收到 NullPointerException 指向:

MainActivity.onCreate(MainActivity.java:125)

这行代码是什么:

mButtonHeader.setOnClickListener(new View.OnClickListener() {

如何访问按钮并为其设置 onClick() 以使其可点击?

我的Header布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="190dp"
android:background="#ffffff"
android:orientation="vertical"
android:id="@+id/myHeader">
<!--android:background="@drawable/background_material_original"-->

<Button
    android:id="@+id/buttonHeader"
    android:layout_width="match_parent"
    android:layout_height="190dp"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:alpha="0.0"
    android:clickable="true"
    android:enabled="true"
     /> 

 </RelativeLayout>

我的MainActivityXML布局文件:

 <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_home"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <include
        android:id="@+id/toolbar"
        layout="@layout/tool_bar" />

    <ImageView
        android:id="@+id/imageViewHomeBackgroundPic"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/toolbar"
        android:layout_marginTop="0dp"
        android:background="@drawable/homebp"
        android:contentDescription="@string/home_bp"/>

    <TextView
        android:id="@+id/mId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

 </RelativeLayout>


<android.support.design.widget.NavigationView
    android:id="@+id/navigation_view"
    android:layout_width="@dimen/navigation_drawer_width"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#fff"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/header"
    app:menu="@menu/menu_drawer_home"
    />


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

我的 MainActivity class:

public class MainActivity extends AppCompatActivity{

  protected Toolbar toolbar;
  protected NavigationView navigationView;
  private DrawerLayout drawerLayout;
  protected Button mButtonHeader;


  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    //Calling method of my navigation drawer
    setupNavViewDrawer();

     mButtonHeader = (Button) findViewById(R.id.buttonHeader);
     mButtonHeader.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            Uri uri = Uri.parse("http://www.google.com/"); 
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);
        }
    });



   }
}

我的logcat错误:

    10-31 20:48:05.342 14553-14553/com.pi.cas.ndtc E/AndroidRuntime: FATAL EXCEPTION: main
10-31 20:48:05.342 14553-14553/com.pi.cas.ndtc E/AndroidRuntime: Process: com.pi.cas.ndtc, PID: 14553
10-31 20:48:05.342 14553-14553/com.pi.cas.ndtc E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pi.cas.ndtc/com.pi.cas.ndtc.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
10-31 20:48:05.342 14553-14553/com.pi.cas.ndtc E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
10-31 20:48:05.342 14553-14553/com.pi.cas.ndtc E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
10-31 20:48:05.342 14553-14553/com.pi.cas.ndtc E/AndroidRuntime:     at android.app.ActivityThread.access0(ActivityThread.java:151)
10-31 20:48:05.342 14553-14553/com.pi.cas.ndtc E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
10-31 20:48:05.342 14553-14553/com.pi.cas.ndtc E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
10-31 20:48:05.342 14553-14553/com.pi.cas.ndtc E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135)
10-31 20:48:05.342 14553-14553/com.pi.cas.ndtc E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5257)
10-31 20:48:05.342 14553-14553/com.pi.cas.ndtc E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
10-31 20:48:05.342 14553-14553/com.pi.cas.ndtc E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
10-31 20:48:05.342 14553-14553/com.pi.cas.ndtc E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
10-31 20:48:05.342 14553-14553/com.pi.cas.ndtc E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
10-31 20:48:05.342 14553-14553/com.pi.cas.ndtc E/AndroidRuntime:  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
10-31 20:48:05.342 14553-14553/com.pi.cas.ndtc E/AndroidRuntime:     at com.pi.cas.ndtc.MainActivity.onCreate(MainActivity.java:125)
10-31 20:48:05.342 14553-14553/com.pi.cas.ndtc E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:5990)
10-31 20:48:05.342 14553-14553/com.pi.cas.ndtc E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
10-31 20:48:05.342 14553-14553/com.pi.cas.ndtc E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
10-31 20:48:05.342 14553-14553/com.pi.cas.ndtc E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390) 
10-31 20:48:05.342 14553-14553/com.pi.cas.ndtc E/AndroidRuntime:     at android.app.ActivityThread.access0(ActivityThread.java:151) 
10-31 20:48:05.342 14553-14553/com.pi.cas.ndtc E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
10-31 20:48:05.342 14553-14553/com.pi.cas.ndtc E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102) 
10-31 20:48:05.342 14553-14553/com.pi.cas.ndtc E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135) 
10-31 20:48:05.342 14553-14553/com.pi.cas.ndtc E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5257) 
10-31 20:48:05.342 14553-14553/com.pi.cas.ndtc E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 
10-31 20:48:05.342 14553-14553/com.pi.cas.ndtc E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372) 
10-31 20:48:05.342 14553-14553/com.pi.cas.ndtc E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
10-31 20:48:05.342 14553-14553/com.pi.cas.ndtc E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

您没有包含 XML,它包含您试图在 MainActivity XML 中查找的按钮。尝试包括在内,事情应该会开始好一点。

为清楚起见编辑:

您必须先加载包含按钮的 XML,然后才能调用 findViewById 并实际取回按钮。正如您的代码目前所显示的那样,它没有被加载,因此永远不会被发现,并且当您在 findViewById 之后尝试调用任何方法调用时,您将始终获得 NPE。

具体来说:

<android.support.design.widget.NavigationView
    android:id="@+id/navigation_view"
    android:layout_width="@dimen/navigation_drawer_width"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#fff"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/header"
    app:menu="@menu/menu_drawer_home">
*** <include layout="@layout/header" /> ***
</android.support.design.widget.NavigationView>

在 activity_main.xml

中包含您的 header 布局文件
<android.support.v4.widget.DrawerLayout ....>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <include
        android:id="@+id/toolbar"
        layout="@layout/tool_bar" />

    ....

 </RelativeLayout>


<android.support.design.widget.NavigationView
    ...

<include
        layout="@layout/header" />

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

这真的是迟到的答案,但也许它可以帮助其他遇到这个问题的人。

您需要像这样更改 activity_main.xml 代码: 在 activity_main 中删除: app:headerLayout="@layout/header"

并包括页眉布局。像那样:

来自

<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_width="@dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#fff"
android:fitsSystemWindows="true"
app:headerLayout="@layout/header"
app:menu="@menu/menu_drawer_home"/>

<android.support.design.widget.NavigationView
    android:id="@+id/navigation_view"
    android:layout_width="@dimen/navigation_drawer_width"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#fff"
    android:fitsSystemWindows="true"

    app:menu="@menu/menu_drawer_home"

 <include 
     layout="@layout/header"
     android:layout_width="match_parent"
     android:layout_height="190dp" />
</android.support.design.widget.NavigationView>