IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Android Discussion :

Erreur "Unable to instantiate activity"


Sujet :

Android

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éclairé
    Homme Profil pro
    Inscrit en
    Février 2013
    Messages
    606
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Février 2013
    Messages : 606
    Par défaut Erreur "Unable to instantiate activity"
    Bonjour,
    Lorsque je compile mon programme j'obtiens cette erreur


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
     
    03-28 20:25:35.571      949-949/com.example.user.cercles E/AndroidRuntime﹕ FATAL EXCEPTION: main
        java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.user.cercles/com.example.user.cercles.MainActivity}: java.lang.NullPointerException
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
                at android.app.ActivityThread.access$600(ActivityThread.java:130)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
                at android.os.Handler.dispatchMessage(Handler.java:99)
                at android.os.Looper.loop(Looper.java:137)
                at android.app.ActivityThread.main(ActivityThread.java:4745)
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:511)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
                at dalvik.system.NativeStart.main(Native Method)
         Caused by: java.lang.NullPointerException
                at android.content.ContextWrapper.getResources(ContextWrapper.java:81)
                at android.view.View.<init>(View.java:3228)
                at com.example.user.cercles.MyView.<init>(MyView.java:25)
                at com.example.user.cercles.MainActivity.<init>(MainActivity.java:59)
                at java.lang.Class.newInstanceImpl(Native Method)
                at java.lang.Class.newInstance(Class.java:1319)
                at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
    ************at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
    ************at android.app.ActivityThread.access$600(ActivityThread.java:130)
    ************at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
    ************at android.os.Handler.dispatchMessage(Handler.java:99)
    ************at android.os.Looper.loop(Looper.java:137)
    ************at android.app.ActivityThread.main(ActivityThread.java:4745)
    ************at java.lang.reflect.Method.invokeNative(Native Method)
    ************at java.lang.reflect.Method.invoke(Method.java:511)
    ************at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
    ************at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    ************at dalvik.system.NativeStart.main(Native Method)
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
     
    public class MainActivity extends ActionBarActivity {
     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            //setContentView(new MyView(this));
     
            Button boutonOk = (Button)findViewById(R.id.ok);
            boutonOk.setOnClickListener(okOnClick);
     
        }
     
     
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu_main, menu);
            return true;
        }
     
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
     
            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                return true;
            }
     
            return super.onOptionsItemSelected(item);
        }
     
     
        EditText varNbCercle;
        EditText varEpaisseur;
     
        String StrNbCercle;
        String StrEpaisseur;
     
        int intNbCercle;
        int intEpaisseur;
     
       MyView Cercle = new MyView(this);
     
     
       private View.OnClickListener okOnClick = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
     
                varNbCercle = (EditText) findViewById(R.id.nbCercle);
                varEpaisseur = (EditText) findViewById(R.id.epaisseur);
     
                StrNbCercle = varNbCercle.getText().toString();
                StrEpaisseur = varEpaisseur.getText().toString();
     
                try {
                    intNbCercle = Integer.parseInt(StrNbCercle);
                    intEpaisseur = Integer.parseInt(StrEpaisseur);
     
                    Cercle.setNbCercle(intNbCercle);
                    Cercle.setEpaisseur(intEpaisseur);
     
                    Cercle.invalidate();//force l'appel de onDraw()
     
                }
     
                catch (NumberFormatException nfe) {}
     
            }
        };
     
    }

  2. #2
    Expert confirmé

    Homme Profil pro
    Ingénieur systèmes et réseaux
    Inscrit en
    Février 2007
    Messages
    4 253
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Ingénieur systèmes et réseaux
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Février 2007
    Messages : 4 253
    Billets dans le blog
    3
    Par défaut
    Normal

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    MyView Cercle = new MyView(this);
    Est executé lors de l'instanciation de l'activité... hors une "view" a besoin d'un contexte (Context) complètement initialisé pour fonctionner, ce qui n'est pas encore le cas à ce moment là => NullPointerException.

    Il faut mettre ce code dans le onCreate au pire.
    au mieux, carrément rajouter dans le layout de l'activité une view de type "MyView".

  3. #3
    Membre éclairé
    Homme Profil pro
    Inscrit en
    Février 2013
    Messages
    606
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Février 2013
    Messages : 606
    Par défaut
    Bonjour,Quand je met MyView Cercle = new MyView(this); dans onCreate() j'ai des erreurs sur
    Cercle.setNbCercle(intNbCercle);
    Cercle.setEpaisseur(intEpaisseur);
    Cercle.invalidate();
    Cannot resolve symbol Cercle

    j'ai mis MyView dans le layout mais après comment puis-je récupérer les données provenant des EditText et les envoyer dans la classe MyView par le biais des set ?

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
     
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
        android:id="@+id/background"
        android:background="#ffffffff"
        android:layout_alignParentBottom="true"
        android:layout_below="@+id/linearLayout">
     
     
     
     <!-- ++++++++++++++++ La zone contenant les textEdit + bouton ok ++++++++++++++++ -->
     
    <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="50dp"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:background="#ff000000"
            android:id="@+id/linearLayout">
     
    <!-- nombre de cercle -->
            <EditText
                android:layout_weight="1"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="5dp"
                android:layout_marginBottom="5dp"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:text="cercle"
                android:id="@+id/nbCercle"
                android:background="#ffffffff"
                android:gravity="center" />
     
    <!-- épaisseur -->
            <EditText
                android:layout_weight="1"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="5dp"
                android:layout_marginBottom="5dp"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:text="épaisseur"
                android:id="@+id/epaisseur"
                android:background="#ffffffff"
                android:gravity="center"
                android:singleLine="false" />
     
            <Button
                android:layout_weight="0"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="OK"
                android:id="@+id/ok"
                android:onClick="okOnClick" />
        </LinearLayout>
     
     
        <!-- ++++++++++++++++ La zone contenant les cercles ++++++++++++++++ -->
     
        <FrameLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_below="@+id/linearLayout"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:background="#ff000000"
            >
     
            <view
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                class="com.example.idriss.cercles.MyView"
                android:id="@+id/view"
                android:layout_gravity="center" />
        </FrameLayout>
     
    </RelativeLayout>

  4. #4
    Expert confirmé

    Homme Profil pro
    Ingénieur systèmes et réseaux
    Inscrit en
    Février 2007
    Messages
    4 253
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Ingénieur systèmes et réseaux
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Février 2007
    Messages : 4 253
    Billets dans le blog
    3
    Par défaut
    Oui il faut que la variable conserve sa portée (l'instance d'activité), donc *déclarer* celle-ci dans l'activité:
    Par contre, l'instanciation de cet élément doit être dans le onCreate:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    cercle = new MyView(this);

    Sinon, je ne vois pas dans le XML d'instanciation de MyView.... si c'est le "view" à la fin, ce n'est pas bon:
    Ce doit être un truc genre:
    Code xml : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
            <my.package.MyView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                class="com.example.idriss.cercles.MyView"
                android:id="@+id/view"
                android:layout_gravity="center" />
    (au passage, l'éditeur visuel doit proposer "MyView" dans les custom-views pour éviter de se taper le XML à la main)

    Et ensuite dans le code (après le setContentView de l'activité bien entendu):
    Code java : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    cercle = (MyView) findViewById(R.id.view);

  5. #5
    Membre éclairé
    Homme Profil pro
    Inscrit en
    Février 2013
    Messages
    606
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Février 2013
    Messages : 606
    Par défaut
    Bonjour,
    Je n'ai pas ajouter MyView view dans le fichier activity_main.xml à la main, j'ai ajouté la classe MyView à partir de l'éditeur visuel et il a généré ce code.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
            <view
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                class="com.example.idriss.cercles.MyView"
                android:id="@+id/view"
                android:layout_gravity="center" />
    je ne comprends toujours pas comment setter les valeurs dans la classe MyView

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
     
    public class MainActivity extends ActionBarActivity {
     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
     
            MyView cercle = (MyView) findViewById(R.id.view);
            Button boutonOk = (Button)findViewById(R.id.ok);
            boutonOk.setOnClickListener(okOnClick);
     
        }
     
     
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu_main, menu);
            return true;
        }
     
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
     
            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                return true;
            }
     
            return super.onOptionsItemSelected(item);
        }
     
     
        EditText varNbCercle;
        EditText varEpaisseur;
     
        String StrNbCercle;
        String StrEpaisseur;
     
        int intNbCercle;
        int intEpaisseur;
     
       //MyView Cercle = new MyView(this);
     
     
       private View.OnClickListener okOnClick = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
     
                varNbCercle = (EditText) findViewById(R.id.nbCercle);
                varEpaisseur = (EditText) findViewById(R.id.epaisseur);
     
                StrNbCercle = varNbCercle.getText().toString();
                StrEpaisseur = varEpaisseur.getText().toString();
     
                try {
                    intNbCercle = Integer.parseInt(StrNbCercle);
                    intEpaisseur = Integer.parseInt(StrEpaisseur);
     
                    cercle.setNbCercle(intNbCercle);
                    cercle.setEpaisseur(intEpaisseur);
                    cercle.invalidate();//force l'appel de onDraw()
     
                }
     
                catch (NumberFormatException nfe) {}
     
            }
        };
     
    }

  6. #6
    Membre éclairé
    Homme Profil pro
    Inscrit en
    Février 2013
    Messages
    606
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Février 2013
    Messages : 606
    Par défaut
    Bonjour,
    j'ai mis cercle = (MyView) findViewById(R.id.view); dans la méthode okOnClick() et ca fonctionne bien
    merci de votre aide

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. many-to-one, erreur "Unable to instantiate default tuplizer"
    Par asmafouni dans le forum Hibernate
    Réponses: 1
    Dernier message: 22/04/2013, 12h08
  2. Erreur "unable to stop activity"
    Par cecile38 dans le forum Android
    Réponses: 2
    Dernier message: 19/11/2012, 11h44
  3. Réponses: 0
    Dernier message: 11/08/2011, 17h21
  4. Réponses: 4
    Dernier message: 07/10/2005, 15h28

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo