Java Android Studio - Activity in BottomNavigation wechseln

A456B123

Ensign
Registriert
März 2011
Beiträge
206
Hallo :),

könnt ihr mir sagen wie ich bei einer Navigationsleiste von der einen Activity zur anderen wechseln kann?

So sieht mein Code momentan aus:

MainActivity

Code:
public class MainActivity extends AppCompatActivity{


    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_licht:
                    Intent intent = new Intent(MainActivity.this, Lampe.class);
                    startActivity(intent);
                    return true;
                //case... weitere Activities

            }
            return false;
        }
    };

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

}

Lampe (zu dieser möchte ich von der MainActivity wechseln)

Code:
public class Lampe extends Activity{

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

}

Vielen Dank für eure Hilfe und einen schönen Feierabend :D
 
Was genau ist denn der Fehler? Passiert einfach nichts nach dem Klicken?

Dein Layout wäre noch interessant. Setzt du eine menu.xml mit einem item @+id/navigation_licht? Also funktioniert dein Listener und du kommst in deine switch-Bedingung (Zeile 11-13)?

Hast du die Activity in deiner AndroidManifest.xml definiert?
 
Danke für deine Antwort :)

Es wechselt zwar die Activity, jedoch wird nicht das richtige XML Layout angezeigt. Hier ist das Manifest:

Code:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.user.solar">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />


                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Lampe"/>
    </application>

</manifest>

Die Klasse Main Activity sieht jetzt so aus:

Code:
public class MainActivity extends AppCompatActivity {



    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_dashboard:
                    Intent intent = new Intent(MainActivity.this, Lampe.class);
                    startActivity(intent);
                    return true;
            }
            return false;
        }
    };

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


        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
    }

}

Kann es sein das beim Mainfest unter Intent Filter etwas fehlt?
Ergänzung ()

Hier ist noch das Layout der activity_main:

Code:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.user.solar.MainActivity">

    <TextView
        android:id="@+id/message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/activity_horizontal_margin"
        android:layout_marginStart="@dimen/activity_horizontal_margin"
        android:layout_marginTop="@dimen/activity_vertical_margin"
        android:text="@string/title_home"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="0dp"
        android:layout_marginStart="0dp"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/navigation" />

</android.support.constraint.ConstraintLayout>
 
A456B123 schrieb:
Es wechselt zwar die Activity, jedoch wird nicht das richtige XML Layout angezeigt.
Das ist schon mal eine andere Aussage, als du zu erst behauptet hast. Was soll den jetzt "nicht das richtige Layout" heißen? Kann es vielleicht sein, dass vom Programmablauf her alles korrekt ist, das Layout "lampe" aber nicht so aussieht wie du es dir vorgestellt hast?

Pro-Tipp: Benenne deine Klassen und Ressourcen so, dass klar ist um was es sich handelt. "lampe" ist nicht wirklich aussagekräftig.
 
Danke für deine Antwort.

Es wurden nur keine Bilder angezeigt. Alle anderen Elemente werden geladen das reicht fürs erste :p

Danke für eure Hilfe :D
 
Zurück
Oben