Der Zurück-Button wird nicht angezeigt

R

-Rockbiest-

Neues Mitglied
0
Hallo Zusammen,

ich bin Neuling und habe aus meiner Unwissenheit heraus ein Problem, welches vermutlich keines ist :D

Ich möchte über ein Menü einen "Zurück"-Button, oben rechts einblenden lassen.
Ich habe folgende Komponenten:
1- .Java (AuftraegeEditActivity)
2- .Java Fragements, da es eine tabbedActitvity ist
3- activityEdit .xml
4- app_bar_auftraege_edit.xml (hier ist die Appbar inkl. eines Logos + darunter eine weitere bar für die Tabs)
5- die einzelnen fragement.xml für den jeweiligen Content
6- main.xml (das Menü, wo der Backbutton draufliegt)

Funktioniert auch alles untereinander, der Backbutton wird jedoch nicht angezeigt.
Was muss ich tun?
Nachfolgend meine Codes:

Ausschnitt aus AufraegeEditActivity.java:

Code:
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_auftraege_edit);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

        setSupportActionBar(toolbar);

        MyApplication app = (MyApplication) getApplication();
        con = connectionclass(app.getUn(), app.getPass(), app.getDb(), app.getIp());

      ...................

       mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(mViewPager);
}


    //Zurück-Button
    @Override
    public boolean onCreateOptionsMenu (Menu menu)
    {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.main, menu);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected (MenuItem item)
    {
        int res_id = item.getItemId();
        if(res_id==R.id.action_back)
        {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


AUSZUG AUS MAIN.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu 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"
    tools:context="iresapp.iresapp2.AuftraegeEditActivity">

    <item
        android:id="@+id/action_back"
        android:orderInCategory="100"
        android:icon="@drawable/ic_menu_back"
        android:title="@string/action_back"
        app:showAsAction="always"
        android:visible="true"/>
</menu>
 
Es wäre gut wenn Du schreibst welches Handy Du hast????
 
  • Danke
Reaktionen: Wattsolls
Lieber Kollege die Frage ist berechtigt. Aber ich glaube da sucht jemand Hilfe beim Programmieren einer App. Darum keine Geräte Angabe. ;)
 
  • Danke
Reaktionen: Reddy
Für mich sieht das nicht danach aus, dass es um ein spezielles Handy geht. Es sieht für mich vielmehr nach App-Programmierung aus ;) Ahhhhh, @Wattsolls war minimal schneller :crying:

@-Rockbiest- Noch ein herzliches Willkommen hier im Forum ;)
 
  • Danke
Reaktionen: Reddy
  • Danke
Reaktionen: Reddy und DPX
Halli Hallo, danke fürs Willkommen :D
Habe ein Samsung Galaxy S5, aber im Simulator ein Nexus.
Funktioniert gleichermaßen nicht.

Habe ich mich hier im falschen Forum verirrt ??
 
Hast Du. Das ist hier nur der Bereich für persönliche Vorstellungen. ;) Aber einer der zuständigen Kollegen wird Dich sicher gleich in den Entwicklerbereich verschieben. :)
 
  • Danke
Reaktionen: Reddy
Welcher Backbutton? Bitte einen genauere Beschreibung des Problems. Meinst du vielleicht den Pfeil in der oberen Leiste (Statusbar)?

Danke für alle zusätzlichen Informationen.
 
Hallo,

ja genau der ist gemeint :)
Quasi zu finden hier:

Code:
<?xml version="1.0" encoding="utf-8"?>
<menu 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"
    tools:context="iresapp.iresapp2.AuftraegeEditActivity">

    <item
        android:id="@+id/action_back"
        android:orderInCategory="100"
        android:icon="@drawable/ic_menu_back"
        android:title="@string/action_back"
        app:showAsAction="always"
        android:visible="true"/>
</menu>

Meine Vermutung ist, dass er es nicht anzeigt, weil ich eine toolbar in die activity reinlade:
aus meiner Activity:

Code:
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_auftraege);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        addListenerOnButton();
    }

Mhm.... was sonst lässt sich sagen...
auf manchen Seiten habe ich noch, so ein Slideing Menu, welches man sich von links reinziehen kann.
Aber das wird vermutlich nicht die Fehlerquelle sein, da er den Button nirgends anzeigt, auch dort nicht, wo das Menu nicht drin ist.
 
Hallo,

da muss etwas in der Form stehen.

Code:
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getActionBar().setDisplayHomeAsUpEnabled(true); // ab API 11 und höher
        // bzw für AppCompat (Support)
        // getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }


Und noch ein paar allgemeinere Informationen:
Navigation with Back and Up | Android Developers
 
Hallo,

danke für den Tipp.
Leider war ich schon mal so weit.
Der Code produziert immer einen Fehler, den ich nicht so recht zu beheben weiß :-/

