Android que j'ai tort? exception non interceptée (groupe = 0x4001d800)

Ce travail est basé sur l'exemple de Patel Deval.

L'application a l'intention de charger le contenu d'ImageSwicher basé sur le bouton radio choisi. Pendant l'exécution, l'exception non interceptée (groupe = 0x4001d800) est imprimée. Où ai-je tort? Voici le code

private Gallery gallery;

private RadioGroup rdgSelection;

private ImageSwitcher isSwitcher;

private Boolean flag = true;

private int radioCheckedId = -1; //no radio button

//arrays of image for alerts

private Integer[] pics ={ R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d,R.drawable.e,R.drawable.exit, R.drawable.plan, R.drawable.icon, R.drawable.plan };

private Integer[] others = {R.drawable.exit, R.drawable.plan, R.drawable.icon, R.drawable.plan,R.drawable.c };



/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sendalert);

    gallery = (Gallery)findViewById(R.id.imgGallery);

    isSwitcher = (ImageSwitcher)findViewById(R.id.ImgSwith);

    rdgSelection = (RadioGroup)findViewById(R.id.radEvent);


    isSwitcher = setFactory(this);

    //get radio button id

    rdgSelection.check(rdgSelection.getChildAt(rdgSelection.getChildCount() - 1).getId());

    if (rdgSelection.getCheckedRadioButtonId()==R.id.radBtnAccident){

        flag = true;


                    gallery.setAdapter(new ImageAdapter(MsgToWeb.this));

    }else{

        flag=false;

        gallery.setAdapter(new ImageAdapter(MsgToWeb.this));

    }

     rdgSelection.setOnCheckedChangeListener(new OnCheckedChangeListener() {


            public void onCheckedChanged(RadioGroup group, int checkedId) {

                gallery.setAdapter(new ImageAdapter(MsgToWeb.this));

                if (rdgSelection.getCheckedRadioButtonId() == R.id.radBtnAccident)

                    flag = true;

                else

                    flag = false;
            }
        });

        gallery.setOnItemSelectedListener(this);

}


private ImageSwitcher setFactory(MsgToWeb msgToWeb) {
   //TODO Auto-generated method stub
    return null;
}


public class ImageAdapter extends BaseAdapter {

    private Context mContext;

    public ImageAdapter(Context c) {
        mContext = c; 

    }

    public Object getItemId() {
       //TODO Auto-generated method stub
        return null;
    }

    public int getCount() {
        //return num of arrays
        if (flag)
            return pics.length;
        else
            return others.length;
    }

    public Object getItem(int position) {
       //TODO Auto-generated method stub
        return position;
    }

    public long getItemId(int position) {
       //TODO Auto-generated method stub
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        ImageView i = new ImageView(mContext);

        if(flag)
            i.setImageResource(pics[position]);
        else
            i.setImageResource(others[position]);

        return i;   
    }

}

public View makeView() {
   //TODO Auto-generated method stub

    ImageView iView = new ImageView(this);

    iView.setBackgroundColor(0xFF000000);

    iView.setScaleType(ImageView.ScaleType.FIT_CENTER);

    iView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));

    return iView;

}


public void onItemSelected(AdapterView<?> arg0, View v, int position,long id) {
   //TODO Auto-generated method stub


    if(flag)
        isSwitcher.setImageResource(pics[position]);
    else
        isSwitcher.setImageResource(others[position]);


}


public void onNothingSelected(AdapterView<?> arg0) {
   //TODO Auto-generated method stub

}


}

Ici le journal

05-24 10:07:03.394: W/dalvikvm(321): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
05-24 10:07:03.404: E/AndroidRuntime(321): FATAL EXCEPTION: main
05-24 10:07:03.404: E/AndroidRuntime(321): java.lang.NullPointerException
05-24 10:07:03.404: E/AndroidRuntime(321):  at com.moLaroute.mu.MsgToWeb.onItemSelected(MsgToWeb.java:147)
05-24 10:07:03.404: E/AndroidRuntime(321):  at android.widget.AdapterView.fireOnSelected(AdapterView.java:864)
05-24 10:07:03.404: E/AndroidRuntime(321):  at android.widget.AdapterView.access$200(AdapterView.java:42)
05-24 10:07:03.404: E/AndroidRuntime(321):  at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:830)
05-24 10:07:03.404: E/AndroidRuntime(321):  at android.os.Handler.handleCallback(Handler.java:587)
05-24 10:07:03.404: E/AndroidRuntime(321):  at android.os.Handler.dispatchMessage(Handler.java:92)
05-24 10:07:03.404: E/AndroidRuntime(321):  at android.os.Looper.loop(Looper.java:123)
05-24 10:07:03.404: E/AndroidRuntime(321):  at android.app.ActivityThread.main(ActivityThread.java:4627)
05-24 10:07:03.404: E/AndroidRuntime(321):  at java.lang.reflect.Method.invokeNative(Native Method)
05-24 10:07:03.404: E/AndroidRuntime(321):  at java.lang.reflect.Method.invoke(Method.java:521)
05-24 10:07:03.404: E/AndroidRuntime(321):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-24 10:07:03.404: E/AndroidRuntime(321):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-24 10:07:03.404: E/AndroidRuntime(321):  at dalvik.system.NativeStart.main(Native Method)
05-24 10:07:07.014: I/Process(321): Sending signal. PID: 321 SIG: 9
0
Merci mec, j'ai trouvé l'erreur lors du reformatage du code.
ajouté l'auteur GrimJov, source
Et veuillez corriger le format de votre question.
ajouté l'auteur ThiefMaster, source

1 Réponses

isSwitcher = setFactory (this);

->

setFactory retourne null

->

si (drapeau)     isSwitcher.setImageResource (photos [position]);  autre     isSwitcher.setImageResource (autres [position]);

jette NPE ici parce que isSwitcher est null

0
ajouté
Oui merci. Vous le devinez bien:)
ajouté l'auteur GrimJov, source
Comment puis-je accéder à l'image sélectionnée à partir d'un événement de bouton. Pouvez-vous m'aider s'il vous plaît.
ajouté l'auteur GrimJov, source