English 中文(简体)
应用程序在快速更改已承诺的首选项后正在崩溃( NAME OF TRANSLATORS)
原标题:app is crashing after quickly changes preferences with commit(

如果用户想要保存新值, 我需要检查首选项中的用户输入 。 我相信代码是正确的... 因为如果我缓慢地改变数值, 没有任何问题。 如果我改变得太快, 应用程序就会崩溃 。

i 在编辑首选项时使用承诺 () 。 这是太慢的方法吗?

我尝试的是什么呢? 如果用户给出了一个空字符串, 我给出了一个提醒对话框和实习生, 我将前置修改为旧值 。 以下是代码 :

public class Preferences extends PreferenceActivity implements OnSharedPreferenceChangeListener {
//save settings
    static String SMStext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            addPreferencesFromResource(R.xml.preferences);

            SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);

            //-- First get SMStext and save in Strings
            SMStext = sp.getString("SMSText", "0");

            //SharedPreferences.Editor prefEditor = sp.edit();
            sp.registerOnSharedPreferenceChangeListener(this);   

    }


  @Override
  public void onSharedPreferenceChanged(SharedPreferences sp, String key) {

    String smstriggerworderror = "String cannot be empty";

    if (key.equals("SMSText")) {

        if  sp.getString("SMSText", "0").equals(""){

            SharedPreferences.Editor prefEditor = sp.edit();

            prefEditor.putString("SMSText", SMStext);
            prefEditor.commit();
                //write current value back to value and refresh interface
            SMStext = sp.getString("SMSText", "0");
                //Show alert dialog
            showdialog(smstriggerworderror);
            //finish();

        }
        //write current value back to value
        SMStext = sp.getString("SMSText", "0");
    }


private void showdialog(String message) {
      //create alertbox
      AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
      alertbox.setMessage(message);
      alertbox.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
      // Click listener on the neutral button or alert box
          public void onClick(DialogInterface arg0, int arg1) {
              finish();
          }
      });

      // show the alert box
      alertbox.show();      
}
最佳回答

on SharedPrefEditor.commit (); 这将调用 on SharedPreviewCheanged 收听器, 然后将 commit () 一遍又一遍地调用 commit () ,直到......... < 坚固 > StackOver错误 。

固定 :

不要改变您在 On sharedPreference 更改的听众中的共享首选项 。

问题回答

暂无回答




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签