Jump to content

Problème de FloatingActionButton


cnz018

Recommended Posts

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

Edited by cnz018
Correction
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...