1

このコードを使用している共有設定に保存されている値をクリアしたい。

/*  SharedPreferences myPrefs = this.getSharedPreferences("myPrefs",
            Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = myPrefs.edit();
            editor.clear();
            editor.commit();   */

しかし、このエラーが発生します。

The method getSharedPreferences(String, int) is undefined for the type new View.OnClickListener(){}

private OnClickListener logoBarListener = new OnClickListener(){/ * *(Javadoc以外)* * @see android.view.View.OnClickListener#onClick(android.view.View)* / public void onClick(View v){

        if (v.getId() == R.id.img_bottom_home) {

            showProgressBar(MainScreen.class);

        } else if (v.getId() == R.id.img_bottom_basket) {

            showProgressBar(ShopBasketGet.class);

        } else if (v.getId() == R.id.img_bottom_notification) {

            showProgressBar(Notification.class);

        } else if (v.getId() == R.id.img_bottom_login) {
            SharedPreferences myPrefs = getSharedPreferences("myPrefs",
                    MODE_WORLD_READABLE);
            SharedPreferences.Editor editor = myPrefs.edit();
            editor.clear();
            editor.commit();   

            showProgressBar();
        }
    }
};
4

8 に答える 8

1

thisキーワードを削除 ( に変更this.getSharedPreferences().はgetSharedPreferences()this内部クラスを指しますがView.onClickListener()、メソッドは実際には Activity クラスにあります。

于 2012-05-31T10:09:34.590 に答える
0

次のコードスニペットを使用する

    Context context=YourActivityName.this;
    SharedPreferences myPrefs = context.getSharedPreferences("myPrefs",
    Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = myPrefs.edit();
    editor.clear();
    editor.commit();  

お役に立てれば、

Vipul

于 2012-05-31T10:25:57.577 に答える
0

Activity クラスで LinearLayout クラスのオブジェクトを作成している場合は、Constructor で Context を渡す必要があります。

下はスニーカー

活動クラス

public class SampleActivity extends Activity
{
    public void onCreate(Bundle bundle)
     {
         MyLinearLayout layout = new MyLinearLayout(this); 
         -----
         -----
     }  
}

MyLinearLayout クラス

public class MyLinearLayout extends LinearLayout {
    private Context context;
    public MyLinearLayout(Context context) {
        super(context);
        this.context=context; 
        SharedPreferences preferences=context.getSharedPreference("pref",
                context.MODE_PRIVATE);

    }

}

于 2012-05-31T10:58:25.673 に答える
0

この方法を試してください

 SharedPreferences pref = YourActivityName.this.getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
 pref.edit().clear().commit();

この方法を使用するよりも、onclickイベントをクリアしようとしていると思います

SharedPreferences myPrefs = v.getContext().getSharedPreferences("myPrefs",Context.MODE_PRIVATE)
SharedPreferences.Editor editor = myPrefs.edit();
editor.clear();
editor.commit();   
于 2012-05-31T10:25:04.570 に答える
0
Editor editor = getSharedPreferences("sharedPreferenceName", Context.MODE_PRIVATE).edit();
editor.clear();
editor.commit();
于 2014-07-07T11:33:30.700 に答える