2017-12-07 5 views
-4

Ich habe zwei activities, versuchen, von der ersten auf die zweite auf Knopfdruck zu navigieren. Erstens ist wie folgt aus:Kann nicht zwischen den Aktivitäten navigieren

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.activity_first); 

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab_new_message); 
    fab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent myIntent = new Intent(FirstActivity.this, SecondActivity.class); 
      startActivity(myIntent); 
     } 
    }); 
} 

Und das zweite ist wie folgt aus:

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.activity_second); 
    // other stuff 
} 

Wenn die Floating Action Button dann gedrückt wird, absolut nichts passiert. Was ist falsch an meinem Code?

Update: Dies ist das Manifest:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="..."> 

    <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=".firstActivity"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

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

    </application> 

</manifest> 

Und dies ist der Code für die Schaltfläche Aktion Schwimmdock:

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
    //Button button = (Button) findViewById(R.id.button); 
    fab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Toast toast = Toast.makeText(getApplicationContext(), "Pressed!", Toast.LENGTH_LONG); 
      toast.show(); 
         } 
    }); 

In XML:

<android.support.design.widget.FloatingActionButton 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:clickable="true" 
    android:id="@+id/fab" 
    android:tint="@android:color/white" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentEnd="true" 
    app:fabSize="mini" 
    android:layout_alignParentRight="true" /> 

Das gesamte Layout:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 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/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.design.widget.FloatingActionButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:clickable="true" 
     android:id="@+id/fab" 
     android:tint="@android:color/white" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentEnd="true" 
     app:fabSize="mini" 
     android:layout_alignParentRight="true" /> 

    <ListView 
     android:id="@+id/list_of_messages" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_alignParentTop="true" 
     android:clickable="true" 
     android:divider="@android:color/transparent" 
     android:dividerHeight="16dp" 
     android:focusable="auto" 
     android:focusableInTouchMode="true" /> 
</RelativeLayout> 
+1

Check in Ihrem Manifest, wenn Sie Ihre 'SecondActivity' hinzugefügt – tahsinRupam

+0

Sie nicht Eintrag von SecondActivity in AndroidManifest.xml –

+1

Haben Sie SecondActivity in Manifest hinzugefügt? Ich sollte abstürzen, denke ich. Aber du sprichst an, dass nichts passiert? – ADM

Antwort

0

Versuchen Sie, diese in Ihrer Manifest.xml Datei zu ändern:

<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=".FirstActivity"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

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

</application> 

Bearbeiten-
Um FAB nach vornem Einsatz zu bringen:
fab.bringToFront(); vor onClickListener, um es zu setzen.

Dank für den Hinweis auf Mike M

+0

mit @global_Warming vereinbart. – InsaneCat

+0

Wenn es ein offensichtliches Problem wäre, wäre es abgestürzt, anstatt "absolut nichts" zu tun. –

+0

Ja, das ist wahr, aber mindestens das Manifest ist falsch und die Frage ist chaotisch geworden + kein Zeichen von Stacktrace. –

0

Verwendung Im Folgenden Code Ihr Problem zu lösen, für mich zu arbeiten:

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
    fab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
        .setAction("Action", null).show(); 
     } 
    }); 

    <android.support.design.widget.FloatingActionButton 
    android:id="@+id/fab" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="bottom|end" 
    android:layout_margin="@dimen/fab_margin" 
    app:srcCompat="@android:drawable/ic_dialog_email" /> 
Verwandte Themen