Nachfolgend:
Code:
FATAL EXCEPTION: main
                  Process: app.app2, PID: XXX23
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{app.app2/app.app2.AuftraegeActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2658)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2723)
                      at android.app.ActivityThread.access$900(ActivityThread.java:172)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:145)
                      at android.app.ActivityThread.main(ActivityThread.java:5832)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:372)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
                   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference
                      at iresapp.iresapp2.AuftraegeActivity.onCreate(AuftraegeActivity.java:30)
                      at android.app.Activity.performCreate(Activity.java:6221)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2611)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2723)
                      at android.app.ActivityThread.access$900(ActivityThread.java:172)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:145)
                      at android.app.ActivityThread.main(ActivityThread.java:5832)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:372)
 
NullPointerException in der AuftraegeActivity Zeile 30. Dort fehlt eine Objekt.
(at iresapp.iresapp2.AuftraegeActivity.onCreate(AuftraegeActivity.java:30))

Ich denke mal, es ist die Zeile Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); Die App findet wahrscheinlich im xml R.layout.auftraege_activity den Node toolbar nicht. Wenn das System den Node nicht findet, liefert die Methode findViewById() null zurück.
 
Die R.layout.activity_auftraege includiert....

Code:
<include
        layout="@layout/app_bar_auftraege"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

app_bar_auftraege, und in app_bar_auftraege hab ich folgenden Inhalt:

Code:
<?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="app.app2.AuftraegeActivity">

    <include layout="@layout/content_auftraege" />  // hier ist der Verweis wiederum auf den Inhalt

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="55dp"
        android:theme="@style/AppTheme.AppBarOverlay">

        <ImageView                      //Hier ist das Logo innerhalb der AppBar hinterlegt
            android:id="@+id/imageView2"
            android:layout_width="138dp"
            android:layout_height="57dp"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            app:srcCompat="@drawable/logo"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true" />

        <android.support.v7.widget.Toolbar
            style="@style/ToolBarStyle"
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/toolbar"                              // und hier ist die die id: toolbar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimaryDark"
            android:minHeight="@dimen/abc_action_bar_default_height_material"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true" />

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

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

also eigentlich müsste er die toolbar finden, oder nicht?
 
Was steht genau in Zeile 30? (Das vorhin war nur eine Vermutung).
 
Da steht folgendes:
getActionBar().setDisplayHomeAsUpEnabled(true); // ab API 11 und höher

Und da steht wiederum folgender Hinweis:

Method invocation 'setDisplayHomeAsUpEnabled' may produce 'java.lang.NullPointerException' less... (Strg+F1)

This inspection analyzes method control and data flow to report possible conditions that are always true or false, expressions whose value is statically proven to be constant, and situations that can lead to nullability contract violations.

Variables, method parameters and return values marked as @Nullable or @NotNull are treated as nullable (or not-null, respectively) and used during the analysis to check nullability contracts, e.g. report possible NullPointerException errors.

More complex contracts can be defined using @Contract annotation, for example:

@Contract("_, null -> null") — method returns null if its second argument is null
@Contract("_, null -> null; _, !null -> !null") — method returns null if its second argument is null and not-null otherwise
@Contract("true -> fail") — a typical assertFalse method which throws an exception if true is passed to it

The inspection can be configured to use custom @Nullable

@NotNull annotations (by default the ones from annotations.jar will be used)

ÜBERSETZUNG

Diese Inspektion analysiert Methodensteuerung und Datenfluss, um mögliche Bedingungen zu berichten, die immer wahr oder falsch sind, Ausdrücke, deren Wert statisch als konstant beurteilt wird, und Situationen, die zu Verletzungen des NULL-Vertrags führen können.

Variablen, Methodenparameter und Rückgabewerte, die als @Nullable oder @NotNull markiert sind, werden als nullable (bzw. nicht-null) behandelt und während der Analyse zur Prüfung von NULL-Kontrakten, z.B. Bericht mögliche NullPointerException Fehler.

Komplexere Verträge können mit der @Contract-Annotation definiert werden, zB:

@Contract ("_, null -> null") - Methode gibt null zurück, wenn das zweite Argument null ist
@Contract ("_, null -> null; _,! Null ->! Null") - Methode gibt null zurück, wenn ihr zweites Argument null und nicht null ist
@Contract ("true -> fail") - eine typische assertFalse-Methode, die eine Exception auslöst, wenn true an sie übergeben wird

Die Inspektion kann so konfiguriert werden, dass benutzerdefinierte @Nullable verwendet werden

@NotNull Anmerkungen (standardmäßig werden die von annotations.jar verwendet)
 
getActionBar() ist null! Benutzt du die Support Library?

Dann müsste dort
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
stehen.
 
Falls es hilft und ich die Frage richtig deuten konnte: So habe ich bei mir den Backbutton eingebunden..
Code:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setBackgroundColor(Color.RED);
        setSupportActionBar(toolbar);
        // add back arrow in toolbar
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayShowHomeEnabled(true);
        }
        // arrow click event
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // mach irgendwas beim Klicken
            }
        });
 

Ähnliche Themen

A
  • AnimaAngelo85
Antworten
1
Aufrufe
303
swa00
swa00
MES
Antworten
10
Aufrufe
774
MES
MES
C
Antworten
8
Aufrufe
1.105
swa00
swa00
Zurück
Oben Unten