Skip to content Skip to sidebar Skip to footer

Getting Error In Manipulating Shared Preferences?

I want that the disclaimer should come 1st time after installing my app ! For that in my MainActivity I have make 2 buttons : 1st 'agree' button will store boolean 'true' vale in t

Solution 1:

Make sure you declare your Welcome activity in your AndroidManifest.xml file.

Final Answer

Ok, this isn't the most elegant way to do this, but it'll get ya started. Afterward, I'd suggest you investigate Ok/Cancel dialogs with long text, and use one of those instead of having two activities.

MainActivity

publicclassMainActivityextendsActivityimplementsOnClickListener 
{ 
    publicstaticStringfilename="MySharedString"; 
    publicstaticfinalStringPREFS_NAME="MyPrefsFile"; 

@OverrideprotectedvoidonCreate(Bundle state) 
{ 
    super.onCreate(state); 
    setContentView(R.layout.main); 

    TextView text1=(TextView)findViewById(R.id.txt1); 
    TextViewtext2= (TextView)findViewById(R.id.txt2);
    text2.setText("Android custom dialog example!"); 

    ImageViewimage= (ImageView)findViewById(R.id.imageView1);
    image.setImageResource(R.drawable.ic_launcher); 

    ButtonagreeButton= (Button)findViewById(R.id.button1);
    ButtondisagreeButton= (Button)findViewById(R.id.button2);

    agreeButton.setOnClickListener(this);
    disagreeButton.setOnClickListener(this); 

    booleanagree= getSharedPreferences(filename, 0).getBoolean("agreement", false);
    if(agree)
    {
        openWelcome();
    }           
}

@OverridepublicvoidonClick(View v) 
{ 
    switch (v.getId()) 
    { 
        case R.id.button1: 
            SharedPreferencessettings= getSharedPreferences(filename, 0);             
            SharedPreferences.Editoreditor= settings.edit(); 

            editor.putBoolean("agreement", true); 
            editor.commit();        

            openWelcome();

            break; 

        case R.id.button2: 
            finish(); 
    }
}

privatevoidopenWelcome()
{
    Intent i=newIntent(this, Welcome.class);
    startActivity(i);

    finish();
    }
}

Solution 2:

Its just that you first have to set the value for in `Sharedpreference and then after only you can access it.

Try as below:

switch (v.getId())
    { 
    case R.id.button1:
        //Add the value in Sharepreference
        SharedPreferences.Editoreditor= settings.edit();
        editor.putBoolean("agreement", true);
        editor.commit();

          //Get the value from it. SharedPreferencessettings= getSharedPreferences(filename, 0);
        booleanagreement= settings.getBoolean("agreement",false);

        Intent i=newIntent(getApplicationContext(),Welcome.class);
                    startActivity(i);
        break;
     }

Solution 3:

You don't get value from sharedprefs before assign value. You didn't assign value to "agreement" variable, how can you get value from it ? It would be like this;

@OverridepublicvoidonClick(View v)
{
    switch (v.getId())
    {
    case R.id.button1:
        SharedPreferencessettings= getSharedPreferences(filename, 0);
        SharedPreferences.Editoreditor= settings.edit();
        editor.putBoolean("agreement", true);
        editor.commit();

        //now you can get value of agreementbooleanagreement= settings.getBoolean("agreement",false);


        Intent i=newIntent(getApplicationContext(),Welcome.class);
                    startActivity(i);
        break;

         case R.id.button2:
             finish();
             System.exit(0);
         }
     }
}

Post a Comment for "Getting Error In Manipulating Shared Preferences?"