共享prefence警报对话框让我的应用程序无响应 [英] Shared Prefence for alert dialog is making my application non responsive
问题描述
我有一点平时这里的问题。我有一个alertdialog,一旦启动我的应用程序已经启动,只要用户点击OK按钮的对话框将不再显示,除非它已被删除,并重新安装。它的工作原理,当我试图在我的模拟器,第一次和第一次我的意思是,当我启动应用程序,一旦我写完了code的共享preference的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();
}}
如果你需要我详细说明,请让我知道
if you need me to elaborate please let me know
推荐答案
您需要将这个code
You need to move this code
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);
}
}
});
}
出,如果
部分的code。由于Shobhit是说,这是从来没有得到执行,下一次你运行你的应用程序。它只有在安装运行
是假
这是不正确的后第一次运行。
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.
修改,避免与窗口泄漏错误的对话框
Edit-Avoid window leak errors with the Dialog
您可以随时检查对话框
与 dialog.isShowing()打开
,如果返回关闭对话框之前的活动被破坏,或某事(另一个活动
)来在上面实现。您可以在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();
}
}
这篇关于共享prefence警报对话框让我的应用程序无响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!