Aller au contenu
View in the app

A better way to browse. Learn more.

Forum de Frandroid

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Problème de FloatingActionButton

Featured Replies

Posté(e)

Bonjour à tous,,

 

Je suis nouveau ici et je viens de me lancer dans mon tout premier projet d'application android. J'ai besoin d'aide. Pour l'instant, j'ai sur ma MainActivity un calendarView et un bouton flottant. Le bouton sert à ouvrir une seconde activité. Mais, chaque fois que j'appuie sur le bouton, mon appli se ferme.

J'ai tenté de modifier la déclaration du bouton (le faire en dehors ou dans la fonction onCreate, ne déclarer que le boutuon et l'id correspondant). De plus, j'ai tenté aussi de nettoyer le programme, comme indiqué sur le site Stack Overflow (Analyze -> Code Cleaner). Et aussi de rajouter l'interface View.OnClickListener à la class MainActivity mais cela génere une autre erreur (comme quoi il ne trouve pas l'interface). Si vous pouviez m'éclairer car ça fait déjà 2 3 jours que cela m’empêche d'avancer. 

 

Voici ma dernière tentative de code :

package com.example.etis.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.app.Activity;
import android.widget.CalendarView;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

    CalendarView calendar;

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

        setContentView(R.layout.activity_main);
        calendar = (CalendarView) findViewById(R.id.calendarView);
        calendar.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
            @Override
            public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
                Toast.makeText(getApplicationContext(), dayOfMonth + "/" + month + "/" + year, Toast.LENGTH_LONG).show();
            }
        });
		init();
    }
        public void init() {
            Button floatingButton = (Button)findViewById(R.id.button);
            floatingButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                startActivity(intent);
            }
        });
        }
}

 

et le 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.etis.myapplication.MainActivity">

    <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="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/button"
        android:layout_width="188dp"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@android:drawable/ic_input_add"
        android:onClick="onClick"/>

    <CalendarView
        android:layout_width="match_parent"
        android:layout_height="371dp"
        android:id="@+id/calendarView" />

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

 

et les erreurs

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.etis.myapplication, PID: 3170
                  java.lang.IllegalStateException: Could not find method onClick(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.design.widget.FloatingActionButton with id 'button'
                      at android.view.View$DeclaredOnClickListener.resolveMethod(View.java:4730)
                      at android.view.View$DeclaredOnClickListener.onClick(View.java:4689)
                      at android.view.View.performClick(View.java:5610)
                      at android.view.View$PerformClick.run(View.java:22265)
                      at android.os.Handler.handleCallback(Handler.java:751)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:154)
                      at android.app.ActivityThread.main(ActivityThread.java:6077)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

 

Merci

Modifié par cnz018
Correction

Rejoignez la conversation

Vous pouvez poster maintenant et vous enregistrez plus tard. Si vous avez un compte, connectez-vous maintenant pour poster.

Invité
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Répondre à ce sujet…

Account

Navigation

Rechercher

Rechercher

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.