Arret Brutal de l'application

Résolu/Fermé
aliounemane2 Messages postés 28 Date d'inscription mardi 16 décembre 2014 Statut Membre Dernière intervention 5 octobre 2017 - 5 févr. 2015 à 23:54
 John82 - 6 févr. 2015 à 00:10
Bonjour j'utilise comme IDE android Studio et en exécutant mon code mon application se lance au bout de 10secondes et se ferme et m'envoie un message sous forme de boite de dialogue en me disant que Test (c'est le nom de l'application) est arrété
voici le code fragment_main.xml

<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout
    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:orientation="vertical"
    >

   <TextView
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:id="@+id/textSexe"
       android:text="Quel est votre sexe :"
       />

    <ListView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:choiceMode="singleChoice"
        android:id="@+id/listSexe"
        >
    </ListView>

    <TextView
        android:id="@+id/textProg"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Quelles sont les langages de Programmations"
        />
    <ListView
        android:id="@+id/listProg"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:choiceMode="multipleChoice"
        >

    </ListView>

    <Button
        android:id="@+id/send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Envoyer"
        />

</LinearLayout>



VOICI le fichier MainActivity.java

package com.example.nema.test;
 
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
 
 
public class MainActivity extends Activity {
 
        private ListView mListSexe = null;
        private ListView mListProg = null;
        private Button msend = null;
        private  String[] msexes = {"Masculin","Feminin"};
        private String[] mLangages = null;
 
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        mListSexe = (ListView) findViewById(R.id.listSexe);
        mListProg= (ListView) findViewById(R.id.listProg);
        msend = (Button) findViewById(R.id.send);
 
        mLangages = new String[]{"C","Java","Cobol","Perl"};
 
        mListSexe.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_single_choice,msexes));
        mListSexe.setItemChecked(0,true);
 
        mListProg.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice,mLangages));
        mListProg.setItemChecked(1,true);
 
        msend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this," Vos données ont été envoyées avec succes",Toast.LENGTH_SHORT).show();
 
                mListSexe.setChoiceMode(ListView.CHOICE_MODE_NONE);
 
                mListSexe.setAdapter(new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,msexes));
 
                mListProg.setChoiceMode(ListView.CHOICE_MODE_NONE);
 
                mListProg.setAdapter(new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,mLangages));
 
                msend.setEnabled(false);
            }
        });
 
 
        }
    }



et enfin voici le fichier activity_main.xml


<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    tools:ignore="MergeRootFrame"
    />




Voici ce qui est affiché dans le Logcat


02-05 22:49:17.139  19589-19589/com.example.nema.test D/ActivityThread? setTargetHeapUtilization:0.25
02-05 22:49:17.149  19589-19589/com.example.nema.test D/ActivityThread? setTargetHeapIdealFree:8388608
02-05 22:49:17.149  19589-19589/com.example.nema.test D/ActivityThread? setTargetHeapConcurrentStart:2097152
02-05 22:49:17.409  19589-19589/com.example.nema.test D/AndroidRuntime? Shutting down VM
02-05 22:49:17.409  19589-19589/com.example.nema.test W/dalvikvm? threadid=1: thread exiting with uncaught exception (group=0x40e51438)
02-05 22:49:17.409  19589-19589/com.example.nema.test E/AndroidRuntime? FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nema.test/com.example.nema.test.MainActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2114)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2139)
            at android.app.ActivityThread.access$700(ActivityThread.java:143)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4963)
            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:1038)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.example.nema.test.MainActivity.onCreate(MainActivity.java:40)
            at android.app.Activity.performCreate(Activity.java:5184)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2078)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2139)
            at android.app.ActivityThread.access$700(ActivityThread.java:143)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4963)
            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:1038)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
            at dalvik.system.NativeStart.main(Native Method)

1 réponse

Tout simplement dans MainActivity tu fais appel au layout activity_main tandis que la listView que tu essaye de référencer se trouve dans fragment_main.xml.

Essaye de faire un setContentView(R.layout.fragment_main)
0