ImageButton.setOnClickListener上的空指针异常

时间:2016-11-07 01:17:05

标签: java android xml reference null

我创建了一个带有Backarrow作为ImageButton的服务器工具栏。我想获得对这个Button的引用,但它总是返回Null。两个小时前它工作,但我改变了代码,我不知道现在有什么问题..如果你可以帮助我会很棒!

MainActivity.java

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

// Always cast your custom Toolbar here, and set it as the ActionBar.
Toolbar tb = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(tb);

// Get the ActionBar here to configure the way it behaves.
ActionBar ab = getSupportActionBar();
//ab.setHomeAsUpIndicator(R.drawable.ic_menu); // set a custom icon for the default home button
ab.setDisplayShowHomeEnabled(true); // show or hide the default home button
ab.setDisplayHomeAsUpEnabled(true);
ab.setDisplayShowCustomEnabled(true); // enable overriding the default toolbar layout
ab.setDisplayShowTitleEnabled(true); // disable the default title element here

ImageButton backar = (ImageButton) findViewById(R.id.backarrow);
backar.setOnClickListener(this);
backar.setOnClickListener(new View.OnClickListener()
{
    public void onClick(View arg0)
    {
        MainActivity.super.onBackPressed();
    }
});

activity_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"
    android:fitsSystemWindows="true"
    tools:context="com.example.marc.stadtcoach.FreeActivity">

    <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="56dp"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />
    </android.support.design.widget.AppBarLayout>

    <RelativeLayout
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FFFFFF"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:context="com.example.marc.stadtcoach.MainActivity">

        <Button
            android:id="@+id/zeitspiel"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:background="@drawable/lockedroundbutton"
            android:text="Zeitspiel"
            android:textColor="#FFFFFF"
            android:layout_below="@+id/spiel"
            android:layout_alignLeft="@+id/spiel"
            android:layout_alignStart="@+id/spiel"
            android:layout_alignRight="@+id/spiel"
            android:layout_alignEnd="@+id/spiel" />

        <Button
            android:id="@+id/statistik"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="26dp"
            android:background="@drawable/roundbutton"
            android:text="Statistik"
            android:textColor="#FFFFFF"
            android:layout_below="@+id/zeitspiel"
            android:layout_alignLeft="@+id/spiel"
            android:layout_alignStart="@+id/spiel"
            android:layout_alignRight="@+id/zeitspiel"
            android:layout_alignEnd="@+id/zeitspiel" />


        <TextView
            android:id="@+id/coinView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="0"
            android:textSize="16dp"
            android:layout_alignBottom="@+id/imageView1"
            android:layout_alignRight="@+id/spiel"
            android:layout_alignEnd="@+id/spiel" />

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/coin"
            android:layout_marginRight="10dp"
            android:layout_marginEnd="10dp"
            android:layout_alignParentTop="true"
            android:layout_toLeftOf="@+id/coinView"
            android:layout_toStartOf="@+id/coinView" />

        <Button
            android:id="@+id/spiel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="108dp"
            android:background="@drawable/startbutton"
            android:text="Lernspiel"
            android:textColor="#FFFFFF"
            android:layout_below="@+id/coinView"
            android:layout_centerHorizontal="true" />
    </RelativeLayout>

</android.support.design.widget.CoordinatorLayout>

costum工具栏menu_back.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    app:layout_scrollFlags="scroll|enterAlways"
    app:layout_collapseMode="pin">
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">        

        <!-- This is a custom left side button -->
        <ImageButton
            android:id="@+id/backarrow"
            android:layout_width="?attr/actionBarSize"
            android:layout_height="?attr/actionBarSize"
            android:layout_marginRight="?attr/actionBarSize"
            android:layout_gravity="start|center_vertical"
            android:visibility="visible"
            android:src="@drawable/backarrow"
            style="@style/Widget.AppCompat.ActionButton" />

    </FrameLayout>
</android.support.v7.widget.Toolbar>

最后是错误信息

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.marc.stadtcoach, PID: 386
                  Theme: themes:{default=overlay:grovelet.s7.galaxys7.galaxy.launcher.theme, iconPack:grovelet.s7.galaxys7.galaxy.launcher.theme, fontPkg:grovelet.s7.galaxys7.galaxy.launcher.theme, com.android.systemui=overlay:grovelet.s7.galaxys7.galaxy.launcher.theme, com.android.systemui.navbar=overlay:grovelet.s7.galaxys7.galaxy.launcher.theme}
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.marc.stadtcoach/com.example.marc.stadtcoach.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2450)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510)
                      at android.app.ActivityThread.-wrap11(ActivityThread.java)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:148)
                      at android.app.ActivityThread.main(ActivityThread.java:5461)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                      at com.example.marc.stadtcoach.MainActivity.onCreate(MainActivity.java:44)
                      at android.app.Activity.performCreate(Activity.java:6251)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2403)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510) 
                      at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:148) 
                      at android.app.ActivityThread.main(ActivityThread.java:5461) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
I/Process: Sending signal. PID: 386 SIG: 9
Application terminated.

问题在于

ImageButton backar = (ImageButton) findViewById(R.id.backarrow);
我的MainActivity.java中的

返回null。

如果有人可以帮助我,我会很高兴的!

1 个答案:

答案 0 :(得分:0)

图像按钮位于工具栏布局内,尝试在工具栏中找到它。 ImageButton backar =(ImageButton)tb.findViewById(R.id.backarrow); 或者首先获取工具栏framelayout并从中获取图像按钮。