Can someone get this custom AlertDialog to actuall

2019-02-28 16:22发布

问题:

I asked another question about custom AlertDialog here.

Then I clicked my way to this custom AlertDialog (found here):

import android.app.AlertDialog;
import android.content.Context;
import android.webkit.WebView;

/**
 * Display a simple about dialog.
 */
public class AboutDialog extends AlertDialog {

    protected AboutDialog(Context context) {
        super(context);
        setContentView(R.layout.about_dialog);

        setTitle(R.string.about_title);
        setCancelable(true);

        WebView webView = (WebView) findViewById(R.id.webview);
        webView.loadData("Written by Cédric Beust (<a href=\"mailto:cedric@beust.com\">cedric@beust.com)", "text/html", "utf-8");
    }

}

I modified like this:

import android.app.AlertDialog;
import android.content.Context;
import android.webkit.WebView;

/**
 * Display a simple about dialog.
 */
public class AboutDialog extends AlertDialog {

    protected AboutDialog(Context context) {
        super(context);
        setTitle("Test");
        setCancelable(true);
        setContentView(R.layout.paus);
    }
}

and then tried to use it, like this:

AboutDialog ad = new AboutDialog(getApplicationContext());
ad.show();

But I get this error:

android.util.AndroidRuntimeException: requestFeature() must be called before adding content
    at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:181)
    at com.android.internal.app.AlertController.installContent(AlertController.java:206)
    at android.app.AlertDialog.onCreate(AlertDialog.java:251)
    at android.app.Dialog.dispatchOnCreate(Dialog.java:307)
    at android.app.Dialog.show(Dialog.java:225)
    at TestPackage.MainActivity$5.onClick(MainActivity.java:382)
    at android.view.View.performClick(View.java:2538)
  etc...

So I'd like to know why this is happening.

===================== EDIT ==========================0

As per suggestions below, I modified the code so it looks like this:

import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.webkit.WebView;

/**
 * Display a simple about dialog.
 */
public class AboutDialog extends AlertDialog {

    protected AboutDialog(Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.paus);

    }
}

But I get a BadTokenException instead.

回答1:

override on create method of altertdialog and call following methods in overridden method oncreate

 setContentView(R.layout.paus);

this will solve your problem.



回答2:

setContentView() must be called before setTitle() ...



回答3:

why don't you add a title (a text view) in R.layout.param and also you can add buttons and then everything will be shown