试图解决Dialog问题

时间:2013-10-11 01:00:24

标签: android

以下是我用来向用户显示信息的alertDialog。

final AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);

alertDialog.show();
alertDialog.setIcon(R.drawable.ic_launcher);
alertDialog.setTitle("Title");
alertDialog.setMessage("dialog");

alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {

    public void onClick(DialogInterface dialog, int which) {

现在,我一直在研究并查看stackoverflow,看看如何让这个对话框显示一次,我发现了一些可能有用的东西。我研究了这段代码并将其分解到我在这方面的知识极限,但我似乎无法弄清楚问题是什么。

        final SharedPreferences settings = getSharedPreferences("pref_name", 0);
        boolean disappear = settings.getBoolean("installed", false);

        if (disappear == false) {

            final AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
            AlertDialog alert = alertDialog .create();

            alertDialog.setIcon(R.drawable.ic_launcher);
            alertDialog.setTitle("Title");
            alertDialog.setMessage("dialog");

            alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                settings.edit().putBoolean("installed", true).commit();
    }
alertDialog.show();

问题在于,当我在模拟器上启动此应用程序时,我的对话框没有显示,我的应用程序中没有任何内容响应任何内容,真的类似于此链接中的这个问题,我发现Shared Prefence for alert dialog is making my application non responsive我必须试图打破这个代码,并试图弄清楚为什么会发生这种情况,但我限制了我在这方面的知识。有人可以帮助解决这个问题。

10-13 03:10:37.496: E/AndroidRuntime(761): FATAL EXCEPTION: main
10-13 03:10:37.496: E/AndroidRuntime(761): android.app.SuperNotCalledException: Activity {com.theproblemsolver/com.theproblemsolver.MainActivity} did not call through to super.onPause()
10-13 03:10:37.496: E/AndroidRuntime(761):  at android.app.Activity.performPause(Activity.java:5210)
10-13 03:10:37.496: E/AndroidRuntime(761):  at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1226)
10-13 03:10:37.496: E/AndroidRuntime(761):  at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3002)
10-13 03:10:37.496: E/AndroidRuntime(761):  at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2971)
10-13 03:10:37.496: E/AndroidRuntime(761):  at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2949)
10-13 03:10:37.496: E/AndroidRuntime(761):  at android.app.ActivityThread.access$800(ActivityThread.java:141)
10-13 03:10:37.496: E/AndroidRuntime(761):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1245)
10-13 03:10:37.496: E/AndroidRuntime(761):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-13 03:10:37.496: E/AndroidRuntime(761):  at android.os.Looper.loop(Looper.java:137)
10-13 03:10:37.496: E/AndroidRuntime(761):  at android.app.ActivityThread.main(ActivityThread.java:5041)
10-13 03:10:37.496: E/AndroidRuntime(761):  at java.lang.reflect.Method.invokeNative(Native Method)
10-13 03:10:37.496: E/AndroidRuntime(761):  at java.lang.reflect.Method.invoke(Method.java:511)
10-13 03:10:37.496: E/AndroidRuntime(761):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-13 03:10:37.496: E/AndroidRuntime(761):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-13 03:10:37.496: E/AndroidRuntime(761):  at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

你错过了两件事

                // creation of alert dialog
                AlertDialog alert = alertDialog .create();

                // show it
                alert.show();

在底部写下两件事。

答案 1 :(得分:0)

这是您的代码修改和工作

 final SharedPreferences settings = getSharedPreferences("pref_name", 0);
    boolean disappear = settings.getBoolean("installed", false);

    if (disappear == false) {

        final AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);

        alertDialog.setIcon(R.drawable.ic_launcher);
        alertDialog.setTitle("Title");
        alertDialog.setMessage("dialog");

        alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
            settings.edit().putBoolean("installed", true).commit();
}
 //////////////////////
 AlertDialog alertDialog = alertDialogBuilder.create();
 alertDialog.show();