警报对话框的“共享首选项"使我的应用程序无响应 [英] Shared Preference for alert dialog is making my application non responsive

查看:76
本文介绍了警报对话框的“共享首选项"使我的应用程序无响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这里有一些通常的问题.我有一个alertdialog,它会在我的应用程序启动后立即启动,并且一旦用户单击确定"按钮,该对话框将永远不会再次显示,除非已将其删除并重新安装.当我第一次在模拟器上尝试它时,它就起作用了.第一次,我的意思是当我启动该应用程序后,就完成了为Alertdialog的共享首选项编写代码的工作.但是,当我关闭仿真器并再次启动我的应用程序时,不会显示alertdialog并且我的应用程序不响应任何内容.我不知道以前是否有人发生过这种情况,也不知道是否会发生这种情况.有人可以帮助我了解发生的情况以及为什么应用程序在首次启动后没有响应.另外,我的logcat也没有显示任何错误.

I have a bit of usual problem here. I have a alertdialog that launches as soon as my application has been launched and as soon as the user clicks the ok button that dialog will never display again unless it has been deleted and installed again. It works when I try it on my emulator for the first time and by first time I mean when I launch the application as soon I am done writing the code for the shared preference for the alertdialog. But when I close the emulator and launch my application again, the alertdialog doesn't display and my application doesn't respond to anything. I do not know if this happened to anybody before and I do not know if this was suppose to happen. Can somebody help me understand what is going and why application doesn't respond to anything after the first time the application has been launched. Also my logcat did not display any errors either.

public class MainActivity extends Activity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



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

        if(!installed){

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

            alertDialog.setTitle("Title");
            alertDialog.setIcon(R.drawable.ic_launcher);
            alertDialog.setAdapter(new MyAdapter(), null);

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

                public void onClick(DialogInterface dialog, int which) {
                    SharedPreferences.Editor editor = settings.edit();
                    editor.putBoolean("installed", true);
                    editor.commit();

                }
            });

            alertDialog.show();

            final EditText et = (EditText) findViewById(R.id.editText1);
            Button getAnswer = (Button) findViewById(R.id.button1);
            getAnswer.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {       
                    if (et.getText().toString().length()==0) {
                        Toast.makeText(getApplicationContext(),"Can't Be Blank!",Toast.LENGTH_LONG).show();             

                    }else{
                        EditText et = (EditText) findViewById(R.id.editText1);
                        String searchTerm = et.getText().toString().trim();         
                        Intent in = new Intent(MainActivity.this, ListView.class);
                        in.putExtra("TAG_SEARCH", searchTerm);
                        startActivity(in);
                    }

                }
            });
        }
     }


    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
    }}

推荐答案

您需要移动此代码

 final EditText et = (EditText) findViewById(R.id.editText1);
        Button getAnswer = (Button) findViewById(R.id.button1);
        getAnswer.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {       
                if (et.getText().toString().length()==0) {
                    Toast.makeText(getApplicationContext(),"Can't Be Blank!",Toast.LENGTH_LONG).show();             

                }else{
                    EditText et = (EditText) findViewById(R.id.editText1);
                    String searchTerm = et.getText().toString().trim();         
                    Intent in = new Intent(MainActivity.this, ListView.class);
                    in.putExtra("TAG_SEARCH", searchTerm);
                    startActivity(in);
                }

            }
        });
    }

代码的if部分中的

.正如Shobhit所说,下次运行应用程序时,此操作将永远不会运行.仅当installedfalse时才运行,而第一次运行后永远不会为真.

out of the if part of your code. As Shobhit is saying, this is never getting run the next time you run your app. It only runs if installed is false which is never true after the first run.

使用Dialog

Edit-Avoid window leak errors with the Dialog

您始终可以检查是否用dialog.isShowing()打开了Dialog,如果在活动被销毁或其他内容(另一个Activity)出现在顶部之前返回true,则关闭对话框.您可以在onPause()中完成此操作.

You can always check if the Dialog is open with dialog.isShowing() and close the Dialog if returns true before the Activity is destroyed or something (another Activity) comes on top. You can do this in onPause().

@Override
public void onPause()
{
    if (dialog != null && dialog.isShowing())
    {    
         dialog.dismiss();    
         super.onPause();
    }
}

这篇关于警报对话框的“共享首选项"使我的应用程序无响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