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 :

Problème nullpointer android


Sujet :

Android

  1. #1
    Membre régulier
    Homme Profil pro
    http://tuatini-godard.me/
    Inscrit en
    Décembre 2010
    Messages
    70
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : http://tuatini-godard.me/

    Informations forums :
    Inscription : Décembre 2010
    Messages : 70
    Points : 93
    Points
    93
    Par défaut Problème nullpointer android
    Bonjour à tous!
    Alors voilà je suis actuellement en train de réaliser une application sur android mais j'éprouve quelques difficultés. Mon problème est que quand j'essaye de récupérer un composant à l'intérieur d'un layout en xml et que j'y applique une de ses méthode, j'obtiens tout simplement une exception.
    Avec le code ci-dessous:
    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
     
    public class WiFileManagerActivity extends Activity{
     
    	public static final int DIALOG_STARTED = 0;
    	public static final int DIALOG_EXITED = 1;
    	public static final int DIALOG_HOST_NOT_FOUND= 2;
    	private ServerInstance serverInstance;
    	private static LookingForHostView lfhv;
    	private CustomHostLoader cusDial = null;
    	private Void v;
     
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
            showDialog(DIALOG_STARTED);
    		lfhv = new LookingForHostView(this.getApplicationContext(), this);
            setContentView(lfhv);
            serverInstance = new ServerInstance(this);
            serverInstance.execute(v);
        }
     
        @Override
        protected Dialog onCreateDialog(int id) {
     
            switch(id) {
            case DIALOG_STARTED:
            	cusDial = new CustomHostLoader(this);
                break;
            case DIALOG_EXITED:
                break;
            case DIALOG_HOST_NOT_FOUND:
            	break;
            }
     
            return cusDial;
        }
     
        public void closeCustomHostLoaderDialog(){
        	if(cusDial != null){
        		cusDial.cancel();
        	}
        }
     
    }
    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
     
    public class CustomHostLoader extends Dialog implements OnClickListener{
    	private TextView hostTv;
    	private ImageButton helpButton;
     
    	public CustomHostLoader(Activity parentActivity) {
    		super(parentActivity);
    		setContentView(R.layout.host_loader);
     
    		hostTv = (TextView) parentActivity.findViewById(R.id.hostTv);
    		//Ici l'exception est déclenchée
    		hostTv.getText();
    	}
     
    	public TextView getHostTv(){
    		return hostTv;
    	}
     
    	@Override
    	public void onClick(View arg0) {
    		Log.i("ProgLog", "Yahoo");
    	}
     
    }
    host_loader.xml
    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
     
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/layout_root"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal"
        android:padding="10dp" >
     
        <ProgressBar
            android:id="@+id/progressBar1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"/>
     
        <TextView
            android:id="@+id/hostTv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/progressBar1"
            android:layout_toRightOf="@+id/progressBar1"
            android:layout_marginTop="18dp"
            android:layout_marginLeft="10dp"
            android:text="Looking for host. Please wait..." />
     
        <EditText
            android:id="@+id/editText1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/progressBar1"
            android:layout_alignRight="@+id/hostTv"
            android:layout_below="@+id/progressBar1"
            android:layout_marginTop="18dp"
            android:inputType="textPersonName" >
        </EditText>
     
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/editText1"
            android:layout_alignRight="@+id/editText1"
            android:layout_below="@+id/editText1"
            android:text="Use this IP as host IP" />
     
        <ImageButton
            android:id="@+id/helpButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/button1"
            android:layout_alignTop="@+id/hostTv"
            android:layout_toRightOf="@+id/hostTv"
            android:src="@drawable/help" />
     
    </RelativeLayout>
    J'obtiens:
    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
    12-11 01:19:06.720: E/AndroidRuntime(23834): FATAL EXCEPTION: main
    12-11 01:19:06.720: E/AndroidRuntime(23834): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.wifilemanager.client/com.wifilemanager.client.activity.WiFileManagerActivity}: java.lang.NullPointerException
    12-11 01:19:06.720: E/AndroidRuntime(23834): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1815)
    12-11 01:19:06.720: E/AndroidRuntime(23834): 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831)
    12-11 01:19:06.720: E/AndroidRuntime(23834): 	at android.app.ActivityThread.access$500(ActivityThread.java:122)
    12-11 01:19:06.720: E/AndroidRuntime(23834): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024)
    12-11 01:19:06.720: E/AndroidRuntime(23834): 	at android.os.Handler.dispatchMessage(Handler.java:99)
    12-11 01:19:06.720: E/AndroidRuntime(23834): 	at android.os.Looper.loop(Looper.java:132)
    12-11 01:19:06.720: E/AndroidRuntime(23834): 	at android.app.ActivityThread.main(ActivityThread.java:4123)
    12-11 01:19:06.720: E/AndroidRuntime(23834): 	at java.lang.reflect.Method.invokeNative(Native Method)
    12-11 01:19:06.720: E/AndroidRuntime(23834): 	at java.lang.reflect.Method.invoke(Method.java:491)
    12-11 01:19:06.720: E/AndroidRuntime(23834): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
    12-11 01:19:06.720: E/AndroidRuntime(23834): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
    12-11 01:19:06.720: E/AndroidRuntime(23834): 	at dalvik.system.NativeStart.main(Native Method)
    12-11 01:19:06.720: E/AndroidRuntime(23834): Caused by: java.lang.NullPointerException
    12-11 01:19:06.720: E/AndroidRuntime(23834): 	at com.wifilemanager.client.dialog.CustomHostLoader.<init>(CustomHostLoader.java:29)
    12-11 01:19:06.720: E/AndroidRuntime(23834): 	at com.wifilemanager.client.activity.WiFileManagerActivity.onCreateDialog(WiFileManagerActivity.java:46)
    12-11 01:19:06.720: E/AndroidRuntime(23834): 	at android.app.Activity.onCreateDialog(Activity.java:2758)
    12-11 01:19:06.720: E/AndroidRuntime(23834): 	at android.app.Activity.createDialog(Activity.java:936)
    12-11 01:19:06.720: E/AndroidRuntime(23834): 	at android.app.Activity.showDialog(Activity.java:2851)
    12-11 01:19:06.720: E/AndroidRuntime(23834): 	at android.app.Activity.showDialog(Activity.java:2810)
    12-11 01:19:06.720: E/AndroidRuntime(23834): 	at com.wifilemanager.client.activity.WiFileManagerActivity.onCreate(WiFileManagerActivity.java:33)
    12-11 01:19:06.720: E/AndroidRuntime(23834): 	at android.app.Activity.performCreate(Activity.java:4397)
    12-11 01:19:06.720: E/AndroidRuntime(23834): 	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
    12-11 01:19:06.720: E/AndroidRuntime(23834): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1779)
    12-11 01:19:06.720: E/AndroidRuntime(23834): 	... 11 more
    Quelqu'un saurait comment résoudre ce problème? Ce que je cherche à faire est simple en fait, c'est de pouvoir récupérer des composants de mon layout host_loader.xml dans le code java de ma classe CustomHostLoader et ainsi les modifier à ma guise, comme faire un hostTv.setText("Hello world!") par exemple. Merci d'avance

  2. #2
    Nouveau membre du Club
    Inscrit en
    Juillet 2009
    Messages
    17
    Détails du profil
    Informations forums :
    Inscription : Juillet 2009
    Messages : 17
    Points : 26
    Points
    26
    Par défaut
    Essaye avec ça :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    hostTv = (TextView) findViewById(R.id.hostTv);

  3. #3
    Membre régulier
    Homme Profil pro
    http://tuatini-godard.me/
    Inscrit en
    Décembre 2010
    Messages
    70
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : http://tuatini-godard.me/

    Informations forums :
    Inscription : Décembre 2010
    Messages : 70
    Points : 93
    Points
    93
    Par défaut
    Merci beaucoup ça fonctionne!! J'avais essayé avec le code suivant auparavant:
    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
     
    public class CustomHostLoader extends Dialog implements OnClickListener{
    	private TextView hostTv;
    	private ImageButton helpButton;
     
    	public CustomHostLoader(Activity parentActivity) {
    		super(parentActivity.getApplicationContext());
    		setContentView(R.layout.host_loader);
     
    		hostTv = (TextView) findViewById(R.id.hostTv);
    		hostTv.setText("Hello world!");
    	}
     
    	public TextView getHostTv(){
    		return hostTv;
    	}
     
    	@Override
    	public void onClick(View arg0) {
    		Log.i("ProgLog", "Yahoo");
    	}
     
    }
    qui renvoyait bien sûr la même erreur... Je dois avouer que android prête vraiment à confusion sur certains aspects. Comme l'impact qu'a le simple fait de ne pas donner le bon argument au constructeur... Merci encore

  4. #4
    Expert éminent

    Avatar de Feanorin
    Profil pro
    Inscrit en
    Avril 2004
    Messages
    4 589
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2004
    Messages : 4 589
    Points : 9 149
    Points
    9 149
    Par défaut
    Bonjour

    Pour tagger votre sujet Résolu merci de bien utiliser le bouton en bas de la page.

    Responsable Android de Developpez.com (Twitter et Facebook)
    Besoin d"un article/tutoriel/cours sur Android, consulter la page cours
    N'hésitez pas à consulter la FAQ Android et à poser vos questions sur les forums d'entraide mobile d'Android.

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

Discussions similaires

  1. Problème Clavier Android
    Par ihebweb dans le forum Android
    Réponses: 4
    Dernier message: 07/04/2011, 15h23
  2. Problème connexion Android
    Par medi88 dans le forum API standards et tierces
    Réponses: 5
    Dernier message: 29/03/2011, 14h21
  3. problème clavier android affiche des caractères chinois
    Par étudiante_info dans le forum Android
    Réponses: 0
    Dernier message: 22/03/2011, 11h07
  4. Problème téléchargement Android SDK
    Par Invité dans le forum Android
    Réponses: 6
    Dernier message: 28/10/2010, 18h13
  5. Probléme telechargement ANDROID sdk
    Par badr007 dans le forum Android
    Réponses: 3
    Dernier message: 31/07/2010, 23h45

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