当我调用这个AlertDialog时它变空了

时间:2017-10-18 13:35:16

标签: android android-alertdialog

当我调用此警报对话框时,它显示为空,由于某种原因,视图未显示,而不是仅向我显示带有应用程序名称和单词"关于"但视图现在显示:

public class AboutBox {
static String VersionName(Context context) {
    try {
        return context.getPackageManager().getPackageInfo(context.getPackageName(),0).versionName;
    } catch (PackageManager.NameNotFoundException e) {
        return "Unknown";
    }
}
public static void Show(Activity callingActivity) {
    //Use a Spannable to allow for links highlighting
    SpannableString aboutText = new SpannableString("Version " + VersionName(callingActivity)+ "nn"
            + callingActivity.getString(R.string.about));
    //Generate views to pass to AlertDialog.Builder and to set the text
    View about;
    TextView tvAbout;
    try {
        //Inflate the custom view
        LayoutInflater inflater = callingActivity.getLayoutInflater();
        about = inflater.inflate(R.layout.aboutbox, (ViewGroup) callingActivity.findViewById(R.id.aboutView));
        tvAbout = (TextView) about.findViewById(R.id.aboutText);
    } catch(InflateException e) {
        //Inflater can throw exception, unlikely but default to TextView if it occurs
        about = tvAbout = new TextView(callingActivity);
    }
    //Set the about text
    tvAbout.setText(aboutText);
    // Now Linkify the text
    Linkify.addLinks(tvAbout, Linkify.ALL);
    //Build and show the dialog
    new AlertDialog.Builder(callingActivity)
            .setTitle("About " + callingActivity.getString(R.string.app_name))
            .setCancelable(true)
            .setIcon(R.drawable.ic_launcher)
            .setPositiveButton("OK", null)
            .setView(about)
            .show();    //Builder method returns allow for method chaining
}

}

enter image description here

1 个答案:

答案 0 :(得分:0)

在调用callingActivity期间将getApplicationContext()替换为getString(),如下所示:

new AlertDialog.Builder(callingActivity)
            .setTitle("About " + getApplicationContext().getString(R.string.app_name))
            .setCancelable(true)
            .setIcon(R.drawable.ic_launcher)
            .setPositiveButton("OK", null)
            .setView(about)
            .show();