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

时间:2015-11-01 02:54:21

标签: android android-layout navigation-drawer navigationview android-navigationview

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

我一直得到一个NullPointerException指向:

MainActivity.onCreate(MainActivity.java:125)

这行代码是什么:

mButtonHeader.setOnClickListener(new View.OnClickListener() {

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

我的页眉布局:

<?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>

我的MainActivity XML布局文件:

 <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类:

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.access$800(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.access$800(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) 

3 个答案:

答案 0 :(得分:1)

在您的activity_main.xml

中包含您的标题布局文件
<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>

答案 1 :(得分:0)

您不包含包含您尝试在MainActivity XML中找到的按钮的XML。试着把它包括在内,事情应该开始好转了。

为清晰起见编辑:

在调用findViewById并实际获取按钮之前,必须加载包含该按钮的XML。正如您的代码当前所代表的那样,它不会被加载,因此永远不会被找到,并且当您在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>

答案 2 :(得分:0)

这是很晚的答案,但也许可以帮助遇到此问题的其他人。

您需要像这样更改您的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>