2017-12-12 6 views
0

私が作っているアプリの一部です。オプションメニューでボタンが選択されています。そのボタンをクリックすると、カスタムダイアログが開きます。私は新しいダイアログを開くためにそのカスタムダイアログ上のボタンが必要ですが、何らかの理由で私の方法によってアプリケーションがクラッシュします。どんな援助も高く評価されるだろう。カスタムダイアログのボタンをクリックしてダイアログを作成するには?

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_client_app); 

    //Copy and paste this line to onClick methods regarding the edit text dialog 
    // textentry = (EditText)findViewById(R.id.entrybox); 


    // Button year = (Button)findViewById(R.id.Year); 



} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.options, menu); 
    return true; 
} 

//Dialog Box for selecting View books in Options Menu 
public Dialog ViewBookOptions() { 
    AlertDialog.Builder builder = 
      new AlertDialog.Builder(this); 
    LayoutInflater inflater = this.getLayoutInflater(); 
    View v = inflater.inflate(R.layout.view_books, null); 
    builder.setView(v); 
    final AlertDialog dialog = builder.create(); 
    return dialog; 



} 

//Dialog boxes for selecting Year, Author, or Title in the ViewBook Dialog Box 

public Dialog ViewByYear(){ 
    AlertDialog.Builder builder = 
      new AlertDialog.Builder(this); 
    LayoutInflater inflater = this.getLayoutInflater(); 
    View v = inflater.inflate(R.layout.text_entry, null); 
    builder.setView(v); 
    final EditText entrybox = (EditText) v.findViewById(R.id.entrybox); 
    builder.setPositiveButton("Ok", 
      new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialogInterface, int i) { 
        String entry = entrybox.getText().toString().toLowerCase(); 
        //This is Where You Will Populate The ListView With Database Stuff 
       } 
      }); 
    builder.setNegativeButton(
      "Cancel", 
      new DialogInterface.OnClickListener() { 
         public void onClick(
           DialogInterface dialog, int id) { 
          dialog.cancel(); 
         } 
        }); 
    AlertDialog dialog = builder.create(); 
    return dialog; 

} 

      //Quick Toast for checking stuff 
       public void toast(String string){ 
       Toast.makeText(this, string, Toast.LENGTH_SHORT).show(); 
     } 
     //All of my button OnClick Methods 
     public void allbooks(View view){ 
     toast("You clicked All books"); 
     } 
      public void year(View view){ 
    toast("Dialog should open and EditText should change hint text"); 
    EditText textentry = (EditText)findViewById(R.id.entrybox); 
     textentry.setHint("Enter the Year of the Book(s)"); 
     Dialog dialog = ViewByYear(); 
    dialog.show(); 
     } 


     // The Options Menu 
     @Override 
     public boolean 
     onOptionsItemSelected 
     (MenuItem item) { 
      switch (item.getItemId()) { 
     case R.id.option1: 
      //Change to View Book Options 
      Dialog dialog = ViewBookOptions(); 
      dialog.show(); 
      return true; 
     case R.id.option2: 

      return true; 
     case R.id.option3: 

      return true; 
     case R.id.option4: 

      return true; 
     case R.id.option5: 

      return true; 
     default: 
      return super.onOptionsItemSelected(item); 
      } 
     } 

これは私のアクティビティコードです。私は、エラーは、最初のダイアログは、2番目のダイアログが作成される前に却下されていないことと関係していると信じていますが、私のダイアログは異なるインスタンスによって異なるボタンによって呼び出されるので、これを修正する方法はわかりません。

+0

致命的なエラーログを投稿できますか? –

+1

私は 'DialogFragment'を使うことをお勧めします。それはあなたのための回転などのすべてのアクションを扱うことができます。ここで例を挙げてドキュメントをチェックしてください:https://developer.android.com/reference/android/app/DialogFragment.html – Micer

+0

これを今見てください。本当にありがとう! – John

答えて

0

あなたは試してみませんか甘い警告ダイアログAndroidライブラリー?コード内でネストされたダイアログの使用をサポートしています。ここにリンクがあります:https://github.com/pedant/sweet-alert-dialog

関連する問題