Domanda

I have an activity with CheckBox & a Button. I used SharedPreferences to save the checkbox value.

The button saves the value of checkbox and opens another activity. The text of checkbox will explain it better.

check.setText("Show This Page On Start");

If checkbox is checked, this activity will be shown on start & if not, it will open another activity.

when i used this, the app force closes.

if (sharedPreferences.getBoolean("check_box_value", "false"))

    {  //launch another activity
    }
    else
    {
// do nothing
}
È stato utile?

Soluzione

// Try this way,hope this will help you...

**activity_main.xml**

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="5dp" >

    <CheckBox
        android:id="@+id/chkShowThisPage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show This Page On Start"
        android:checked="true"/>

    <Button
        android:id="@+id/btnGetCheckBoxValue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout

**MainActivity.java**

public class MainActivity extends Activity{

    private CheckBox  chkShowThisPage;
    private Button btnGetCheckBoxValue;
    SharedPreferences sharedPreferences;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        chkShowThisPage = (CheckBox) findViewById(R.id.chkShowThisPage);
        btnGetCheckBoxValue = (Button) findViewById(R.id.btnGetCheckBoxValue);
         sharedPreferences = getSharedPreferences(getString(R.string.app_name), MODE_PRIVATE);
        if(!sharedPreferences.getBoolean("showThisPage",true)){
            Intent intent = new Intent(this,YourAnotherActivity.class);
            startActivity(intent);
            finish();
        }


        btnGetCheckBoxValue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putBoolean("showThisPage",chkShowThisPage.isChecked());
                editor.commit();
                Intent intent = new Intent(MainActivity.this,YourAnotherActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }
}

Altri suggerimenti

if (sharedPreferences.getBoolean("check_box_value", false))

    {  //launch another activity
    }
    else
    {
// do nothing
}

this is the correct way to access a boolean value

you are passing string in getBoolean(see "false" in your code. its mean this is string you need to pass false without "").that is not the correct way to do.try this way

boolean isChecked = sharedPreferences.getBoolean("check_box_value", false)

if (isChecked)
{ 
     //launch another activity
}
else
{
   // do nothing
}

You are passing a String as the default value for the SharedPreference to use if it's not set, which may cause your app to FC.

Try writing your condition passing a boolean as default:

 if (sharedPreferences.getBoolean("check_box_value", false)
     {...}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top