1

我遇到的问题是我无法直接从我的 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) 
4

3 回答 3

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>
于 2015-11-01T03:41:55.240 回答
0

您不包括包含您尝试在 MainActivity XML 中查找的按钮的 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>
于 2015-11-01T03:22:58.690 回答
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>
于 2019-08-02T18:36:45.330 回答