2016-11-07 3 views
0

Ich habe eine Costum Toolbar mit einem Backarrow als ImageButton erstellt. Ich möchte einen Verweis auf diesen Button erhalten, aber er gibt immer Null zurück. Vor zwei Stunden hat es funktioniert, aber ich habe den Code geändert und ich weiß nicht, was jetzt falsch ist. Es wäre toll, wenn du mir helfen könntest!Nullzeiger Ausnahme bei ImageButton.setOnClickListener

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> 

Die costum Symbolleiste 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> 

Und schließlich die Fehlermeldung

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. 

Das Problem ist, dass

ImageButton backar = (ImageButton) findViewById(R.id.backarrow); 

in meinem MainActivity.java null zurückgibt.

Ich würde mich sehr freuen, wenn es jemanden gibt, der mir helfen kann!

+0

Es wäre besser, onCreateOptionsMenu und onOptionsItemSelected zu überschreiben, um benutzerdefinierte Symbole in der Symbolleiste hinzuzufügen. Ich habe diese Methode verwendet und es funktioniert perfekt .. Wenn Sie Zweifel haben, fragen Sie mich .. – vishnumm93

Antwort

0

Die Bildschaltfläche befindet sich im Symbolleistenlayout. Versuchen Sie, sie in der Symbolleiste zu finden. ImageButton backar = (Bildschaltfläche) tb.findViewById (R.id.backarrow); Oder hol dir zuerst das Framelayout der Toolbar und hole die Bildschaltfläche daraus.

Verwandte Themen