简体   繁体   中英

Dialog not displaying when button is clicked - Android

I am trying to get a dialog box to display. Right now the program does not crash so there is no LogCat to display. When the button is clicked, nothing happens that can be seen. Is there a line of code I am missing that is preventing the dialog box from displaying?

Java code:

public class Results extends Activity {

    Button detailsBtn;
    final Context context = this;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.resultsmain);

        detailsBtn = (Button)findViewById(R.id.detailsBtn);
        detailsBtn.setText("Details");

        detailsBtn.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {

                final Dialog dialog = new Dialog(context);
                dialog.setContentView(R.layout.resultsdetailsdisplay);
                dialog.setTitle("Detailssss - TESTING");

                TextView title = (TextView)dialog.findViewById(R.id.title);
                title.setText("TITLE - TESTING");

                Button close = (Button)dialog.findViewById(R.id.close);

                close.setOnClickListener(new OnClickListener() {
                    public void onClick(View arg0) {
                        dialog.dismiss();
                    }
                });
            }
        });
    }
}

resultsdetailsdisplay.xml:

<RelativeLayout 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/scroll" >

    <TextView
        android:id="@+id/title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="12sp"
        android:layout_centerHorizontal="true"
        android:gravity="center" />
</RelativeLayout>

It's probably because you haven't called the show() method for your dialog . Add

dialog.show();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM