2015-05-04 65 views
0

我創建了一個AlertDialog如何從AlertDialog開始活動?

public class ConfirmChoixDownloadDialogView extends AlertDialog { 

    private Activity activity; // this activity shows this Dialog 

    private View contenu, titleBar; 

    private final int OUI = DialogInterface.BUTTON_POSITIVE; 
    private final int NON = DialogInterface.BUTTON_NEGATIVE; 
    private final int NEUTRE = DialogInterface.BUTTON_NEUTRAL; 

    public static final int requestCode = 100; 

    private int btnClicked = NEUTRE; 

    private AsyncTask<String, Void, Void> indirectTask; 

    @SuppressLint("InlinedApi") 
    public ConfirmChoixDownloadDialogView(Context context, LayoutInflater inflater) { 
     super(context, AlertDialog.THEME_HOLO_DARK); 
     contenu = inflater.inflate(R.layout.msg_dialog, null); 
     ((TextView)contenu.findViewById(R.id.msgText)).setText(context.getResources().getString(R.string.questionChargementWebVersMobile)); 
     titleBar = inflater.inflate(R.layout.custom_dialog_title, null); 
     ((ImageView)titleBar.findViewById(R.id.icone)).setImageDrawable(context.getResources().getDrawable(R.drawable.ic_action_about)); 
     ((TextView)titleBar.findViewById(R.id.titre)).setText(context.getResources().getString(R.string.titreConfirmMsgBox)); 
     setCustomTitle(titleBar); 
     setView(contenu, 0, 0, 0, 0); 
     setButton(DialogInterface.BUTTON_POSITIVE, context.getResources().getString(R.string.button_oui), new OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       btnClicked = OUI; 
       dialog.dismiss(); 
      } 
     }); 
     setButton(DialogInterface.BUTTON_NEGATIVE, context.getResources().getString(R.string.button_non), new OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       btnClicked = NON; 
       dialog.dismiss(); 
      } 
     }); 
     setOnDismissListener(dismissListener); 
    } 
    public void afficher(Activity act, AsyncTask<String, Void, Void> tacheIndirecte) { 
     activity = act; 
     indirectTask = tacheIndirecte; 
     show(); 
    } 
    private OnDismissListener dismissListener = new OnDismissListener() { 

     @Override 
     public void onDismiss(DialogInterface dialog) { 

      if (dialog instanceof ConfirmChoixDownloadDialogView) { 

       if (btnClicked == OUI) { 

        indirectTask.execute("commune.txt", 
             "parcelles.txt", 
             "rues.txt", 
             "batiments.txt", 
             "appartements.txt", 
             "occupants_appartement.txt", 
             "categories_activite.txt", 
             "natures_activite.txt", 
             "activites_dans_appart.txt", 
             "contribuables.txt"); 

       } 

       else if (btnClicked == NON) { 


        Intent i = new Intent(activity, SelectionQuartierActivity.class); 
        activity.startActivity(i); // it makes app crash 

       } 

      } 

     } 

    }; 

} 

正如你可以看到我想要的點擊DialogBUTTON_NEGATIVE時的情況下啓動一個activity。但它崩潰了!那麼如何在這種情況下啓動activity

+1

你能發佈崩潰日誌嗎? –

+0

我不能,因爲我在平板電腦上運行應用程序,並且它的驅動程序沒有被計算機識別。 – pheromix

+0

你不能使用模擬器?或者將https://try.crashlytics.com/添加到應用程序中? –

回答

0

好吧,我發現了尖開始activityactivity,其通過實施activityonWindowFocusChanged方法並且在的OnDismissListener內設置變量來打開!

1
showDialog(this, "", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      Intent i = new Intent(getApplicationContext(), NextActivty.class); 
      startActivity(i); 
      finish(); 
     } 
    }); 
0

在你的情況下,使用AlertDialog的情況下也許嘗試,而不是活動的調用它是這樣的:

context.startActivity(i);,而不是activity.startActivity(i)