2012-01-27 10 views
0

を使用しようとすると、私は私の最初のAndroidアプリケーションを書いていると私は私は2つの別々のダイアログを表示したいのですが1つの活性を有する:Androidのアクティビティがクラッシュ私はAlertDialogとカスタムダイアログ

ダイアログA - シンプルにテキストを表示して消滅するAlertDialog ダイアログB - 編集テキストと保存とキャンセルのボタンを含む「名前を付けて保存」ポップアップ

AlertDialogsとカスタムダイアログの作成に関するチュートリアルが見つかりました。それぞれが働くことができますが、別々にしかありません。 onCreateDialogメソッドのswitch/caseステートメントにすべてのコードを書き込もうとすると、AlertDialogの起動時にアプリケーションがクラッシュします。

は、ここに私のonCreateDialogコードです:。

@Override 
    protected Dialog onCreateDialog(int id) { 
     switch (id) { 
     // Display dialog 
     case 0: 
      AlertDialog.Builder alertDialog = new AlertDialog.Builder(this); 
      alertDialog.setMessage(messageText); 
      alertDialog.setNegativeButton(android.R.string.ok, 
        new DialogInterface.OnClickListener() { 

       public void onClick(DialogInterface dialog, int which) { 

       } 
      }); 
      return alertDialog.create();  
     // Save As dialog 

     case 1: 
      Dialog dialog = new Dialog(this); 
      dialog.setContentView(R.layout.save_as); 
      dialog.setTitle("Save as:"); 

      Button cancel = (Button)findViewById(R.id.cancel); 
      cancel.setOnClickListener(new View.OnClickListener() { 

       public void onClick(View v) { 

       } 

      }); 
      return dialog; 
     } 
     return null; 

    } 

私は両方のケースを入れたときにどちらの場合は、それ自体で動作しますが、アプリケーションがクラッシュします

ここでカスタムダイアログのレイアウトのためのXMLです:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:padding="10dp"> 

    <TextView 
     android:text="Save this list as:" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"/> 
    <EditText 
     android:id="@+id/list_name" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"/> 

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:background="@android:drawable/bottom_bar" 
    android:paddingLeft="4.0dip" 
    android:paddingTop="5.0dip" 
    android:paddingRight="4.0dip" 
    android:paddingBottom="1.0dip" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 
     <Button 
      android:id="@+id/save" 
      android:layout_width="0.0dip" 
     android:layout_height="fill_parent" 
     android:text="Save" 
     android:layout_weight="1.0" 
      ></Button> 
     <Button 
      android:id="@+id/cancel" 
      android:layout_width="0.0dip" 
     android:layout_height="fill_parent" 
     android:text="Cancel" 
     android:layout_weight="1.0" 
      ></Button> 

    </LinearLayout> 

</LinearLayout> 

私はちょうど1つのフォーマットまたは他のものに固着すべきですか?私はDialogFragmentsが現在推奨されていることも読んだことがありますが、まだそれらについては初心者レベルのチュートリアルは見つかりませんでした。どんな提案も高く評価されます。

+0

補足:私が修正した不正な引数例外のためにAlertDialogがクラッシュしていました。 AlertDialogを起動しない場合でも、カスタムダイアログはクラッシュします。ケース0が存在しない場合に機能します。 – Matt

+0

私はそれの一部を考え出しました。 2つのダイアログはswitch/caseステートメントで機能します。不正な引数の例外のためにAlertDialogがクラッシュし、cancel.setOnClickListenerでカスタムダイアログがクラッシュしました。私は私のEditTextを引っ張って、その上にsetText()メソッドを使ってみました。そしてそれもクラッシュしましたので、ダイアログのセッターに問題があるようです。私はそれについてさらに研究をします。 – Matt

+0

レイアウトを膨張させるとクラッシュはなくなりましたが、セッターはまだ動作しません。 – Matt

答えて

0

私は最終的にダイアログとの間でデータを渡す必要があることを認識しました。低APIをターゲットにしていますので、名前を付けて保存ダイアログをアクティビティに変更しました。しかし、ダイアログの多くの制限を学びました....

関連する問題