2011-04-13 6 views
0

J'ai suivi la documentation d'Android pour créer une boîte de dialogue personnalisée. Lorsque je clique sur le bouton qui lance ma boîte de dialogue personnalisée, l'application. des accidents. Comment puis-je commencer à résoudre ce problème?Lancement d'une boîte de dialogue personnalisée

La boîte de dialogue:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/relativeLayout1" android:gravity="center_vertical" android:layout_height="wrap_content" android:layout_width="fill_parent"> 
<CheckBox android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/chkBlueTooth" android:id="@+id/chkBlueTooth" android:layout_below="@+id/chkAudio" android:layout_alignLeft="@+id/chkAudio"></CheckBox> 
<Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_below="@+id/chkBlueTooth" android:layout_alignLeft="@+id/chkBlueTooth" android:id="@+id/btnOK" android:text="@string/btnOK"></Button> 
<CheckBox android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/chkNetwork" android:id="@+id/chkNetwork" android:layout_above="@+id/chkBlueTooth" android:layout_toRightOf="@+id/chkBlueTooth"></CheckBox> 
<CheckBox android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/chkWifi" android:id="@+id/chkWifi" android:layout_below="@+id/chkNetwork" android:layout_alignLeft="@+id/chkNetwork" android:layout_alignRight="@+id/chkNetwork"></CheckBox> 
<CheckBox android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/chkAudio" android:id="@+id/chkAudio" android:layout_below="@+id/txtChoose" android:layout_alignLeft="@+id/txtChoose"></CheckBox> 
</RelativeLayout> 

Le code de la boîte de dialogue: static final int TIME_DIALOG_ID = 0; statique final int POWER_OFF_OPTIONS = 1;

@Override protected Boîte de dialogue onCreateDialog (int id) { Boîte de dialogue; switch (id) { case TIME_DIALOG_ID: Renvoyer new TimePickerDialog (this, mTimeSetListener, mHour24, mMinute, false);

 case POWER_OFF_OPTIONS: 
      AlertDialog.Builder builder; 
      AlertDialog alertDialog; 

      Context mContext = getApplicationContext(); 
      LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE); 
      View layout = inflater.inflate(R.layout.options, 
        (ViewGroup)findViewById(R.id.relativeLayout1)); 
      //Capture view elements 
      mChkAudio = (CheckBox) findViewById(R.id.chkAudio); 
      mChkBluetooth = (CheckBox) findViewById(R.id.chkBlueTooth); 
      mChkNetwork = (CheckBox) findViewById(R.id.chkNetwork); 
      mChkWifi = (CheckBox) findViewById(R.id.chkWifi); 
      mBtnOK = (Button) findViewById(R.id.btnOK); 
      mBtnOK.setOnClickListener(new View.OnClickListener() { 
       public void onClick(View v) { 
        DismissPowerOptions(); 
        } 
      }); 
      builder = new AlertDialog.Builder(mContext); 
      builder.setView(layout); 
      alertDialog = builder.create(); 
      //return builder; 
     default: 
      dialog = null; 
     } 
     return dialog; 
    } 

Le bouton qui montre la boîte de dialogue et bloque l'application:

mBtnPowerOffOptions.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      //stuff for Options dialog 
      showDialog(POWER_OFF_OPTIONS); 
     } 
    }); 
+0

Quel est le message de débogage? Vous devez décomposer le code en actions simples jusqu'à ce que cela fonctionne. Ensuite, ajoutez de plus en plus de fonctionnalités. – Thorben

Répondre

1

Vous appelez findViewById sur le mauvais objet; la vue parent (ou activité).

Après builder.setView (...) ne

 
      AlertDialog myDialog = builder.create(); 

Maintenant, vous pouvez faire vos appels à findViewById

 
      //Capture view elements 
      mChkAudio = (CheckBox) myDialog.findViewById(R.id.chkAudio); 
       . 
       . 
       . 
+0

Je n'ai jamais réussi à faire fonctionner ça, et j'ai abandonné avec le dialogue personnalisé. Je pourrais probablement le faire fonctionner maintenant que j'ai plus d'expérience de programmation d'androïde mais n'ai pas besoin de maintenant. Ce que j'ai fait était de construire une boîte de dialogue d'alerte liste personnalisée. Il y a un exemple similaire ici: http://stackoverflow.com/questions/3608018/toggling-check-boxes-in-multichoice-alertdialog-in-android – Andrew

Questions connexes