Note that there are some explanatory texts on larger screens.

plurals
  1. POWhere do I define my OnClickListener to close a custom dialog view in Android?
    text
    copied!<p>I have defined the following custom dialog view:</p> <pre><code>public class MyDialog extends Dialog { public MyDialog(Context context) { super(context); } @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.my_dialog); getWindow().setLayout(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); } } </code></pre> <p>The dialog uses the following layout which contains only a "Dismiss Me" button:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" &gt; &lt;Button android:id="@+id/dismiss_btn" android:layout_width="100dip" android:layout_height="30dip" android:layout_centerHorizontal="true" android:text="Dismiss me" android:textSize="8dip" android:textColor="#ffffff" /&gt; &lt;/RelativeLayout&gt; </code></pre> <p>My <code>MainActivity</code> displays a button, <code>triggerDialogBtn</code>, which will show my dialog when pressed. I also defined a handler method for the button <code>dismiss_btn</code> on my dialog which is intended to dismiss my dialog when pressed.</p> <pre><code>public class MainActivity extends Activity{ private Button triggerDialogBtn; private MyDialog myDialog; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //initialize my dialog myDialog = new MyDialog(this); //the button which will trigger the dialog to pop up Button triggerDialogBtn = (Button)findViewById(R.id.trigger_btn); triggerDialogBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { myDialog.show(); //my dialog pop up } }); // I would like to dismiss my dialog when the "Dismiss me" button on the dialog is pressed /**** BUT I GOT NULL POINTER EXCEPTION HERE ******/ //This is the "Dismiss me" button defined on dialog layout Button dismissMeBtn = (Button)findViewById(R.id.dismiss_btn); dismissMeBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { myDialog.dismiss(); } }); } } </code></pre> <p>Layout of <code>main.xml</code>:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"&gt; &lt;!-- The trigger button which will pop up my dialog when pressed --&gt; &lt;Button android:id="@+id/trigger_btn" android:layout_width="80dip" android:layout_height="20dip" android:text="Trigger dialog" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>The problem is that if I define the click handler for <code>dismiss_btn</code> (which is located on my dialog ) inside <code>MainActivity</code>, I cannot dismiss the dialog when <code>dismiss_btn</code> is pressed; it always throws <code>NullPointerException</code>. </p> <p>So, where and how can I define the handler to dismiss my dialog when the button on my dialog is pressed? </p> <h3>UPDATED</h3> <p>I also tried to put the <code>dismiss_btn</code> handler in my custom dialog view class as below:</p> <pre><code>public class MyDialog extends Dialog { public MyDialog(Context context) { super(context); } @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.my_dialog); Button dismissMeBtn = (Button)findViewById(R.id.dismiss_btn); //ERROR: The method setOnClickListener(View.OnClickListener) in the // type View is not applicable // for the arguments (new DialogInterface.OnClickListener(){}) dismissMeBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { ???.dismiss(); // What should replace "???" } }); getWindow().setLayout(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); } } </code></pre> <p>I get the indicated error don't know what to replace the question marks with.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload